码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3268 Silver Cow Party

时间:2017-02-02 17:25:37      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:init   为什么   blog   mit   nec   clu   direct   stream   poj   

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20274   Accepted: 9278

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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.

Source

USACO 2007 February Silver

翻译时间:来看一哈我的英文水平张进点没有。  题目大意:

块田中(1≤N≤1000)都有1只牛去参加盛大的母牛聚会,这个聚会被安排在X号田(1≤X ≤N)。一共有M( 1≤M≤100,000)条单行道分别连接着两块田,且通过路i需要花Ti(1≤Ti≤100)的时间。每头母牛必需参加宴会并且在宴会结束时回到自己的田地,但是每头牛都很懒而喜欢选择化是最少的一个方案。来时的路和去时的可能不一样。求每头牛要来回的最短时间。

找出来回路程最长的。输出来回最长的距离。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<queue>
 5 using namespace std;
 6 #define N 1010
 7 #define Max 10000000
 8 queue<int> q;
 9 bool exist[N];
10 int dis[N],map[N][N],num[N],n,m,x;
11 void init(){
12     memset(exist,false,sizeof(exist));
13     memset(dis,0x3f,sizeof(dis));
14 }
15 void SPFA(int x){
16     init();
17     exist[x]=true;q.push(x);dis[x]=0;
18     while(!q.empty()){
19         int p=q.front();q.pop();exist[p]=false;
20         for(int i=1;i<=n;i++)
21           if(dis[p]+map[p][i]<dis[i]){
22             dis[i]=dis[p]+map[p][i];
23             if(exist[i]==false) q.push(i),exist[i]=true;
24           }
25     }
26 }
27 void trave(){
28     for(int i=1;i<=n;i++)
29       for(int j=1,temp;j<i;j++){// 注意这里是从j到i-1 如果j从1到n 那么相当于没反
30       // 至于为什么 自己想去 
31           temp=map[i][j];map[i][j]=map[j][i];map[j][i]=temp;
32       }
33 }
34 int main()
35 {
36     scanf("%d%d%d",&n,&m,&x);
37     for(int i=1;i<=n;i++)
38       for(int j=1;j<=n;j++)
39         map[i][j]=Max;// 这里表示很不解 循环赋值AC啦
40         // memset赋值 样例都过不了 
41     for(int i=1,u,w,v;i<=m;i++){
42         scanf("%d%d%d",&u,&v,&w);
43         map[u][v]=w;
44     }
45     SPFA(x);
46     for(int i=1;i<=n;i++)
47       num[i]=dis[i];
48     trave();
49     SPFA(x);
50     int max=0;
51     for(int i=1; i<=n; i++)
52         if(dis[i]+num[i]>max)
53             max=dis[i]+num[i];
54     printf("%d\n",max);
55     return 0;
56 }

思路:先来一遍SPFA,这时相当于求出了返程的的距离,全部转存到num数组里,再来一次反边,再跑一边SPFA,即求出了去时的最短路径,相加求和。

POJ 3268 Silver Cow Party

标签:init   为什么   blog   mit   nec   clu   direct   stream   poj   

原文地址:http://www.cnblogs.com/suishiguang/p/6361339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!