HDU3631:Shortest Path(Floyd)
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?
End of input is indicated by a line containing N = M = Q = 0.
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.
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
ERROR! At point 4
ERROR! At point 1
0
0
ERROR! At point 3
ERROR! At point 4
#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)的更多相关文章
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- [ZOJ2760]How Many Shortest Path(floyd+最大流)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...
- [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 ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- hdu 2807 The Shortest Path(矩阵+floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 74(2B)Shortest Path (hdu 5636) (Floyd)
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 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 ...
随机推荐
- jquery.placeholder.js的使用
最近做东西用到placeholder这个属性,可是这个属性在低版本的IE或者QQ浏览器等这些浏览器上这个属性不能生效,后来在网上查了下,发现了jquery的一个插件jquery.placeholder ...
- Message,MessageQueue,Looper,Handler ——由view.post(runnable想到的)
近日看到代码有view.post(runable),发现对handler机制又有些模糊,故做些复习. 这里就不再对具体的源码原理做深入复习了,就抄一些基本的结论吧. 1.基本概念 Message:基本 ...
- centos 给鼠标右击添加 “打开终端” 菜单项
1.以root身份在终端执行如下命令 yum -y install nautilus-open-terminal 2.重启操作系统 shutdown -r now
- 【Usaco2008 Mar】土地购买
[题目描述] 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; ...
- When Colon Scripting is comming(JavaScript语法扩充)
当冒号脚本来临-- JavaScript语法扩充 连续好几夜的不能安眠,得出结论就是,未来语言未来的编译器应该是支持语法定制规则和语法扩展的,这样使得编程语言不断进化以更利于人类使用!of cours ...
- Jquery Mobile学习
<!doctype html> <html lang="zh-hans"> <head> <meta charset="UTF- ...
- Excel报表
Excel报表 1.Excel报表导入到GridView protected void Page_Load(object sender, EventArgs e) { string path = Se ...
- ucenter 通信原理
1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证. 2.如果验证成功,将调用位 ...
- 个人Python常用Package及其安装
为了避免每次重装系统时又要东翻西找,现在此记录一下目前常用的Python包安装过程. 1) Python: 2.7.11, 下载地址:www.python.org.由于个人喜欢使用PyQt4(其实是不 ...
- 2-16 HDO1106
这题寒假也没搞出来,但今天花了一小时终于搞定. 题意是输入一串数字字符,把‘5’当作空格,然后把被分割开的数字进行排序输出. 首先是字符串输入,按照高精度的处理方法,数值低位放到数组低位.(字符串型的 ...