HDU-3631 Shortest Path (floyd)
Description
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
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
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 题目解析:每标记一个点就单独对这个点松弛。要注意有个 "There is a blank line between two consecutive test cases.” 代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<queue>
# include<algorithm>
const int INF=<<;
using namespace std;
int mp[][];
int n,m,q,mark[];
void floyd(int k)
{
for(int i=;i<n;++i)
for(int j=;j<n;++j)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
void work(int s,int t)
{
if(mark[s]==||mark[t]==)
printf("ERROR! At path %d to %d\n",s,t);
else{
if(mp[s][t]!=INF)
printf("%d\n",mp[s][t]);
else
printf("No such path\n");
}
}
int main()
{
//freopen("Qcin.txt","r",stdin);
int a,b,c,i,j,cas=;
while(scanf("%d%d%d",&n,&m,&q)==)
{
if(n==&&m==&&q==)
break;
if(cas)
printf("\n");
for(i=;i<n;++i)
for(j=;j<n;++j)
mp[i][j]=(i==j)?:INF;
memset(mark,,sizeof(mark));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=min(mp[a][b],c);
}
int comd;
printf("Case %d:\n",++cas);
while(q--)
{
scanf("%d",&comd);
if(comd==){
scanf("%d",&a);
if(mark[a]){
printf("ERROR! At point %d\n",a);
}else{
mark[a]=;
floyd(a);
}
}
else if(comd==){
scanf("%d%d",&a,&b);
work(a,b);
}
}
}
return ;
}
HDU-3631 Shortest Path (floyd)的更多相关文章
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU 5636 Shortest Path(Floyd)
题目链接 HDU5636 n个点,其中编号相邻的两个点之间都有一条长度为1的边,然后除此之外还有3条长度为1的边. m个询问,每次询问求两个点之前的最短路. 我们把这三条边的6个点两两算最短路, 然 ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- Shortest Path(hdu5636)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 3631 Shortest Path
floyd算法好像很奇妙的样子.可以做到每次加入一个点再以这个点为中间点去更新最短路,效率是n*n. #include<cstdio> #include<cstring> #i ...
- HDU ACM 1869 六度分离(Floyd)
六度分离 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Leetcode 943. Find the Shortest Superstring(DP)
题目来源:https://leetcode.com/problems/find-the-shortest-superstring/description/ 标记难度:Hard 提交次数:3/4 代码效 ...
- HDU 5938 Four Operations(四则运算)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
随机推荐
- Vue源码解析之数组变异
力有不逮的对象 众所周知,在 Vue 中,直接修改对象属性的值无法触发响应式.当你直接修改了对象属性的值,你会发现,只有数据改了,但是页面内容并没有改变. 这是什么原因? 原因在于: Vue 的响应式 ...
- 基于Spring Cloud的微服务落地
微服务架构模式的核心在于如何识别服务的边界,设计出合理的微服务.但如果要将微服务架构运用到生产项目上,并且能够发挥该架构模式的重要作用,则需要微服务框架的支持. 在Java生态圈,目前使用较多的微服务 ...
- 干货:Java并发编程系列之volatile(二)
接上一篇<Java并发编程系列之synchronized(一)>,这是第二篇,说的是关于并发编程的volatile元素. Java语言规范第三版中对volatile的定义如下:Java编程 ...
- ES6学习--箭头函数
1. 箭头函数基本形式 let func = (num) => num; let func = () => num; let sum = (num1,num2) => num1 + ...
- mysql的级联复制和多源复制
MySQL的复制:https://www.cnblogs.com/wxzhe/p/10051114.html 级联复制的结构如图 我们来设置基于filename和pos的级联复制,并且接受mysql- ...
- SP211 PRIMIT - Primitivus recurencis(欧拉回路)
SP211 PRIMIT - Primitivus recurencis 欧拉回路 Warning: enormous Input/Output data 警告:巨大的输入/输出 经过若干(11)次提 ...
- RabbitMQ-C 客户端接口使用说明
rabbitmq-c是一个用于C语言的,与AMQP server进行交互的client库.AMQP协议为版本0-9-1.rabbitmq-c与server进行交互前需要首先进行login操作,在操作后 ...
- STL与泛型编程(第一周)
part 1 C++模版简介 一,模版概观 1.模板 (Templates)是C++的一种特性,允许函数或类(对象)通过泛型(generic types)的形式表现或运行. 模板可以使得函数或类在对应 ...
- HTTP If-Modified-Since引发的浏览器缓存汇总
在看Spring中HttpServlet的Service方法时,对于GET请求,代码逻辑如下: if (method.equals(METHOD_GET)) { long lastModified = ...
- Effective TensorFlow Chapter 4: TensorFlow中的广播Broadcast机制【转】
本文转载自:https://blog.csdn.net/LoseInVain/article/details/78763303 TensorFlow支持广播机制(Broadcast),可以广播元素间操 ...