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 ...
随机推荐
- 修改tt模板让ADO.NET C# POCO Entity Generator With WCF Support 生成的实体类继承自定义基类
折腾几天记载一下,由于项目实际需要,从edmx生成的实体类能自动继承自定义的基类,这个基类不是从edmx文件中添加的Entityobject. 利用ADO.NET C# POCO Entity Gen ...
- response.setContentType()的作用及参数
package com.java1234.util; import java.io.PrintWriter; import javax.servlet.http.HttpServletResponse ...
- ajax请求aspx页面
首先,这么用是不好的.最好用ashx,但也难免遇到这种需求.开发过这么一个系统,每天访问量最多100,web服务器压力很小,完全大马拉小车,主要压力都在数据库服务器上,要做大量的统计.所以页面直接全上 ...
- 很棒的jQuery代码片段分享
jQuery实现的内链接平滑滚动 不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动 $('a[href^="#"]').bind('click.smoot ...
- Android 的 Relative Layout 常量
android:layout_above 将该控件的底部置于给定ID的控件之上 --Rule that aligns a child's bottom edge ...
- SVN global ignore pattern for c#
*.resharperoptions Web_Data log */[Bb]in [Bb]in */obj obj */TestResults TestResults *.svclog Debug ...
- 07_RHEL7配置yum源
redhat 默认自带的 yum 源需要注册才能更新.想不花钱也可以更新,就需要替换掉redhat的yum源. 检查是否安装yum包 查看RHEL是否安装了yum,若是安装了,那么又有哪些yum包: ...
- underscorejs-some学习
2.11 some 2.11.1 语法: _.some(list, predicate, [context]) 2.11.2 说明: 对list集合的每个成员根据predicate进行真值检测,如果一 ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- 封装好的PHP分页类,简单好用--在开源看到的,取回来自己用
class Pagination 独立分页类 调用方式: $pagenation = new Pagination( 4, 10, 200 ); // 4(第一个参数) = currentPage, ...