zoukankan      html  css  js  c++  java
  • POJ 3268 Silver Cow Party 最短路

    原题链接:http://poj.org/problem?id=3268

    Silver Cow Party
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15545   Accepted: 7053

    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

    题意

    有一只牛举办派对,其他的牛去参加,牛都会走最短路,并且派对结束还要回到自己家里。问哪头牛走的路径最长,输出最长路径。

    题解

    就跑两边spfa,正着反着跑两次。然后就搞定了。

    代码

    #include<iostream>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<cstdio>
    #define INF 1000006
    #define MAX_N 1003
    using namespace std;
    
    struct node {
    public:
        int u, c;
    
        node(int uu, int cc) : u(uu), c(cc) { }
    
        node() { }
    };
    
    struct edge {
    public:
        int to, cost;
    
        edge(int t, int c) : to(t), cost(c) { }
    
        edge() { }
    };
    
    queue<node> que;
    int n,m,x;
    void spfa(int s,vector<edge> G[],int d[]) {
        fill(d, d + n + 1, INF);
        que.push(node(s, 0));
        d[s] = 0;
        while (que.size()) {
            node now = que.front();
            que.pop();
            if (now.c != d[now.u])continue;
            int u = now.u;
            for (int i = 0; i < G[u].size(); i++) {
                int v = G[u][i].to;
                int t = d[u] + G[u][i].cost;
                if (t < d[v]) {
                    d[v] = t;
                    que.push(node(v, t));
                }
            }
        }
    }
    
    vector<edge> G[MAX_N],rG[MAX_N];
    int d[MAX_N],rd[MAX_N];
    int main() {
        scanf("%d%d%d", &n, &m, &x);
        for (int i = 0; i < m; i++) {
            int u, v, c;
            scanf("%d%d%d", &u, &v, &c);
            G[u].push_back(edge(v, c));
            rG[v].push_back(edge(u, c));
        }
        spfa(x, G, d);
        while (que.size())que.pop();
        spfa(x, rG, rd);
        int ans = 0;
        for (int i = 1; i <= n; i++)ans = max(ans, d[i] + rd[i]);
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    jdbc之存储过程的调用和调用方法
    jdbc之Statement和Preparement
    jdbc之连接Oracle的基本步骤
    Oracle之子程序(存储过程、方法、包)
    Oracle之plsql及游标
    Oracle之多表查询
    Oracle之单表查询及常用函数
    Oracle之基础操作
    IO流之字符流
    IO流之字节流
  • 原文地址:https://www.cnblogs.com/HarryGuo2012/p/4746376.html
Copyright © 2011-2022 走看看