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 ...
随机推荐
- 4 个技巧学习 Golang
到达 Golang 大陆:一位资深开发者之旅. 2014 年夏天…… IBM:“我们需要你弄清楚这个 Docker.” 我:“没问题.” IBM:“那就开始吧.” 我:“好的.”(内心声音):”Doc ...
- python之路----面向对象进阶二
item系列 __getitem__\__setitem__\__delitem__ class Foo: def __init__(self,name,age,sex): self.name = n ...
- SQLServer 进阶记录式学习
1.强制类型转换 nvarchar->decimal ) , , ) SET @i = '1083.589' SET @num = @i SELECT @num , )) SELECT @nu ...
- 异常和TCP通讯
第七章 异常处理 * 异常处理机制中的try-catch * 语法: * try{ * 代码片段 * }catch(XXXException e){ * 当try中的代码片段出现了XXXExcepti ...
- SNMP学习笔记之iReasoning MIB Browser
0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...
- Python3 获取网络图片并且保存到本地
Python3 获取网络图片并且保存到本地 import requests from bs4 import BeautifulSoup from urllib import request impor ...
- 20145324王嘉澜《网络对抗技术》Web基础
实践要求 ①Web前端HTML: 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML ②Web前端javascipt: 理解JavaScript ...
- python函数总结
1.函数是一种子程序.程序员使用函数来减少代码重复,并用于组织或模块化程序.一旦定义了函数,它可以从程序中的许多不同位置被多次调用.参数允许函数具有可更改的部分.函数定义中出现的参数称之为形参,函数调 ...
- CF 316E3 Summer Homework(斐波那契矩阵+线段树)
题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...
- 51nod 1021 石子归并 区间DP
1021 石子归并 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 N堆石子摆成一条线.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆 ...