Silver Cow Party
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 15989 |
|
Accepted: 7303 |
Description
One cow from each of N farms (1≤n≤1000) conveniently numbered 1..N are going to attend the big Cow party to being held at Farm #X (1≤x≤n). A total of M (1≤m≤100,000) unidirectional (one-way roads connects pairs of farms; road Irequires Ti (1≤ti≤100) Uni TS of time to traverse.
Each of the cow must walk to the "party" and "when the" is "over" return to her farm. Each cow is a lazy and thus picks an optimal route with the shortest time. A Cow ' s return route might is different from her original route to the party since roads is one-way.
Of all the cows, what's 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 ten time units.
The minimum value of the target point to the other points in the graph.
Dijkstra directly to the distance, and then the comparison to find the minimum value.
Code:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string
> #include <cstring> #pragma warning (disable:4996) using namespace std;
const int MAX = 100005;
int edge[1005][1005];
int vist[1005],vist2[1005],minidis1[1005][1005],minidis2[1005][1005];
int n,m,x;
void init () {int i,j;
for (i=1;i<=n;i++) {for (j=1;j<=n;j++) {if (j==i) {edge[i][j]=0;
minidis1[i][j]=0;
minidis2[i][j]=0;
} else {edge[i][j]=-1;
Minidis1[i][j]=max;
Minidis2[i][j]=max;
}}} for (i=1;i<=n;i++) {vist[i]=0;
vist2[i]=0;
}} void Dijkstra (int i) {int j,k;
int position=i;
int position2=i;
Vist[position]=1;
Vist2[position]=1;
minidis1[i][position]=0;
minidis2[position][i]=0; for (j=1;j<=n-1;j++)//Add a total of num-1 points {for (k=1;k<=n;k++) {if (vist[k]==0 && edge[position][k]!=-1 & ;& Minidis1[i][position]+edge[position][k] < minidis1[i][k])//newly filled point updateMinidis {minidis1[i][k]=minidis1[i][position]+edge[position][k]; } if (vist2[k]==0 && edge[k][position2]!=-1 && Minidis2[position2][i]+edge[k][position2] < Minidis2 K [i])
Newly populated point update Minidis {Minidis2[k][i]=minidis2[position2][i]+edge[k][position2];
}} int min_value=max,min_pos=0;
int min_value2=max,min_pos2=0; for (k=1;k<=n;k++) {if (vist[k]==0 && minidis1[i][k]<min_value)//Compare the smallest one as the new add-in store {min_value = m
INIDIS1[I][K];
Min_pos = k;
} if (vist2[k]==0 && minidis2[k][i]<min_value2)//Compare the smallest one as the newly added store {min_value2 = Minidis2[k][i];
Min_pos2 = k;
}} vist[min_pos]=1;
Position=min_pos;
Vist2[min_pos2]=1;
Position2=min_pos2;
}} int main () {int i;
cin>>n>>m>>x;
Init ();
int Temp1,temp2,temp3;
for (i=1;i<=m;i++) {cin>>temp1>>temp2>>temp3;
Edge[temp1][temp2]=temp3;
} memset (Vist,0,sizeof (vist)); memset (Vist2,0,sizeof (viST2));
Dijkstra (X);
int ans=-1;
for (i=1;i<=n;i++) {if (i==x) continue;
Ans=max (Ans,minidis1[x][i]+minidis2[i][x]);
} cout<<ans<<endl;
return 0; }