Silver Cow Party(POJ-3268)
Problem 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
題意:給出 n 個點,m 條路,除 x 點外的其他點都要有一頭牛到達 x 點,求往返路程的最小值。
思路:先以所給邊為正向,x 為起點求最短路,再把邊反著讀一遍作為反向,以x為起點求最短路。把兩次求最短路的數(shù)組加起來,找最大值即可。
Source Program
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<set> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 1001 #define MOD 123 #define E 1e-6 using namespace std; int g[N][N]; int dis_positive[N],vis_positive[N];//各點到x int dis_negative[N],vis_negative[N];//x到各點 int main() {int n,m,x;scanf("%d%d%d",&n,&m,&x);memset(g,INF,sizeof(g));for(int i=1;i<=N;i++)for(int j=1;j<=N;j++)if(i==j)g[i][j]=0;while(m--){int x,y,w;scanf("%d%d%d",&x,&y,&w);if(g[x][y]>w)g[x][y]=w;}for(int i=1;i<=n;i++){/*各點到x*/dis_positive[i]=g[i][x];vis_positive[i]=0;/*x到各點*/dis_negative[i]=g[x][i];vis_negative[i]=0;}vis_positive[x]=1;vis_negative[x]=1;for(int i=1;i<n;i++)//各點到x{int k;int minn=INF;for(int j=1;j<=n;j++)if( !vis_positive[j] && dis_positive[j]<minn ){minn=dis_positive[j];k=j;}if(minn==INF)break;vis_positive[k]=1;for(int j=1;j<=n;j++)if( !vis_positive[j] && dis_positive[j]>dis_positive[k]+g[j][k] )dis_positive[j]=dis_positive[k]+g[j][k];}for(int i=1;i<n;i++)//x到各點{int k;int minn=INF;for(int j=1;j<=n;j++)if( !vis_negative[j] && dis_negative[j]<minn ){minn=dis_negative[j];k=j;}if(minn==INF)break;vis_negative[k]=1;for(int j=1; j<=n; j++)if( !vis_negative[j] && dis_negative[j]>dis_negative[k]+g[k][j] )dis_negative[j]=dis_negative[k]+g[k][j];}int maxx=-INF;for(int i=1;i<=n;i++)maxx=max(maxx,dis_negative[i]+dis_positive[i]);printf("%d\n",maxx);return 0; }?
總結(jié)
以上是生活随笔為你收集整理的Silver Cow Party(POJ-3268)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 理论基础 —— 排序 —— 计数排序
- 下一篇: 动态规划 —— 状压 DP