POJ3268 Silver Cow Party

PS: 对原图和反图求最短路,然后求最长的即可。省赛热身练习。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1010;
const int INF = 0x3ffffff;
//Accepted	468K	63MS
struct edges {
    int from, to, w;
};

int n, m, s;
int d1[maxn];
int d2[maxn];

queue<int> que;
bool inque[maxn];

void spfa(vector<edges> G[], int d[]) {
    while(!que.empty()) que.pop();
    memset(inque, false, sizeof(inque));
    for(int i = 1; i <= n; i++) d[i] = INF;
    d[s] = 0;
    inque[s] = true;
    que.push(s);
    while(!que.empty()) {
        int u = que.front(); que.pop();
        for(int i = 0; i < (int)G[u].size(); i++) {
            edges v = G[u][i];
            if(d[u]!=INF && d[u]+v.w < d[v.to]) {
                d[v.to] = d[u] + v.w;
                if(!inque[v.to]) {
                    inque[v.to] = true;
                    que.push(v.to);
                }
            }
        }
        inque[u] = false;
    }
}

int work() {
    int ans = -INF;
    for(int i = 1; i <= n; i++) {
        if(d1[i]+d2[i]>ans) {
            ans = d1[i] + d2[i];
        }
    }
    return ans;
}

vector<edges> G[maxn];
vector<edges> RG[maxn];
int main()
{
    edges t;
    int from, to, w;
    while(scanf("%d%d%d", &n, &m, &s)!=EOF) {
        for(int i = 1; i <= m; i++) {
           scanf("%d%d%d", &from, &to, &w);
           t.from = from;
           t.to = to;
           t.w = w;
           G[from].push_back(t);
           t.from = to;
           t.to = from;
           RG[t.from].push_back(t);
        }
        spfa(G, d1);
        spfa(RG, d2);
        int res = work();
        printf("%d\n", res);
    }
    return 0;
}

你可能感兴趣的:(POJ3268 Silver Cow Party)