POJ 3268 Silver Cow Party

给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出。

最短路径只需要从x到i的最短路径代表他们返回的最短路径,然后将所有边反过来,再从x到i的最短路径代表他们来参加聚会的最短路径,这样对应相加找出一个最大值就可以了,当然其实不需要将所有边反过来。

反向找路径,很有意思的一道题,用邻接表超快,先给出我的第一个版本

//384K	47MS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pii;

int n, m, x;
const int maxn = 1000;
const int maxm = 100000;
const int inf = 0x7fffffff;
vector<pii> G1[maxn+5];
vector<pii> G2[maxn+5];
int d1[maxn+5], d2[maxn+5];

void Dijkstra(vector<pii> G[], int d[], int s){
    priority_queue< pii, vector<pii>, greater<pii> > pque;
    fill(d+1, d+n+1, inf);
    d[s] = 0;
    pque.push(pii(0, s));
    while(!pque.empty()){
        pii t = pque.top(); pque.pop();
        int v = t.second;
        if(d[v] < t.first) continue;
        int len = G[v].size();
        for(int i=0; i<len; i++){
            pii e = G[v][i];
            if(d[e.second] > t.first + e.first){
                d[e.second] = t.first + e.first;
                pque.push(pii(d[e.second], e.second));
            }
        }
    }
}

int main(){
    while(~scanf("%d%d%d", &n, &m, &x)){
        int a, b, t;
        for(int i=1; i<=m; i++){
            scanf("%d%d%d", &a, &b, &t);
            G1[a].push_back(pii(t, b));
            G2[b].push_back(pii(t, a));
        }
        Dijkstra(G1, d1, x);
        Dijkstra(G2, d2, x);
        int res = 0;
        for(int i=1; i<=n; i++){
            if(d1[i]+d2[i] > res) res = d1[i]+d2[i];
        }
        printf("%d\n", res);
    }
    return 0;
}

下面是我自定义优先级的比较函数,没有用pair

//384K	79MS
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

struct node{
    int cost, to;
    node(int cost=0, int to=0) :
        cost(cost), to(to) {
        }
    bool operator < (const node& j) const {
        return cost > j.cost;
    }
};

int n, m, x;
const int maxn = 1000;
const int maxm = 100000;
const int inf = 0x7fffffff;
vector<node> G1[maxn+5];
vector<node> G2[maxn+5];
int d1[maxn+5], d2[maxn+5];

void Dijkstra(vector<node> G[], int d[], int s){
    priority_queue<node> pque;
    fill(d+1, d+n+1, inf);
    d[s] = 0;
    pque.push(node(0, s));
    while(!pque.empty()){
        node t = pque.top(); pque.pop();
        int v = t.to;
        if(d[v] < t.cost) continue;
        int len = G[v].size();
        for(int i=0; i<len; i++){
            node n = G[v][i];
            if(d[n.to] > t.cost + n.cost){
                d[n.to] = t.cost + n.cost;
                pque.push(node(d[n.to], n.to));
            }
        }
    }
}

int main(){
    while(~scanf("%d%d%d", &n, &m, &x)){
        int a, b, t;
        for(int i=1; i<=m; i++){
            scanf("%d%d%d", &a, &b, &t);
            G1[a].push_back(node(t, b));
            G2[b].push_back(node(t, a));
        }
        Dijkstra(G1, d1, x);
        Dijkstra(G2, d2, x);
        int res = 0;
        for(int i=1; i<=n; i++){
            if(d1[i]+d2[i] > res) res = d1[i]+d2[i];
        }
        printf("%d\n", res);
    }
    return 0;
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值