[BZOJ1631][Usaco2007 Feb]Cow Party

1631: [Usaco2007 Feb]Cow Party

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 870  Solved: 628 [Submit][Status][Discuss]

Description

    农场有N(1≤N≤1000)个牛棚,每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对.共有M(1≤M≤100000)条单向路连接着牛棚,第i条踣需要Ti的时间来通过.牛们都很懒,所以不管是前去X牛棚参加派对还是返回住所,她们都采用了用时最少的路线.那么,用时最多的奶牛需要多少时间来回呢?

Input

第1行:三个用空格隔开的整数.

 第2行到第M+1行,每行三个用空格隔开的整数:Ai, Bi,以及Ti.表示一条道路的起点,终点和需要花费的时间.

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

样例说明:

共有4只奶牛参加聚会,有8条路,聚会位于第2个农场.

第4只奶牛可以直接到聚会所在地(花费3时间),然后返程路线经过第1和第3个农场(花费7时间),总共10时间.

 
正向建边和反向建边各跑一次最短路,然后扫一遍记录最大值即可
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){
    int n = 0;
    char ch = *++ptr;
    while(ch < '0' || ch > '9') ch = *++ptr;
    while(ch <= '9' && ch >= '0'){
        n = (n << 1) + (n << 3) + ch - '0';
        ch = *++ptr;
    }
    return n;
}
const int maxn = 100000 + 10, maxm = 100000 + 10;
struct Edge{
    int to, val, next;
    Edge(){}
    Edge(int _t, int _v, int _n): to(_t), val(_v), next(_n){}
}e[maxm * 2];
int fir[maxn] = {0}, Fir[maxn] = {0}, cnt = 0;
inline void add1(int u, int v, int w){
    e[++cnt] = Edge(v, w, fir[u]); fir[u] = cnt;
}
inline void add2(int u, int v, int w){
    e[++cnt] = Edge(v, w, Fir[u]); Fir[u] = cnt;
}
int n, m, x;
bool inq[maxn] = {false};
int dis1[maxn], dis2[maxn];
int q[maxn], h, t;
void spfa1(){
    memset(dis1, 0x3f, sizeof dis1);
    dis1[x] = 0;
    h = t = 0;
    q[t++] = x;
    int u, v;
    while(h != t){
        u = q[h++]; if(h == 100010) h = 0;
        inq[u] = false;
        for(int i = fir[u]; i; i = e[i].next){
            v = e[i].to;
            if(dis1[v] > dis1[u] + e[i].val){
                dis1[v] = dis1[u] + e[i].val;
                if(!inq[v]){
                    inq[v] = true;
                    q[t++] = v;
                    if(t == 100010) t = 0;
                }
            }
        }
    }
}
void spfa2(){
    memset(dis2, 0x3f, sizeof dis2);
    dis2[x] = 0;
    h = t = 0;
    q[t++] = x;
    int u, v;
    while(h != t){
        u = q[h++]; if(h == 100010) h = 0;
        inq[u] = false;
        for(int i = Fir[u]; i; i = e[i].next){
            v = e[i].to;
            if(dis2[v] > dis2[u] + e[i].val){
                dis2[v] = dis2[u] + e[i].val;
                if(!inq[v]){
                    inq[v] = true;
                    q[t++] = v;
                    if(t == 100010) t = 0;
                }
            }
        }
    }
}
int main(){
    fread(buf, sizeof(char), sizeof(buf), stdin); 
    n = readint();
    m = readint();
    x = readint();
    for(int u, v, w, i = 1; i <= m; i++){
        u = readint();
        v = readint();
        w = readint();
        add1(u, v, w);
        add2(v, u, w);
    }
    spfa1();
    spfa2();
    int ans = 0;
    for(int i = 1; i <= n; i++)
        ans = max(ans, dis1[i] + dis2[i]);
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2017-09-06 20:18  Elder_Giang  阅读(91)  评论(0编辑  收藏  举报