1. 程式人生 > >Silver Cow Party(最短路 + Dijkstra + 鄰接表 + 優先佇列)

Silver Cow Party(最短路 + Dijkstra + 鄰接表 + 優先佇列)

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11348 Accepted: 5077

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(1~1000),M(1 ~ 100000),X(1 ~ N),代表有 N 個地方,M條路,目的地是X。後給出 M 條路,每條路都是單向的,代表 A 到 B 需要花費 T (1 ~ 100)的時間。每個地點都要到達終點且從終點返回原地方,每條路都要求時間最短。求出這麼多地點中的所需最長時間。

     思路:

     最短路。一看以為是Floyd,但是TLE之後分析了下時間複雜度O(n ^ 3)就是 1000 ^ 3 = 10 ^ 9 = 10 ^ 7 * 100 = 1s * 100,100s的時間,題目給的是2000ms = 2s,果斷超時不能用Floyd。故只能用 Dijkstra 一個個單源算最短路,因為沒有負環的情況故用 Dijkstra。順便總結下時間複雜度(N為節點數,M為邊數):

    Floyd :O(N ^ 3);

    Dijkstra :鄰接矩陣 O(N ^ 2);鄰接表 + 優先佇列:O(NlogN + M)從優先佇列取元素出來是logN,一共需要取N次,故為NlogN,每次最多可能要遍歷M條邊故為NlogN + M;

    Bellman Ford :O(N * M);

    SPFA :O(kM)= O(M);

    所以,用 Dikstra 鄰接表 + 優先佇列是能夠滿足題目要求的。    

    AC:

#include <cstdio>
#include <queue>
#include <utility>
#include <iostream>
#include <string.h>
#include <vector>
#define MAX 100005
#define INF 99999999
using namespace std;

typedef pair<int,int> pii;

int v[MAX],w[MAX],fir[1005],next[MAX];
int d[1005][1005],vis[1005];
int ind,n;

void add_edge(int f,int t,int val) {
    v[ind] = t;
    w[ind] = val;
    next[ind] = fir[f];
    fir[f] = ind;
    ind++;
}

void Dijstra(int s) {
    for(int i = 1;i <= n;i++)   d[s][i] = INF;
    memset(vis,0,sizeof(vis));
    d[s][s] = 0;
    priority_queue<pii,vector<pii>,greater<pii> > q;
    q.push(make_pair(d[s][s],s));
    while(!q.empty()) {
        pii k = q.top();q.pop();
        int x = k.second;
        if(vis[x])  continue;
        vis[x] = 1;
        for(int e = fir[x];e != -1;e = next[e]) {
            if(d[s][v[e]] > d[s][x] + w[e]) {
               d[s][v[e]] = d[s][x] + w[e];
               q.push(make_pair(d[s][v[e]],v[e]));
            }
        }
    }
}

int main() {
    int m,p;
    memset(fir,-1,sizeof(fir));
    scanf("%d%d%d",&n,&m,&p);
    ind = 0;
    while(m--) {
        int f,t,val;
        scanf("%d%d%d",&f,&t,&val);
        add_edge(f,t,val);
    }

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

    int max_time = -1;
    for(int i = 1;i <= n;i++) {
        if(i == p)  continue;
        if(max_time < d[i][p] + d[p][i])
           max_time = d[i][p] + d[p][i];
    }

    printf("%d\n",max_time);
    return 0;
}