2155 Problem Description(floyd算法变型)

Problem Description

Kudo’s real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, and Kudo is only her nickname.
Now, she is facing an emergency in her hometown:
Her mother is developing a new kind of spacecraft. This plan costs enormous energy but finally failed. What’s more, because of the failed project, the government doesn’t have enough resource take measure to the rising sea levels caused by global warming, lead to an island flooded by the sea.
Dissatisfied with her mother’s spacecraft and the government, civil war has broken out. The foe wants to arrest the spacecraft project’s participants and the “Chief criminal” – Kudo’s mother – Doctor T’s family.
At the beginning of the war, all the cities are occupied by the foe. But as time goes by, the cities recaptured one by one.
To prevent from the foe’s arrest and boost morale, Kudo and some other people have to distract from a city to another. Although they can use some other means to transport, the most convenient way is using the inter-city roads. Assuming the city as a node and an inter-city road as an edge, you can treat the map as a weighted directed multigraph. An inter-city road is available if both its endpoint is recaptured.
Here comes the problem
Given the traffic map, and the recaptured situation, can you tell Kudo what’s the shortest path from one city to another only passing the recaptured cities?

Input

The input consists of several test cases.
The first line of input in each test case contains three integers N (0

Output

For each case, print the case number (1, 2 …) first.
For each operation 0, if city x is already recaptured (that is,the same 0 x operation appears again), print “City x is already recaptured.”
For each operation 1, if city x or y is not recaptured yet, print “City x or y is not available.” otherwise if Kudo can go from city x to city y only passing the recaptured cities, print the shortest path’s length; otherwise print “No such path.”
Your output format should imitate the sample output. Print a blank line after each test case.

Sample Input

3 3 6
0 1 1
1 2 1
0 2 3
1 0 2
0 0
0 2
1 0 2
1 2 0
0 2
(空行)
0 0 0

Sample Output

Case 1:
City 0 or 2 is not available.
3
No such path.
City 2 is already recaptured.

解题思路:

1.Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法
2.这里我们只需要对每个刚修复的点进行两重循环(图中每两个点之间的距离通过新加入的点来更新)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define INF 1000000000
using namespace std;
int maps[310][310];
int book[310];
int d[310][310];
int main(){int n,m,q;int t=0;while(scanf("%d%d%d",&n,&m,&q)){if(n==0&&m==0&&q==0){break;}t++;for(int i=0;i<n;i++){for(int j=0;j<n;j++){if(i==j){maps[i][j]=0;d[i][j]=INF;}else{maps[i][j]=INF;d[i][j]=INF;}}}memset(book,0,sizeof(book));int x,y,z;for(int i=0;i<m;i++){scanf("%d%d%d",&x,&y,&z);if(maps[x][y]>z){//需要特别注意这一点!!!!maps[x][y]=z;}}printf("Case %d:\n",t);int f,a,b;for(int i=0;i<q;i++){scanf("%d",&f);if(f==0){scanf("%d",&a);if(book[a]==1){printf("City %d is already recaptured.\n",a);}else{book[a]=1;for(int j=0;j<n;j++){if(book[j]==1){if(d[a][j]>maps[a][j]){d[a][j]=maps[a][j];}if(d[j][a]>maps[j][a]){d[j][a]=maps[j][a];}}}for(int k=0;k<n;k++){for(int j=0;j<n;j++){d[a][k]=min(d[a][k],d[a][j]+d[j][k]);d[k][a]=min(d[k][a],d[k][j]+d[j][a]);}}for(int k=0;k<n;k++){for(int j=0;j<n;j++){d[a][k]=min(d[a][k],d[a][j]+d[j][k]);d[k][a]=min(d[k][a],d[k][j]+d[j][a]);d[k][j]=min(d[k][j],d[k][a]+d[a][j]);d[j][k]=min(d[j][k],d[j][a]+d[a][k]);}}/*wa了,因为忽略了经过a点更新最小路for(int k=0;k<n;k++){for(int j=0;j<n;j++){d[a][k]=min(d[a][k],d[a][j]+d[j][k]);d[k][a]=min(d[k][a],d[k][j]+d[j][a]);}}/*超时了for(int p=0;p<n;p++){for(int k=0;k<n;k++){for(int j=0;j<n;j++){d[k][j]=min(d[k][j],d[k][p]+d[p][j]);}}}*/}}else{scanf("%d%d",&a,&b);if(book[a]==0||book[b]==0){printf("City %d or %d is not available.\n",a,b);}else{if(d[a][b]!=INF){printf("%d\n",d[a][b]);}else{printf("No such path.\n");}}}}printf("\n");}return 0;
}/***************************************************
User name: dlili
Result: Accepted
Take time: 576ms
Take Memory: 936KB
Submit time: 2018-04-13 12:51:43
****************************************************/
/*
附上队友的代码:(更优更简洁)
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>using namespace std;const int maxn=310;int book[maxn];
int mapp[maxn][maxn];
const int inf=10000000;
int n,m,q;int main() {
    //ios::sync_with_stdio(false);int ca=0;while(scanf("%d%d%d",&n,&m,&q)) {if(n==0&&m==0&&q==0) {break;}int u,v,w;memset(book,0,sizeof(book));for(int i=0; i<n; i++) {for(int j=0; j<n; j++) {if(i==j)mapp[i][j]=0;else mapp[i][j]=inf;}}for(int i=0; i<m; i++) {scanf("%d%d%d",&u,&v,&w);if(w<mapp[u][v])mapp[u][v]=w;}ca++;printf("Case %d:\n",ca);int a,b,c;for(int i=0; i<q; i++) {scanf("%d",&a);if(a==0) {scanf("%d",&b);if(book[b]==0) {book[b]=1;for(int k=0; k<n; k++) {for(int j=0; j<n; j++) {if(mapp[k][j]>mapp[k][b]+mapp[b][j])mapp[k][j]=mapp[k][b]+mapp[b][j];// cout<<mapp[k][j]<<" ";}// cout<<endl;}}else {printf("City %d is already recaptured.\n",b);}}else {scanf("%d%d",&b,&c);if(book[b]==0||book[c]==0)printf("City %d or %d is not available.\n",b,c);else if(mapp[b][c]==inf)cout<<"No such path."<<endl;else {cout<<mapp[b][c]<<endl;}}}cout<<endl;}return 0;
}*/

2155 Problem Description(floyd算法变型)相关推荐

  1. 【Java数据结构与算法】第二十章 Dijkstra算法和Floyd算法

    第二十章 Dijkstra算法和Floyd算法 文章目录 第二十章 Dijkstra算法和Floyd算法 一.Dijkstra算法 1.介绍 2.代码实现 二.Floyd算法 1.介绍 2.代码实现 ...

  2. Floyd算法 Java实现

    Floyd算法 Java实现 算法导入 算法核心 代码实现 参考资料 结尾 算法导入 在上一篇博客中,咱讲述了求单源最短路径的一种经典算法 Dijkstra 算法,想了解的同学可以走前门瞅一瞅,记得回 ...

  3. hdu 2544最短路 Floyd算法

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Problem Descrip ...

  4. HDU2066 一个人的旅行【最短路径+Floyd算法】

    一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. 六度分离(floyd算法,SPFA算法,最短路—Dijkstra算法)

    Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) ...

  6. 弗洛伊德算法c语言path,Floyd算法(弗洛伊德算法)

    算法描述: Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按 ...

  7. HDU2112 HDU Today【Floyd算法】

    HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. 1128 信使(floyd算法)

    1. 问题描述: 战争时期,前线有 n 个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所.当指挥部下达一 ...

  9. 【POJ/算法】 3259 Wormholes(Bellman-Ford算法, SPFA ,FLoyd算法)

    Bellman-Ford算法 Bellman-Ford算法的优点是可以发现负圈,缺点是时间复杂度比Dijkstra算法高.而SPFA算法是使用队列优化的Bellman-Ford版本,其在时间复杂度和编 ...

最新文章

  1. IDEA中MAVEN项目打JAR包的简单方法
  2. cmd运行python服务器,python如何利用paramiko执行服务器命令
  3. 【Linux】15.mdc启动网卡并设置其ip和子网掩码
  4. 实验详解——parted单磁盘分区并进行配额
  5. Java编程,打印昨天的当前时刻
  6. 结对开发——一维数组最大子数组判断溢出
  7. [1] 图像预处理----图像灰度化处理
  8. 【power designer】使用power designer编辑pdm物理模型图时,为字段添加中文备注
  9. IBM x3850 x5U盘启动或光驱启动不起作用
  10. 关于电的计算机公式,电气设计相关计算公式大全
  11. 升级wireshark支持openflow13抓包
  12. 什么是和谐操作系统? 华为新操作系统介绍
  13. Qt开源作品16-通用无边框拖动拉伸
  14. CSS3 2D转换3D转换
  15. 思科、华为、华三模拟器大比拼,你最爱哪一款?(附模拟器下载)
  16. px和毫米的换算_js转换px与mm, cm
  17. 【大学物理·恒定电流的磁场】毕奥-萨伐尔定律
  18. 数学建模动态规划的小案例之R代码实现——生产计划问题
  19. 使用Spark读取并分析二进制文件
  20. 适应大众化教育的创客理念设计

热门文章

  1. 我在江北学安全(五) 渗透测试资源总览 和 XSS扫描系统原理 (续)
  2. Python 中__name__用法
  3. 存储过程 编译错误:PLS-00103: Encountered the symbol TABLE when expecting one of the following:
  4. IL、ST、LD、FBD、SFC分别是什么语言?
  5. 怎样量化评价搜索引擎的结果质量
  6. 字符串转运算表达式条件表达式
  7. vue mixin传参
  8. 最难游戏2计算机5关,史上最牛的游戏2攻略 史上最牛的游戏1~5关攻略
  9. 双线双IP(多线多IP)域名智能解析教程
  10. 胃总是涨 大便不成形 打嗝 口有异味?