c语言sscanf函数的用法是什么
271
2022-08-31
poj 3268 Silver Cow Party(单源最短路径Dijkstra·最小环)
题目:Cow Party
Time Limit: 2000MS | | Memory Limit: 65536KB | | 64bit IO Format: %I64d & %I64u |
Submit Status
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Line 1: Three space-separated integers, respectively: N, M, and X Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
终点给出,是一个定点。所有的牛犊都会到达该点并且由通过不同的道路回到原点。由此想到单源最短路径,把终点看作源点S,但是怎么回去呢?看了一个大神的博客,矩阵转置(第一次自己转置都写错了||-_-)。效果:(1,2) (2,7) (7,4) --> (2,1) (7,2) (4,7) means (4,7) (7,2) (2,1) 起点和终点的地位互换!这样再一次用Dijkstra或者spfa等算法求出回家的最短路径就构成了环路。
关于矩阵转置举个例子:
for(int i=0;i<4;i++){
for(int j=0;j<=i;j++){
swap(a.m[i][j],a.m[j][i]);
}
}
不是:
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
swap(a.m[i][j],a.m[j][i]);
}
}
来看看效果:
#include 0 0: show: 1 2 3 4 7 3 5 5 6 6 6 6 0 0 0 0 1 0: show: 1 7 3 4 2 3 5 5 6 6 6 6 0 0 0 0 1 1: show: 1 7 3 4 2 3 5 5 6 6 6 6 0 0 0 0 2 0: show: 1 7 6 4 2 3 5 5 3 6 6 6 0 0 0 0 2 1: show: 1 7 6 4 2 3 6 5 3 5 6 6 0 0 0 0 2 2: show: 1 7 6 4 2 3 6 5 3 5 6 6 0 0 0 0 3 0: show: 1 7 6 0 2 3 6 5 3 5 6 6 4 0 0 0 3 1: show: 1 7 6 0 2 3 6 0 3 5 6 6 4 5 0 0 3 2: show: 1 7 6 0 2 3 6 0 3 5 6 0 4 5 6 0 3 3: show: 1 7 6 0 2 3 6 0 3 5 6 0 4 5 6 0 是的,就是这样。 Dijkstra:基于贪心的思想。不足之处在于不能有负边,用于求解单源最短路径问题。这里没有负边问题,所以我就用它吧。 #include
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~