POJ3268Silver Cow Party~Dij算法+矩阵倒置

    技术2022-05-18  18

    Silver Cow Party Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 5052 Accepted: 2151

    Description

    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?

    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 10 time units. ----------------------------------------------------------------------------------------------------------------------------------- 第一次Dijkstra()生成由终点到其他起点所需时间,调用Reversal()将矩阵倒置,在执行一次Dijkstra()反向生成最短路径,相当于计算由其它起点到终点所需时间。下面是菜鸟本人的代码:(期中sum[N]是用来储存由起点到终点来回2次的总时间) #include <stdlib.h> #include <string.h> #include <stdio.h> #define N 1050 #define MaxInt 0x3f3f3f3f int map[N][N],visited[N],dis[N],sum[N]; int n,m,x; void Dijkstra() { int i,j,position,min; memset(visited,0,sizeof(visited)); for(i=1;i<=n;i++) dis[i]=map[x][i]; visited[x]=1; for(i=1;i<n;i++) { min=MaxInt; for(j=1;j<=n;j++) { if(visited[j]==0&&min>dis[j]) { min=dis[j]; position=j; } } visited[position]=1; sum[position]+=min; for(j=1;j<=n;j++) if(visited[j]==0&&dis[position]+map[position][j]<dis[j]) dis[j]=dis[position]+map[position][j]; } } void Reversal() { int i,j,temp; for(i=1;i<=n;i++) for(j=1;j<i;j++) { temp=map[i][j]; map[i][j]=map[j][i]; map[j][i]=temp; } } int main() { int i,u,v,c,max=0; memset(map,MaxInt,sizeof(map)); scanf("%d%d%d",&n,&m,&x); for(i=1;i<=n;i++) map[i][i]=0; while(m--) { scanf("%d%d%d",&u,&v,&c); if(c<map[u][v]) map[u][v]=c; } Dijkstra(); Reversal(); Dijkstra(); for(i=1;i<=n;i++) if(max<sum[i]) max=sum[i]; printf("%d/n",max); system("pause"); return 0; }

    最新回复(0)