poj 3628 Silver Cow Party (spfa)

来源:互联网 发布:关闭windows数字签名 编辑:程序博客网 时间:2024/05/29 06:53

http://poj.org/problem?id=3268

 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 Titime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都有一个参加聚会并且回来的最短时间,从这些最短时间里找出一个最大值输出

解题思路:最短路径只需要从x到i的最短路径代表他们返回的最短路径,然后将所有边反过来,再从x到i的最短路径代表他们来参加聚会的最短路径,这样对应相加找出一个最大值就可以了,当然其实不需要将所有边反过来,只需要将g的行和列对换一下就可以了,数据比较大,所以floyd超时,用dijkstra比较好点

//之前多次调用dijkstra,导致超时,又看了大牛的解题思路,将g的行和列对换一下就可以了哭

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>using namespace std;#define N 1010#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8int g[N][N], dist[N], dback[N], vis[N], n, m, x;int Dij ();int main (){    cin >> n >> m >> x;    for (int i=1; i<=n; i++)    {        for (int j=1; j<=n; j++)        {            if (i != j) g[i][j] = INF;            else g[i][j] = 0;        }    }    int a, b, c;    for (int i=0; i<m; i++)    {        cin >> a >> b >> c;        g[a][b] = c;    }    cout << Dij () << endl;    return 0;}int Dij (){    for (int i=1; i<=n; i++)    {        vis[i] = 0;        dist[i] = g[x][i];        dback[i] = g[i][x];//行列调换    }    for (int i=1; i<=n; i++)    {        int index = 0, minx = INF;        for (int j=1; j<=n; j++)            if (!vis[j] && dist[j] < minx)                minx = dist[j], index = j;        vis[index] = 1;        for (int j=1; j<=n; j++)            if (!vis[j] && dist[j] > dist[index] + g[index][j])                dist[j] = dist[index] + g[index][j];    }    memset (vis, 0, sizeof (vis));    for (int i=1; i<=n; i++)    {        int index = 0, minx = INF;        for (int j=1; j<=n; j++)            if (!vis[j] && dback[j] < minx)                minx = dback[j], index = j;        vis[index] = 1;        for (int j=1; j<=n; j++)            if (!vis[j] && dback[j] > dback[index] + g[j][index])                dback[j] = dback[index] + g[j][index];    }    int maxn = -INF;    for (int i=1; i<=n; i++)        if (maxn < dist[i] + dback[i])            maxn = dist[i] + dback[i];//去返总路程    return maxn;}


spfa算法实现


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 1010#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))struct node{    int a, b, c;}p[N*N];int n, m, x, g[N][N], dist[N], dback[N], vis[N];int SPFA ();int main (){    scanf ("%d %d %d", &n, &m, &x);    met (g, INF);    for (int i=1; i<=m; i++)    {        scanf ("%d %d %d", &p[i].a, &p[i].b, &p[i].c);        g[ p[i].a ][ p[i].b ] = min (g[ p[i].a ][ p[i].b ], p[i].c);    }    printf ("%d\n", SPFA ());    return 0;}int SPFA (){    queue <int> que;    que.push (x);    met (dist, INF);    met (vis, 0);    dist[x] = 0;    vis[x] = 1;    while (que.size ())    {        int Index = que.front (); que.pop ();        vis[Index] = 0;        for (int i=1; i<=n; i++)        {            if (dist[i] > dist[Index] + g[Index][i])            {                dist[i] = dist[Index] + g[Index][i];                if (!vis[i])                {                    vis[i] = 1;                    que.push (i);                }            }        }    }    met (g, INF);    for (int i=1; i<=m; i++)        g[ p[i].b ][ p[i].a ] = min (g[ p[i].b ][ p[i].a ], p[i].c);    queue <int> que1;    que1.push (x);    for (int i=1; i<=n; i++)        dback[i] = INF;    met (vis, 0);    dback[x] = 0;    vis[x] = 1;    while (que1.size ())    {        int Index = que1.front (); que1.pop ();        vis[Index] = 0;        for (int i=1; i<=n; i++)        {            if (dback[i] > dback[Index] + g[Index][i])            {                dback[i] = dback[Index] + g[Index][i];                if (!vis[i])                {                    vis[i] = 1;                    que1.push (i);                }            }        }    }    int maxn = -INF;    for (int i=1; i<=n; i++)        maxn = max (maxn, dist[i] + dback[i]);    return maxn;}



0 0
原创粉丝点击