poj 3268 Silver Cow Party 【最短路Dijkstra + 结构体妙用】

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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.

题意:给定N个顶点,M条边,构成一个单向图,求从图上某点到 X点 (已知)来回的最大最短路。

分析:题目很水,一次是固定终点,一次是固定起点,终点固定可以看成固定起点同时把边反向,两次Dijkstra,求和就可以了。但是这里有一个结构体的妙用,可以简化代码,而且用起来超级方便,给推荐~~~

/****************************>>>>HEADFILES<<<<****************************/
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
/****************************>>>>>DEFINE<<<<<*****************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define rep(i,a,b)      for(int i = a;i <= b;i++)
#define rep0(i,b)       for(int i = 0;i < b;i++)
#define rep1(i,b)       for(int i = 1;i <= b;i++)
#define MP(a,b)         make_pair(a,b)
#define PB(a)           push_back(a)
#define fst             first
#define snd             second
/****************************>>>>>>DEBUG<<<<<<****************************/
#define out(x)          cout<<x<<"  ";
/****************************>>>>SEPARATOR<<<<****************************/
const int maxn = 1000 + 5;
const int INF = 0x3f3f3f3f;
int N,M,X;
struct Edge
{
    int from, to, dist;
    Edge() {}
    Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist) {}
};
struct HeapNode
{
    int pos, d;
    HeapNode() {}
    HeapNode(int _pos, int _d) : pos(_pos), d(_d) {}
    bool operator < (const HeapNode & p) const
    {
        return d > p.d;
    }
};
struct Dijkstra
{
    int n, s, d[maxn];
    bool vis[maxn];
    vector<Edge> edges;
    vector<int> G[maxn];
    Dijkstra() {}
    Dijkstra(int _n, int _s) : n(_n), s(_s) {}
    void init(int _n, int _s)
    {
        this->n = _n, this->s = _s;
        edges.clear();
        rep1(i, n)
        {
            G[i].clear();
        }
    }
    void AddEdge(int u, int v, int t)
    {
        edges.PB(Edge(u, v, t));
        int m = edges.size();
        G[u].PB(m - 1);
    }
    void dijkstra()
    {
        memset(vis, false, sizeof(vis));
        rep1(i, n) d[i] = (i == s ? 0 : INF);
        priority_queue<HeapNode> Que;
        Que.push(HeapNode(s, 0));
        while(!Que.empty())
        {
            HeapNode now = Que.top();
            Que.pop();
            int u = now.pos;
            if(vis[u]) continue;
            vis[u] = true;

            for(int i = 0; i < G[u].size(); i++)
            {
                Edge& e = edges[G[u][i]];
                if(d[e.to] > d[u] + e.dist)
                {
                    d[e.to] = d[u] + e.dist;
                    Que.push(HeapNode(e.to,d[e.to]));
                }

            }
        }
    }
}dij1,dij2;

int main()
{
   // FIN;
    while(~scanf("%d %d %d",&N,&M,&X))
    {
        dij1.init(N,X);dij2.init(N,X);
        int a,b,t;
        rep(i,1,M)
        {
            scanf("%d %d %d",&a,&b,&t);
            dij1.AddEdge(b,a,t);
            dij2.AddEdge(a,b,t);
        }
        dij1.dijkstra();
        dij2.dijkstra();
        int ans = 0;
        rep1(i,N)
            ans = max(ans,dij1.d[i] + dij2.d[i]);
        printf("%d\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-13 21:31:13

poj 3268 Silver Cow Party 【最短路Dijkstra + 结构体妙用】的相关文章

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party(SPFA)

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 re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 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 

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 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

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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 ≤

POJ 3268 Silver Cow Party (Dijkstra)

题目链接:POJ 3268 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