1. 程式人生 > 實用技巧 >Silver Cow Party

Silver Cow Party

總時間限制: 2000ms
記憶體限制: 65536

題目描述

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?

輸入格式:

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.

輸出格式:

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

樣例輸入

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

樣例輸出

10
提示

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.

題意

求有向圖求各點到X和X到各點的最短路徑之和中的最大值

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>

#define maxn 1000 + 5
#define INF 0x3f3f3f3f
#define xin(a) scanf("%d", &a)
#define ffr(i, m, n) for ( i = m; i < n; i++ )
#define clr(a, b) memset(a, b, sizeof(a));
#define pi acos(-1, 0)
typedef double db;
typedef long long ll;
using namespace std;
int dis[maxn];       //最小路徑權值
int vis[maxn];       //是否訪問過
int path[maxn];      //儲存路徑
int G[maxn][maxn];   //權值資訊
int point, edge, zd; // point個座標,edge條邊
void Dijkstra(int star, int end)
{
        for ( int i = 1; i <= point; i++ )
        {
            vis[i] = 0;
            dis[i] = G[star][i];
        }
        for ( int i = 1; i <= point; i++ )
        { //遍歷全部點
            int x, minn = INF;
            for ( int j = 1; j <= point; j++ )
            { //找出距離未訪問點的最小距離和該點
                if ( !vis[j] && dis[j] <= minn )
                {
                    x = j;
                    minn = dis[j];
                }
            }
            vis[x] = 1;
            for ( int j = 1; j <= point; j++ )
            { //更新未訪問點經過最小距離點後的最小權值
                if ( !vis[j] && dis[x] + G[x][j] < dis[j] ) { dis[j] = dis[x] + G[x][j]; }
            }
        }
    }
void init() //初始化圖
{
    int i, j, a, b, c;
    cin >> point >> edge >> zd;
    for ( i = 1; i <= point; i++ )
        for ( j = 1; j <= point; j++ )
            if ( i == j ) G[i][j] = 0;
            else
            {
                G[i][j] = INF;
            }
    for ( i = 0; i < edge; i++ )
    {
        cin >> a >> b >> c;
        G[a][b] = min(c, G[a][b]);
    }
}
void n(void)
{
    int n, i, d = -1, j, sum = 0;
    init();
    Dijkstra(zd, 1);
    for ( i = 1; i <= point; i++ ) {
        path[i] = dis[i];
    }
    for ( i = 1; i <= point; i++ )
        for ( j = i+1; j <= point; j++ ) swap(G[i][j], G[j][i]);
    Dijkstra(zd, i);
    for ( i = 1; i <= point; i++ )
    {
        path[i] += dis[i];
        d = max(d, path[i]);
    }
    cout << d << endl;
}
int main(void)
{
    #ifndef ONLINE_JUDGE
    long _begin_time = clock();
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    n();
    #ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms\n", _end_time - _begin_time);
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}

解題思路

x到各點的最短路徑由Dijkstra演算法求出,第一次提交直接用了n次求n到x最短路徑,不出意料的超時.經過查詢各點到x的最短路徑只需將有向圖翻轉,仍可由Dijkstra求出,求出x到各點最短路徑,也就是各點到x的最短路徑,即可.