标签:ems div eof integer iostream body cst center tab
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions:28457 | Accepted: 12928 |
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
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<vector> 5 #include<queue> 6 using namespace std; 7 int n,m,x; 8 const int inf = 1e9; 9 struct node 10 { 11 int num; 12 int dis; 13 14 bool operator < (const node x)const 15 { 16 return x.dis<dis; 17 } 18 }; 19 vector<int>u[1024],w[1024]; 20 vector<int>ux[1024],wx[1024]; 21 int dis[1024],dis2[1024]; 22 bool book[1024]; 23 void dijkstra1() 24 { 25 fill(dis,dis+n+5,inf); 26 priority_queue<node>q; 27 memset(book,0,sizeof(book)); 28 q.push(node{x,0}); 29 dis[x]=0; 30 node exa; 31 while(!q.empty()){ 32 exa=q.top();q.pop(); 33 int t=exa.num; 34 if(book[t]){continue;} 35 book[t]=true; 36 int siz=u[exa.num].size(); 37 for(int i=0;i<siz;i++){ 38 if(!book[u[t][i]]&&dis[u[t][i]]>dis[t]+w[t][i]){ 39 dis[u[t][i]]=dis[t]+w[t][i]; 40 q.push(node{u[t][i],dis[u[t][i]]}); 41 } 42 } 43 } 44 } 45 46 void dijkstra2() 47 { 48 fill(dis2,dis2+n+5,inf); 49 priority_queue<node>q; 50 memset(book,0,sizeof(book)); 51 q.push(node{x,0}); 52 dis2[x]=0; 53 node exa; 54 while(!q.empty()){ 55 exa=q.top();q.pop(); 56 int t=exa.num; 57 if(book[t]){continue;} 58 book[t]=true; 59 int siz=ux[exa.num].size(); 60 for(int i=0;i<siz;i++){ 61 if(!book[ux[t][i]]&&dis2[ux[t][i]]>dis2[t]+wx[t][i]){ 62 dis2[ux[t][i]]=dis2[t]+wx[t][i]; 63 q.push(node{ux[t][i],dis2[ux[t][i]]}); 64 } 65 } 66 } 67 } 68 69 int main() 70 { 71 scanf("%d%d%d",&n,&m,&x); 72 int q,l,e; 73 for(int i=1;i<=m;i++){ 74 scanf("%d%d%d",&q,&l,&e); 75 u[q].push_back(l); 76 w[q].push_back(e); 77 ux[l].push_back(q); 78 wx[l].push_back(e); 79 } 80 int ans=0; 81 dijkstra1(); 82 dijkstra2(); 83 for(int i=1;i<=n;i++){ 84 ans=max(ans,dis[i]+dis2[i]); 85 } 86 printf("%d\n",ans); 87 }
POJ 3268 Silver Cow Party (Dijkstra)
标签:ems div eof integer iostream body cst center tab
原文地址:https://www.cnblogs.com/ZGQblogs/p/9392456.html