# Silver Cow Party（最短路）

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19130 Accepted: 8734
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.
Source

USACO 2007 February Silver

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
int n,m,x;
int d[maxn][maxn];
void floyd_warshall(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
d[i][j]=min(d[i][j] , d[i][k] + d[k][j]);
return;
}
int main(){
scanf("%d%d%d",&n,&m,&x);
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
if(i==j) d[i][j]=0;
else d[i][j]=INF;
while(m--){
int x,y,val;
scanf("%d%d%d",&x,&y,&val);
d[x][y]=val;
}
floyd_warshall();
int res=-1;
for(int i=1;i<=n;i++)
res=max(res,d[i][x]+d[x][i]);
cout << res << endl;
return 0;
}

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

4 7 2
1 2 7
2 1 3
1 3 4
3 1 2
1 4 3
4 3 5
2 3 2

4 8 2
1 2 10
2 1 1
1 3 4
3 1 6
4 1 2
3 4 4
2 3 3
3 2 1
output
10
20
14

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
int n,m,x;
int cost[maxn][maxn];
struct edge{int from,to,cost;}E[1000010];
//从b点到e点的最短路径
int Bellman_ford(int b,int e){
int d[maxn];
for(int i=1;i<=n;i++) d[i]=INF;
d[b]=0;
while(true){
bool flag=false;
for(int i=0;i<m;i++){
edge t=E[i];
if(d[t.from]!=INF && d[t.to] > d[t.from] + t.cost){
d[t.to]=d[t.from] + t.cost;
flag=true;
}
}
if(!flag) break;
}
return d[e];
}
int main(){
scanf("%d%d%d",&n,&m,&x);
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++){
if(i==j) cost[i][j]=0;
else cost[i][j]=INF;
}
}
for(int i=0;i<m;i++)
scanf("%d%d%d",&E[i].from,&E[i].to,&E[i].cost);
int res=-1;
for(int i=1;i<=n;i++)
res=max(res,Bellman_ford(x,i) + Bellman_ford(i,x));
cout << res << endl;
return 0;
}