上题:
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 3
10
map[2][1]=30 map[2][2]=0 map[2][3]=10 map[2][4]=999999
map[3][1]=999999 map[3][2]=999999 map[3][3]=0 map[3][4]=3
map[4][1]=15 map[4][2]=999999 map[4][3]=999999 map[4][4]=0
dis[1]=0
dis[2]=20
dis[3]=30
dis[4]=33
-----------
map[1][1]=0 map[1][2]=30 map[1][3]=999999 map[1][4]=15
map[2][1]=20 map[2][2]=0 map[2][3]=999999 map[2][4]=999999
map[3][1]=999999 map[3][2]=10 map[3][3]=0 map[3][4]=999999
map[4][1]=999999 map[4][2]=999999 map[4][3]=3 map[4][4]=0
---dis2[1]=0--
---dis2[2]=28--
---dis2[3]=18--
---dis2[4]=15--
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 999999
int map[1005][1005],map2[1005][1005];
int dis1[1002],vis[1002],dis2[1002];
int n,m,x;
void dikjstar()
{
for(int i=1;i<=n;i++)
{
dis1[i]=map[x][i];
}
vis[x]=1;
int min=INF,u;
for(int i=1;i<=n-1;i++)
{
min=INF;
for(int j=1;j<=n;j++)
{
if(dis1[j]<min&&!vis[j])
{
min=dis1[j];
u=j;
}
}
vis[u]=1;
for(int v=1;v<=n;v++)
{
if(dis1[v]>dis1[u]+map[u][v])
{
dis1[v]=dis1[u]+map[u][v];
}
}
}
}
void dikjstar2()
{
for(int i=1;i<=n;i++)
{
dis2[i]=map2[x][i];
}
vis[x]=1;
int min=INF,u;
for(int i=1;i<=n-1;i++)
{
min=INF;
for(int j=1;j<=n;j++)
{
if(dis2[j]<min&&!vis[j])
{
min=dis2[j];
u=j;
}
}
vis[u]=1;
for(int v=1;v<=n;v++)
{
if(!vis[v]&&dis2[v]>dis2[u]+map2[u][v])
{
dis2[v]=dis2[u]+map2[u][v];
}
}
}
}
void tran()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
map2[i][j]=map[j][i];
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
{
map[i][j]=0;
}
else
{
map[i][j]=INF;
}
}
}
int a,b,c;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
{
map[a][b]=c;
}
}
dikjstar();
/* for(int i=1;i<=n;i++)
{
printf("---dis1[%d]=%d--\n",i,dis1[i]);
}*/
tran();
dikjstar2();
/*for(int i=1;i<=n;i++)
{
printf("---dis2[%d]=%d--\n",i,dis2[i]);
}*/
int sum=0;
for(int i=1;i<=n;i++)
{
//printf("sum=%d dis1[%d]+dis2[%d]=%d\n",sum,dis1[i],s)
if(sum<dis1[i]+dis2[i])
{
sum=dis1[i]+dis2[i];
}
}
printf("%d\n",sum);
}
}