标签:des style http color os strong io for
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12633 | Accepted: 5631 |
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
Output
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
Source
#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstring> #include <stdlib.h> using namespace std; int mp[1005][1005]; int dis[1005]; bool vis[1005]; int dis2[1005]; const int INF=100000009; int n; void Dijstra(int v){ int minx,x; for(int i=0;i<=n;i++){ dis[i]=mp[v][i]; vis[i]=0; } vis[v]=1; dis[v]=0; for(int i=1;i<n;i++){ minx=INF; for(int j=1;j<=n;j++){ if(minx>dis[j]&&!vis[j]){ minx=dis[j]; x=j; } } if(minx==INF) break; vis[x]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&dis[x]+mp[x][j]<dis[j]) dis[j]=dis[x]+mp[x][j]; } } } void Dijstra2(int v){ int minx,x; for(int i=0;i<=n;i++){ dis2[i]=mp[i][v]; vis[i]=0; } vis[v]=1; dis2[v]=0; for(int i=1;i<n;i++){ minx=INF; for(int j=1;j<=n;j++){ if(minx>dis2[j]&&!vis[j]){ minx=dis2[j]; x=j; } } if(minx==INF) break; vis[x]=1; for(int j=1;j<=n;j++){ if(!vis[j]&&dis2[x]+mp[j][x]<dis2[j]) dis2[j]=dis2[x]+mp[j][x]; } } } int main(){ int m,x; scanf("%d%d%d",&n,&m,&x); memset(mp,INF,sizeof(mp)); for(int i=0;i<m;i++){ int s,e,t; scanf("%d%d%d",&s,&e,&t); if(mp[s][e]>t) mp[s][e]=t; } Dijstra(x); Dijstra2(x); int ans=-INF; for(int i=1;i<=n;i++){ if(i==x) continue; if(dis2[i]==INF||dis[i]==INF) continue; if(ans<dis[i]+dis2[i]) ans=dis2[i]+dis[i]; } printf("%d\n",ans); return 0; }
POJ 3268 Silver Cow Party,布布扣,bubuko.com
标签:des style http color os strong io for
原文地址:http://blog.csdn.net/kimi_r_17/article/details/38349151