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?
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.
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
// // 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; }