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 ...
随机推荐
- 2015 Multi-University Training Contest 1 题解 BY FZUw
题目链接:5288-5299 HDU5288 题解原文链接:我是链接
- phpmyadmin安装出错,缺少 mysqli 扩展。请检查 PHP 配置
下载了个phpmyadmin最新版本的,始终显示这样的内容,求助.如何解决哈?>缺少 mysqli 扩展.请检查 PHP 配置. <a href="Documentation.h ...
- oracle解析xml完成版第二次修改
其实XML字符串就好像是ORACLE中的外部表,因此Oracle对 解析XML字符串一些规则要求非常严格.XML字符串提供的数据就是一张表,所以Oracle必须提供跟 xml数据一致的列头 示例一 S ...
- Swift - 29 - 参数的默认值
// 参数设置了默认值之后, 在调用的时候, 可以写这个参数 // 在参数前面添加"_", 表示取消外部参数名, 但还是建议使用苹果默认格式 func sayHello(nickN ...
- python下 help()使用方法
查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules: help("modules ...
- 在VC6中使用ogre进行游戏开发
微软公司开发的visual c++6.0堪称史上最易用.最成熟的开发工具.vc6以其小巧.轻便赢得了程序员的喜爱,以至于在VS大行其道的时代,很多程序员仍然使用vc6作为开发工具,vc6的欢迎性可见一 ...
- C++多重继承虚表的内存分布
接前面虚表的内存分布,今天重点看多重继承的虚表内存分布,简单的说,继承几个类便有几个虚表,如下代码 class Drive : public Base1, public Base2, public B ...
- SCXML和QScxml使用总结
最近接触了SCXML这个状态描述文本,简单来讲就是描述了整个状态的变迁过程的一种XML格式的表格.Qt labs中有一个项目就是QScxml,它基于QStateMachine上层制作,可以直接读取SC ...
- 关于泥水佬的minihttp与MVC4的对比
相同的功能: 在Action里面实现调用Redis服务端的时间戳,然后转成“yyyy-MM-dd HH:mm:ss”格式的字符串,显示在界面上,显示结果是一样的: 下面分别贴上用ab压的结果: 先是M ...
- 【Android实战开发】3G技术和Android发展简介
随着移动设备的不断普及和发展,相关软件的开发也越来越受到人们的关注,其中要提及的就是Android开发.本系列博客主要为大家介绍Android的开发,可能会有人问:现在互联网上已经有很多的Androi ...