上题:
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?
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.
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.
Line 1: One integer: the maximum of time any one cow must walk.
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
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.
题意是 :
现在有一张图,n个顶点和m条边,你要从每个点到x,之后再从x点到原来的点,求出最短路,这也是一道比较经典的题,我们可以很容易的求出x点到各原点的距离(迪杰斯特拉,spfa都可以),但是各原点到x点的最短距离是不容易求得的(路是单向的),那我们应该怎样求得呢?很简单(其实我是特么看了题解,然后又画了个图,想好久才想出来的),那就是我们反向构边之后再跑一遍最短路就是x到各原点的距离,下面给出一个例子吧就比如现在有4个点,5条边
1->2 20
2->1 30
2 ->3 10
3 ->4 3
4->1 15
最后出来就是这个样子了 ,如果真的不懂可以照着这个样例画一下图就ok了
map[1][1]=0 map[1][2]=20 map[1][3]=999999 map[1][4]=999999
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--
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);
}
}