POJ 3255 Roadblocks

来源:互联网 发布:opencv java 人脸识别 编辑:程序博客网 时间:2024/06/05 05:02

Roadblocks
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11445 Accepted: 4036

Description

Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shortest rather than the shortest path. She knows there must be some second-shortest path.

The countryside consists of R (1 ≤ R ≤ 100,000) bidirectional roads, each linking two of the N (1 ≤N ≤ 5000) intersections, conveniently numbered 1..N. Bessie starts at intersection 1, and her friend (the destination) is at intersectionN.

The second-shortest path may share roads with any of the shortest paths, and it may backtrack i.e., use the same road or intersection more than once. The second-shortest path is the shortest path whose length is longer than the shortest path(s) (i.e., if two or more shortest paths exist, the second-shortest path is the one whose length is longer than those but no longer than any other path).

Input

Line 1: Two space-separated integers: N andR
Lines 2..R+1: Each line contains three space-separated integers: A,B, and D that describe a road that connects intersections A andB and has length D (1 ≤ D ≤ 5000)

Output

Line 1: The length of the second shortest path between node 1 and nodeN

Sample Input

4 41 2 1002 4 2002 3 2503 4 100

Sample Output

450

Hint

Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)

题目大意:在一个图上有许多个农场,有个人从1农场出发,到他的朋友n农场去,他不想走一条最短路径,
这次他想换条路走,要你帮他找一条次短路径,次短路的定义是,比最短路径长度短(可能有多条),
但是不会比其他的路径长度长。而且告诉你数据中一定存在至少一条次短路。
解题思路:大致的分析下,如果我们用常规思想做这题:
删除某一条边求最短路径,找出的最短路径比最短路径短,但是比其他路径长就是的了
这样做的时间复杂度是,spfa的时间复杂度大约是O(KE),E为边的总数=200000
然后枚举边O(E),那么总的时间复杂度为O(KE^2),大约是K*400亿。这样明显会超时。

最短路明显不会是次短路,因为题目说了次短一定是存在的,那么他们不可能重合,
这样次短路肯定是最短路中某一条边不走,而走了其他边再回到最短路上,而且不可能绕两个地方,
只可能绕一个地方,因为明显绕两个地方比绕一个地方的路径长,明显不是次短路了
所以我们枚举每条边<s,t>
有d[s]---》源点到s的最短距离
有dr[t]----》t到汇点的最短距离,这样就需要从t到s求一次最短路得到了
len<s,t>表示<s,t>这条边的长度
然后我们枚举每一条边有:tmp=d[s]+dr[t]+len<s,t>
找出其中比最短路小但是比其他路长的一个值就是次短路径了


#include <stdio.h>#include <string.h>#include <queue>#define MAX 5005using namespace std;int N, R;int num;struct node{int v;int w;} e[200005];int first[MAX];int next[200005];int dist[MAX];int rdist[MAX];int vis[MAX];typedef pair<int, int> PAIR;PAIR temp;priority_queue<PAIR, vector<PAIR>, greater<PAIR> > Q;void add(int u, int v, int w){e[num].v = v;e[num].w = w;next[num] = first[u];first[u] = num++;}void Dijkstra(int S, int dt[]){int i, j, k, w;memset(vis, 0, sizeof(vis));dt[S] = 0;Q.push(make_pair(dt[S], S));while(!Q.empty()){temp = Q.top();Q.pop();i = temp.second;if(vis[i])continue;vis[i] = 1;for(j = first[i]; j != 0; j = next[j]){k = e[j].v;w = e[j].w;if(!vis[k] && dt[k] > dt[i] + w){dt[k] = dt[i] + w;Q.push(make_pair(dt[k], k));}}}}int main(){int A, B, C, i, j, k, w, ans;while(scanf("%d%d", &N, &R) != EOF){num = 1;memset(first, 0, sizeof(first));memset(next, 0, sizeof(next));for(i = 1; i <= R; i++){scanf("%d%d%d", &A, &B, &C);add(A, B, C);add(B, A, C);}memset(dist, 0x3f, sizeof(dist));memset(rdist, 0x3f, sizeof(rdist));Dijkstra(1, dist);Dijkstra(N, rdist);ans = 0x3f3f3f3f;for(i = 1; i <= N; i++){for(j = first[i]; j != 0; j = next[j]){k = e[j].v;w = e[j].w;if(dist[i] + w + rdist[k] > dist[N] && dist[i] + w + rdist[k] < ans)ans = dist[i] + w + rdist[k];}}printf("%d\n", ans);}return 0;}


0 0