POJ-3268 Silver Cow Party

34 篇文章 0 订阅
14 篇文章 0 订阅
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K

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 ≤ XN). 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.
————————————————————集训13.4的分割线————————————————————
前言:跟POJ-1511 Invitation Cards
一个思路。
思路:建立两张图,一张是正的,另一张是反的。所以源点仅有X点,跑两次SPFA。最后维护一下答案。找最大的最小值之和。
代码如下:
/* 
ID: j.sure.1 
PROG:  
LANG: C++ 
*/  
/****************************************/  
#include <cstdio>  
#include <cstdlib>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <map>  
#include <string>  
#include <iostream>  
#include <climits>  
#define INF 0x3f3f3f3f  
using namespace std;  
/****************************************/    
const int N = 1005, M = 1e5+5;  
int n, m, tot, head[N], ophead[N],  dis[2][N];  
bool inq[N];  
struct Node {  
    int u, v, w;  
    int next;  
}edge[M], opedge[M];  
  
void init()  
{  
    tot = 0;  
    memset(head, -1, sizeof(head));  
    memset(ophead, -1, sizeof(ophead));  
}  
  
void add(int u, int v, int w)
{  
    edge[tot].u = u; edge[tot].v = v; edge[tot].w = w;  
    edge[tot].next = head[u]; head[u] = tot;  
    opedge[tot].u = v; opedge[tot].v = u; opedge[tot].w = w;  
    opedge[tot].next = ophead[v]; ophead[v] = tot;  
    tot++;  
}  
  
void spfa(int st, int (&hh)[N], Node (&Edge)[M], int id)//C++引用数组的方法,避免降值问题的发生  
{
	for(int i = 0; i <= n; i++)
		dis[id][i] = INF;
    memset(inq, 0, sizeof(inq));  
    queue <int> q;//局部队列,不需清空  
    q.push(st);  
    dis[id][st] = 0;  
    inq[st] = true;  
    while(!q.empty()) {  
        int u = q.front();  
        q.pop();  
        inq[u] = false;  
        for(int i = hh[u]; i != -1; i = Edge[i].next) {  
            int v = Edge[i].v;  
            if(dis[id][v] > dis[id][u] + Edge[i].w) {//松弛操作  
                dis[id][v] = dis[id][u] + Edge[i].w;  
                if(!inq[v]) {  
                    q.push(v);  
                    inq[v] = true;  
                }  
            }  
        }  
    }  
}  
  
int main()  
{  
#ifdef J_Sure  
    freopen("000.in", "r", stdin);  
//  freopen(".out", "w", stdout);  
#endif     
	init();
	int st;
	scanf("%d%d%d", &n, &m, &st);  
	int a, b, c;  
	for(int i = 0; i < m; i++) {  
		scanf("%d%d%d", &a, &b, &c);  
		add(a, b, c);
	}  
	int ans = 0;  
	spfa(st, head, edge, 0);  
	spfa(st, ophead, opedge, 1);  
	int maxi = -1;
	for(int i = 2; i <= n; i++) {  
		maxi = max(maxi, dis[0][i] + dis[1][i]);  
	}  
	printf("%d\n", maxi);//正反各跑一次,统计答案  
     
    return 0;    
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值