POJ3268 - Silver Cow Party(最短路)

Silver Cow Party POJ - 3268

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
题意:给定N个点和M条边,和一个终点,求所有点到终点并从终点到所有点的最短距离的最大值。

思路:一开始想逐一求每个点到终点的最短路,再算从终点到改点的最短路,加和然后与当前最大值比较。但是复杂度太高,超时。之后改成先求从终点到个点的最短路,记录在数组里,然后a[i][j] 和 a[j][i]进行互换,再求一遍,存在第二个数组里,两个数组对应位置加和求最大值。

TLE代码

#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f
using namespace std;
int a[1005][1005], vis[1005], book[1005];
int main()
{
    std::ios::sync_with_stdio(0);
    int n, m, e, i, j, k, x, t;
    cin >> n >> m >> e;
    memset(a, 0, sizeof(a));
    memset(book, 0, sizeof(book));
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            if(i != j)a[i][j] = inf;
        }
    }
    for(k = 1; k <= m; k++)
    {
        cin >> i >> j >> x;
        if(a[i][j] > x)
        {
            a[i][j] = x;
        }
    }
    int ans = -1;
    for(t = 1; t <= n; t++)
    {
        memset(book, 0, sizeof(book));
        for(i = 1; i <= n; i++)vis[i] = a[t][i];
        book[t] = 1;
        while(book[e] == 0)
        {
            int minn = inf, u;
            for(i = 1; i <= n; i++)
            {
                if(book[i] == 0 && vis[i] < minn)
                {
                    minn = vis[i];
                    u = i;
                }
            }
            book[u] = 1;
            for(i = 1; i <= n; i++)
            {
                if(vis[i] > vis[u] + a[u][i])
                {
                    vis[i] = vis[u] + a[u][i];
                }
            }
        }
        k = vis[e];
        memset(book, 0, sizeof(book));
        for(i = 1; i <= n; i++)vis[i] = a[e][i];
        book[e] = 1;
        while(book[t] == 0)
        {
            int minn = inf, u;
            for(i = 1; i <= n; i++)
            {
                if(book[i] == 0 && vis[i] < minn)
                {
                    minn = vis[i];
                    u = i;
                }
            }
            book[u] = 1;
            for(i = 1; i <= n; i++)
            {
                if(vis[i] > vis[u] + a[u][i])
                {
                    vis[i] = vis[u] + a[u][i];
                }
            }
        }
        x = vis[t];
        //if(x + k >= inf)continue;
        //cout << x + k << endl;
        ans = max(ans, x + k);
    }
    cout << ans << endl;
}

AC代码

#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f
using namespace std;
int a[1005][1005], vis[1005], book[1005], ans[1005] = {0};
int main()
{
    std::ios::sync_with_stdio(0);
    int n, m, e, i, j, k, x, t;
    cin >> n >> m >> e;
    memset(a, 0, sizeof(a));
    memset(book, 0, sizeof(book));
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            if(i != j)a[i][j] = inf;
        }
    }
    for(k = 1; k <= m; k++)
    {
        cin >> i >> j >> x;
        if(a[i][j] > x)
        {
            a[i][j] = x;
        }
    }
    memset(book, 0, sizeof(book));
    for(i = 1; i <= n; i++)vis[i] = a[e][i];
    book[e] = 1;
    for(t = 1; t <= n - 1; t++)
    {
        int minn = inf, u;
        for(i = 1; i <= n; i++)
        {
            if(book[i] == 0 && vis[i] < minn)
            {
                minn = vis[i];
                u = i;
            }
        }
        book[u] = 1;
        for(i = 1; i <= n; i++)
        {
            if(vis[i] > vis[u] + a[u][i])
            {
                vis[i] = vis[u] + a[u][i];
            }
        }
    }
    for(i = 1; i <= n; i++)ans[i] = vis[i];
    for(i = 1; i <= n; i++)
    {
        for(j = 1; j <= n; j++)
        {
            if(i < j)
            {
                int r = a[i][j];
                a[i][j] = a[j][i];
                a[j][i] = r;
            }
        }
    }
    memset(book, 0, sizeof(book));
    for(i = 1; i <= n; i++)vis[i] = a[e][i];
    book[e] = 1;
    for(t = 1; t <= n - 1; t++)
    {
        int minn = inf, u;
        for(i = 1; i <= n; i++)
        {
            if(book[i] == 0 && vis[i] < minn)
            {
                minn = vis[i];
                u = i;
            }
        }
        book[u] = 1;
        for(i = 1; i <= n; i++)
        {
            if(vis[i] > vis[u] + a[u][i])
            {
                vis[i] = vis[u] + a[u][i];
            }
        }
    }
    int maxn = -1;
    for(i = 1; i <= n; i++)maxn = max(maxn, vis[i] + ans[i]);
    cout << maxn << endl;
}

你可能感兴趣的:(最短路)