Silver Cow Party Time limit:4000/2000ms (java/other) Memory limit:131072/65536k (java/other) Total Submission (s): accepted submission (s): Problem Description
One cow from each of N farms (1≤n≤1000) conveniently numbered 1..N are going to attend the "Big cow" 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) UN Its in time to traverse.
Each of the cow must walk to the "party" and "the", "when" is over, return to her farm. Each cow is lazy and thus picks a optimal route with the shortest time. A Cow ' s return route might is different from her original route to the party since roads are one-way.
Of all the cows, what are the longest amount of time a cow must spend 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 the 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
Source PKU The main effect of the topic:
There are a group of cows (n only) parties, to a certain cattle (x) home, known to have M road, and each road is one-way
Find the way the cow walks the longest in all the cows
Knowledge Points:
1) The Dijstra algorithm is used to compute the distance from the source point to the remaining points
And this topic requires
(1) The distance from point X to all remaining points (can be used Dijstra algorithm)
(2) The distance of all the other points to the X point (cannot be directly obtained, need to convert thinking)
The best way is to find the transpose matrix, and then to the transpose matrix for Dijstra (), then out of all the number point to the X point distance
2 See this problem, then think of Floyd (), and then timed out, according to others, cautious use of Floyd () algorithm
#include <stdio.h> #include <string.h>
#define MAXN 1005
#define INF 0x3f3f3f3f
int EDGE[MAXN][MAXN],DIS[MAXN],VIS[MAXN],DIS2[MAXN];
int n,m,x;
void Dijstra ()
{
int I,j,k,mi;
memset (vis,0,sizeof Vis);
memset (dis,0x3f,sizeof Dis);
dis[x]=0;
for (i=1;i<=n;i++)//n times
{
Mi=inf;
for (j=1;j<=n;j++)
{
if (Dis[j]<mi && vis[j]==0)
Mi=dis[j],k=j;
}
Vis[k]=1;
for (j=1;j<=n;j++)
{
if (Dis[j]>dis[k]+edge[k][j])
DIS[J]=DIS[K]+EDGE[K][J];
}
}
}
int main ()
{
int u,v,w,i,j,s,t;
while (scanf ("%d%d%d", &n,&m,&x)!=eof)
{
int max=0;
memset (edge,0x3f,sizeof Edge);
for (i=1;i<=m;i++)
{
scanf ("%d%d%d", &u,&v,&w);
Edge[u][v]=w;
}
for (i=1;i<=n;i++)
edge[i][i]=0;
Dijstra ()//Find the distance from X point to the other point
for (i=1;i<=n;i++)
Dis2[i]=dis[i];
The new transpose matrix is calculated to calculate the distance from the point to the X point.
for (i=1;i<=n;i++)
{
for (j=i;j<=n;j++)
{
T=edge[j][i];
EDGE[J][I]=EDGE[I][J];
edge[i][j]=t;
}
}
Dijstra ();
for (i=1;i<=n;i++)
{
if (Dis[i]!=inf && dis2[i]!=inf)
S=dis[i]+dis2[i];
if (max<s)
Max=s;
}
printf ("%d\n", Max);
}
return 0;
}