[kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party(最短路spfa+转置邻接矩阵)...
2019-07-18 17:43
567 查看
原文链接:http://www.cnblogs.com/Vampire6/p/11208903.html
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
D - Silver Cow Party(双向边)
题目链接:https://vjudge.net/contest/66569#problem/D
题目:
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 XLines 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 3Sample Output
10Hint 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. 题意:有1~N个农场和每个农场的奶牛,每个奶牛需要从它当前的农场到农场编号为X的农场, 到达目的的农场后需要再回到原来各自编号的农场,由于去和回来的权值不同, 所以这时候需要转置邻接矩阵,我们可以换一下思路: 以X农场为起始点,这样一个spfa下来就可以算从X出发到各个农场的最小权值和, 然后再转置road[i][j],就可以将边反向, 再来一个spfa就可以算去的状态了,代码如下: spfa算法比较快,用了94ms就AC辽
// // Created by hanyu on 2019/7/18. // #include<iostream> #include<queue> #include<cstring> #include<cstdio> #define MAX 0x3f3f3f3f using namespace std; const int maxn=1005; int dgo[maxn],dback[maxn],road[maxn][maxn]; bool book[maxn]; int N,M,X; void spfa(int *d) { memset(book,false,sizeof(book)); for(int i=1;i<=maxn;i++) d[i]=MAX; queue<int>qu; qu.push(X); book[X]= true; d[X]=0; while(!qu.empty()) { int now=qu.front(); qu.pop(); book[now]=false; for(int i=1;i<=N;i++) { if(d[i]>d[now]+road[now][i]) { d[i]=d[now]+road[now][i]; if(!book[i]) { qu.push(i); book[i]=true; } } } } } int main() { scanf("%d%d%d",&N,&M,&X); for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j) road[i][j]=0; else road[i][j]=MAX; } } memset(dgo,MAX,sizeof(dgo)); memset(dback,MAX,sizeof(dback)); int x,y,z; for(int i=1;i<=M;i++) { scanf("%d%d%d",&x,&y,&z); road[x][y]=z; } spfa(dgo); for(int i=1;i<=N;i++) for(int j=1;j<=i;j++) swap(road[i][j],road[j][i]);//转置邻接矩阵 int maxx=-1; spfa(dback); for(int i=1;i<=N;i++) { maxx=max(maxx,dgo[i]+dback[i]);//X农场到其他每个农场的最小权值+其他农场到X农场的最小权值 } printf("%d\n",maxx); return 0; }
转载于:https://www.cnblogs.com/Vampire6/p/11208903.html
相关文章推荐
- 【算法系列学习】Dijkstra求最短路 [kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party
- poj 3268 Silver Cow Party(最短路矩阵转置~)
- kuangbin专题最短路 D - Silver Cow Party
- poj 3268 Silver Cow Party ([kuangbin带你飞]专题四 最短路练习)
- Silver Cow Party(最短路)
- poj 3268 Silver Cow Party 最短路/dijkstra
- POJ Silver Cow Party (最短路+反向建图)
- poj 3268 Silver Cow Party(最短路)
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- Poj 3268 Silver Cow Party (最短路)
- poj3268 Silver Cow Party(最短路变形)
- POJ-3268 Silver Cow Party[Dijkstra] [邻接矩阵] [置换矩阵]
- [kuangbin带你飞]专题四 最短路练习 B( POJ 2253) Frogger(spfa)
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party (最短路,置换矩阵)
- poj-3268 Silver Cow Party (最短路-spfa-求到同一起点,同一终点的最短路)
- [kuangbin带你飞]专题四 最短路练习
- poj 1502 MPI Maelstrom([kuangbin带你飞]专题四 最短路练习)
- POJ 3268-Silver Cow Party【正向+反向最短路】