您的位置:首页 > 其它

POJ 3268 Silver Cow Party

2017-11-07 13:11 465 查看
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:
N, M, 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 - 1头牛去另外一头牛家参加派对, 并会在结束后返回自己的家中, 但是他们都很懒, 所以会走效率高的路(尽可能短的路), 请求出走得最远的牛走了多远。

解题思路:

一开始用Dijkstra傻傻的从单源点向其他点跑和从其他点向单源点跑, 妥妥TLE, 按点跑Dijkstra的时间复杂度是O(N^2)。 于是我们可以开两个数组, 一个记录所有牛回家的距离, 一个记录所有牛走的距离。 两个距离相加取最大值, 对于从其他点向单源点跑的操作, 我们可以对于初始矩阵进行矩阵转置操作, 相当于在反着跑一边。O(2 * N)。第二种解法可以用优先队列, 按着边开始跑,时间复杂度为O(N * (Elog(E)))。

代码:

#include <iostream>

#include <sstream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <iomanip>

#include <utility>

#include <string>

#include <cmath>

#include <vector>

#include <bitset>

#include <stack>

#include <queue>

#include <deque>

#include <map>

#include <set>

using namespace std;

/*

 *ios::sync_with_stdio(false);

 */

typedef long long ll;

typedef unsigned long long ull;

const int inf = 0x7fffffff;

const int mod = 1000000;

const int Max = 1010;

int n, m ,x;

int Map[Max][Max][2];

bool vis[Max];

int dis_c[Max], dis_g[Max];

void Init() {

    for (int i = 1; i <= n; ++i) {

        for (int j = 1; j <= n; ++j) {

            Map[i][j][0] = (i == j) ? 0 : inf;

            //Map[j][i][0] = (i == j) ? 0 : inf;

            Map[i][j][1] = (i == j) ? 0 : inf;

            //Map[j][i][1] = (i == j) ? 0 : inf;

        }

    }

}

void map_Swap() {

    for (int i = 1; i <= n; ++i) {

        for (int j = 1; j <= n; ++j) {

            Map[i][j][0] = Map[j][i][1];

        }

    }

}

void Dijkstra(int st, int f) {

    int dis[Max];

    for (int i = 1; i <= n; ++i) {

        dis[i] = Map[st][i][f];

        vis[i] = 0;

    }

    vis[st] = 1;

    for (int i = 1; i <= n; ++i) {

        int Min = inf, mark;

        for (int j = 1; j <= n; ++j) {

            if (!vis[j] && Min > dis[j]) {

                Min = dis[mark = j];

            }

        }

        vis[mark] = true;

        for (int j = 1; j <= n; ++j) {

                        // that's very important

            if (!vis[j] && Map[mark][j][f] != inf && dis[j] > dis[mark] + Map[mark][j][f]) {

                dis[j] = dis[mark] + Map[mark][j][f];

            }

        }

    }

    if (f == 0) {

        for (int i = 1; i <= n; ++i) {

            dis_c[i] = dis[i];

        }

    }

    else{

        for (int i = 1; i <= n; ++i) {

            dis_g[i] = dis[i];

        }

    }

}

void check() {

    int ans = -1;

    for (int i = 1; i <= n; ++i) {

        ans = max(ans, (dis_c[i] + dis_g[i]));

    }

    printf("%d\n", ans);

}

int main() {

    // initialization

    scanf("%d %d %d", &n, &m, &x);

    Init();

    for (int i = 0; i < m; ++i) {

        int x, y, t;

        scanf("%d %d %d", &x, &y, &t);

        if (Map[x][y][1] > t)

            Map[x][y][1] = t;

    }

    // back

    Dijkstra(x, 1);

    // a to b >> b to a

    map_Swap();

    // go

    Dijkstra(x, 0);

    // output the answer

    check();

    return 0;

}

/** Dijkstra + priority_queue

#include <iostream>

#include <sstream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <iomanip>

#include <utility>

#include <string>

#include <cmath>

#include <vector>

#include <bitset>

#include <stack>

#include <queue>

#include <deque>

#include <map>

#include <set>

using namespace std;

/*

 *ios::sync_with_stdio(false);

 */

typedef long long ll;

typedef unsigned long long ull;

const int inf = 0x7fffffff;

const int mod = 1000000;

const int Max = 1010;

int n, m ,x;

int dis[Max];

bool vis[Max];

struct node {

    // endpoint and distance

    int x, d;

    node() {};

    node(int a, int b) {

        x = a;

        d = b;

    }

    bool operator < (const node &a) const {

        if (d == a.d) return x < a.x;

        else return d > a.d;

    }

};

vector < node> edge[Max];

void Init() {

    for (int i = 0; i <= n; ++i) {

        edge[i].clear();

    }

}

int Dijkstra(int st, int ed) {

    for (int i = 0; i <= n; ++i) {

       dis[i] = inf;

    }

    dis[st] = 0;

    priority_queue < node> Q;

    Q.push(node(st, dis[st]));

    while (!Q.empty()) {

        node x = Q.top();

        Q.pop();

        for (int i = 0; i < edge[x.x].size(); ++i) {

            node y = edge[x.x][i];

            if (dis[y.x] > x.d + y.d) {

                dis[y.x] = x.d + y.d;

                Q.push(node(y.x, dis[y.x]));

            }

        }

    }

    return dis[ed];

}

void check() {

    int ans = 0, minn;

    for (int i = 1; i <= n; ++i) {

        minn = Dijkstra(x, i) + Dijkstra(i, x);

        if (minn > ans)

            ans = minn;

    }

    printf("%d\n", ans);

}

int main() {

    // initialization

    scanf("%d %d %d", &n, &m, &x);

    Init();
    for (int i = 0; i < m; ++i) {

        int x, y, t;

        scanf("%d %d %d", &x, &y, &t);

        edge[x].push_back(node(y, t));

    }

    // perform the algorithm

    check();

    return 0;

}

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: