Problem Description
When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in Informatics) in GD team. One day, GD team’s coach, Prof. GUO asked them to solve the following shortest-path problem.
There is a weighted directed multigraph G. And there are following two operations for the weighted directed multigraph:

(1) Mark a vertex in the graph.

(2) Find the shortest-path between two vertices only through marked vertices.

For it was the first time that LMY faced such a problem, she was very nervous. At this moment, YY decided to help LMY to analyze the shortest-path problem. With the help of YY, LMY solved the problem at once, admiring YY very much. Since then, when LMY meets problems, she always calls YY to analyze the problems for her. Of course, YY is very glad to help LMY. Finally, it is known to us all, YY and LMY become programming lovers.

Could you also solve the shortest-path problem?

 
Input
The input consists of multiple test cases. For each test case, the first line contains three integers N, M and Q, where N is the number of vertices in the given graph, N≤300; M is the number of arcs, M≤100000; and Q is the number of operations, Q ≤100000. All vertices are number as 0, 1, 2, … , N - 1, respectively. Initially all vertices are unmarked. Each of the next M lines describes an arc by three integers (x, y, c): initial vertex (x), terminal vertex (y), and the weight of the arc (c). (c > 0) Then each of the next Q lines describes an operation, where operation “0 x” represents that vertex x is marked, and operation “1 x y” finds the length of shortest-path between x and y only through marked vertices. There is a blank line between two consecutive test cases.

End of input is indicated by a line containing N = M = Q = 0.

 
Output
Start each test case with "Case #:" on a single line, where # is the case number starting from 1.

For operation “0 x”, if vertex x has been marked, output “ERROR! At point x”.

For operation “1 x y”, if vertex x or vertex y isn’t marked, output “ERROR! At path x to y”; if y isn’t reachable from x through marked vertices, output “No such path”; otherwise output the length of the shortest-path. The format is showed as sample output.

There is a blank line between two consecutive test cases.

 
Sample Input
5 10 10
1 2 6335
0 4 5725
3 3 6963
4 0 8146
1 2 9962
1 0 1943
2 1 2392
4 2 154
2 2 7422
1 3 9896
0 1
0 3
0 2
0 4
0 4
0 1
1 3 3
1 1 1
0 3
0 4
0 0 0
 
Sample Output
Case 1:
ERROR! At point 4
ERROR! At point 1
0
0
ERROR! At point 3
ERROR! At point 4
 
这道题要用floyd过的话关键就看对于floyd的理解了,因为只有标记的点可以走,为了节省时间,我们可以再新标记点的时候以那点为中转点进行一次floyd,这就避免了n^3的复杂度
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int inf = 999999999; int n,m,k;
int map[305][305];
int hash[305]; void floyd(int k)
{
int i,j;
for(i = 0; i<n; i++)
for(j = 0; j<n; j++)
if(map[i][j]>map[i][k]+map[k][j])
map[i][j] = map[i][k]+map[k][j];
} int main()
{
int x,y,c,i,j,cas = 1;
while(~scanf("%d%d%d",&n,&m,&k),n+m+k)
{
memset(hash,0,sizeof(hash));
for(i = 0; i<=n; i++)
{
for(j = 0; j<=n; j++)
map[i][j] = inf;
map[i][i] = 0;
}
while(m--)
{
scanf("%d%d%d",&x,&y,&c);
if(c<map[x][y])
map[x][y] = c;
}
if(cas!=1)
printf("\n");
printf("Case %d:\n",cas++);
while(k--)
{
scanf("%d",&c);
if(c)
{
scanf("%d%d",&x,&y);
if(hash[x] && hash[y])
{
if(map[x][y]!=inf)
printf("%d\n",map[x][y]);
else
printf("No such path\n");
}
else
printf("ERROR! At path %d to %d\n",x,y);
}
else
{
scanf("%d",&x);
if(hash[x])
printf("ERROR! At point %d\n",x);
else
{
hash[x] = 1;
floyd(x);//以新加入的点为中转点去更新最短路
}
}
}
} return 0;
}

HDU3631:Shortest Path(Floyd)的更多相关文章

  1. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  2. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  3. [LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

  4. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  6. 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...

  7. hdu 2807 The Shortest Path(矩阵+floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. 74(2B)Shortest Path (hdu 5636) (Floyd)

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. 得于吾师傅的js知识 js类,单写模板,和私有保护的方法

    js的类的写法: 1,写法一:function内部包含this.function()如代码: var origin_class = function(name) { var lover = ''; t ...

  2. (转)DEDECMS模板原理、模板标签学习 - .Little Hann

    本文,小瀚想和大家一起来学习一下DEDECMS中目前所使用的模板技术的原理: 什么是编译式模板.解释式模板,它们的区别是什么? 模板标签有哪些种类,它们的区别是什么,都应用在哪些场景? 学习模板的机制 ...

  3. java中的IO二

    java中流分为节点流和处理流,IO一中的案例都是节点流 一.处理流的使用实例 二.装饰者模式 以上BufferReader的用法就是装饰者模式 Decorator就是动态地给对象增添行为 如果要实现 ...

  4. FckEditor组件的使用(新闻浏览发布页面)

    FckEditor组件的使用 准备工作: 1)将FckEditor目录拷贝到网站根目录内. 2)在Web.config的AppSetting分支中添加如下的代码,并修改FckEditor的目录和上传文 ...

  5. [转]Delphi导出Excel的设置操作

    procedure CreatRepSheet(SheetName:String;PageSize,PageLay:Integer); {新建Excel工作簿.进行页面设置} begin {新建Exc ...

  6. 虾米loop 摇头代码

    .head{ position: absolute; -webkit-animation: shake 1s infinite; -webkit-transform-origin: center bo ...

  7. Python md5 sha1 的使用

    版本: Python 2.7 说明: Python 内置的 hashlib 模块中有 md5 和 sha1 加密方法,可以直接使用. md5加密 import hashlib data = 'This ...

  8. js监控键盘大小写事件

    JavaScript键盘事件侦听    在使用JavaScript做WEB键盘事件侦听捕获时,主要采用onkeypress.onkeydown.onkeyup三个事件进行出来.该三个事 件的执行顺序如 ...

  9. CSS3圆角详解

    一.CSS3圆角的优点 传统的圆角生成方案,必须使用多张图片作为背景图案.CSS3的出现,使得我们再也不必浪费时间去制作这些图片了,而且还有其他多个优点: * 减少维护的工作量.图片文件的生成.更新. ...

  10. python——BS解析器