POJ - 3268 Silver Cow Party(Dijkstra + 链式前向星 && 模板)

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: 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.


大致题意: n个点, m条边,party位置 每条边都是单向的 求奶牛到party并返回所花的时间最大值最小

解题思路: 正向建边跑一次能求出从paty到每个点的最小值   反向建边再跑一遍能求出所有点到party的最小值 找一个奶牛两段路和最大值


#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#define met(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e4 + 10;
using namespace std;

struct Dijkstra
{
    typedef struct EdgeNode
    {
        int to, val, next;
        EdgeNode(int _to, int _val, int _next) { to = _to, val = _val, next = _next; }
    }edges;
    typedef struct HeapNode
    {
        int now, val;
        HeapNode(int _now, int _val) { now = _now, val = _val; }
        bool operator < (const HeapNode& rhs) const { return val > rhs.val; }
    }node;
    int idx;
    vector<edges> V;
    int dis[MAXN], head[MAXN];
    void init()
    {
        met(dis, 0x3f);
        met(head, -1);
        V.clear();
        idx = 0;
        return ;
    }
    void add(int u, int v, int val)
    {
        V.push_back(edges(v, val, head[u]));
        head[u] = idx++;
        return ;
    }
    void dijkstra(int bg)
    {
        priority_queue<node> Q;
        Q.push(node(bg, dis[bg] = 0));
        while (!Q.empty())
        {
            node q = Q.top(); Q.pop();
            if (dis[q.now] < q.val)
                continue;
            for (int i = head[q.now]; i != -1; i = V[i].next)
            {
                int to = V[i].to;
                if (dis[to] > q.val + V[i].val)
                    Q.push(node(to, dis[to] = q.val + V[i].val));
            }
        }
        return ;
    }
}Dj[2];//0为正向边 1为反向边

int main()
{
    int n, m, star;
    while (cin >> n >> m >> star)
    {
        Dj[0].init();
        Dj[1].init();
        for (int i = 0; i < m; i++)
        {
            int u, v, w;
            cin >> u >> v >> w;
            Dj[0].add(u, v, w);
            Dj[1].add(v, u, w);
        }
        Dj[0].dijkstra(star);
        Dj[1].dijkstra(star);
        int maxn = 0;
        for (int i = 1; i <= n; i++)
        {
            if (Dj[0].dis[i] == INF || Dj[1].dis[i] == INF)
                continue;
            maxn = max(maxn, Dj[0].dis[i] + Dj[1].dis[i]);
        }
        cout << maxn << endl;
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值