A

来源:互联网 发布:网络市场监管 编辑:程序博客网 时间:2024/06/13 04:01

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 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi 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 farmAi 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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.


嗯其中我觉得

int cnt;for(i=1;i<=n;i++)   for(j=i+1;j<=n;j++) {cnt=edge[i][j];edge[i][j]=edge[j][i];edge[j][i]=cnt;     }

转换矩阵的时候这边要注意一下 就是

j=i+1如果是j=1的话 其实相当于转换了两次 还是一样的根本没有变化就是这个样子的然后一个是目标点到每个地方的距离 也就是单源到多源 还有一个是每个地方到目标点的的距离  也就是多源到单源 这个地方需要注意一下 进行矩阵转换才可以 

c++代码:

#include<iostream>#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define inf 0x3f3f3f3fint n,m,x;int edge[1050][1050];int road[1050];int dis1[1050];int dis2[1050];int book[1050]; bool cmp(int a,int b){    return a>b;}int main(){scanf("%d%d%d",&n,&m,&x);int i,j,k;int a,b,c;for(i=1;i<=n;i++)   for(j=1;j<=n;j++)      if(i==j)      edge[i][j]=0;      else       edge[i][j]=inf;//初始化   for(i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&c);    if(edge[a][b]>c)       edge[a][b]=c;}for(i=1;i<=n;i++){ dis1[i]=edge[x][i];}memset(book,0,sizeof(book));    book[x]=1;    int u,v;for(i=1;i<=n;i++){int minn=inf;for(j=1;j<=n;j++){if(book[j]==0&&dis1[j]<minn){minn=dis1[j];u=j;}}book[u]=1;for(j=1;j<=n;j++)        {           if(book[j]==0&&dis1[j]>dis1[u]+edge[u][j])           {           dis1[j]=dis1[u]+edge[u][j];           }       }}int cnt;for(i=1;i<=n;i++)   for(j=i+1;j<=n;j++) {cnt=edge[i][j];edge[i][j]=edge[j][i];edge[j][i]=cnt;     }     for(i=1;i<=n;i++) dis2[i]=edge[x][i];memset(book,0,sizeof(book));    book[x]=1;for(i=1;i<=n;i++){int minn=inf;for(j=1;j<=n;j++){if(book[j]==0&&dis2[j]<minn){minn=dis2[j];u=j;}}book[u]=1;for(j=1;j<=n;j++)        {           if(book[j]==0&&dis2[j]>dis2[u]+edge[u][j])           {           dis2[j]=dis2[u]+edge[u][j];           }       }}    for(i=1;i<=n;i++)road[i]=dis1[i]+dis2[i];sort(road+1,road+1+n,cmp);    printf("%d\n",road[1]);return 0;}