[第2短路变形]LightOJ 1099 - Not the Best
2012-05-25 01:37
330 查看
1099 - Not the Best
Robin has moved to a small village and sometimes enjoys returning to visit one of his best friends. He does not want to get to his old home too quickly, because he likes the scenery along the way. He has decided to take the second-shortest rather than the
shortest path. He knows there must be some second-shortest path.
The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his friend
(the destination) is at intersection N.
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).
Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe
a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).
PROBLEM SETTER: JANE ALAM JAN
传送门:http://lightoj.com/volume_showproblem.php?problem=1099
题意:对第2短路做了一个新的定义,如果存在多条最短路,那么一条比最短路长,而且比其他所有路径短的路径才是第2短路。
思路:和上一题差不多,直接用A*,然后改变退出条件即可,即 cur.f > dist[src]
代码:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
shortest path. He knows there must be some second-shortest path.
The countryside consists of R bidirectional roads, each linking two of the N intersections, conveniently numbered from 1 to N. Robin starts at intersection 1, and his friend
(the destination) is at intersection N.
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
Input starts with an integer T (≤ 10), denoting the number of test cases.Each case contains two integers N (1 ≤ N ≤ 5000) and R (1 ≤ R ≤ 105). Each of the next R lines contains three space-separated integers: u, v and w that describe
a road that connects intersections u and v and has length w (1 ≤ w ≤ 5000).
Output
For each case, print the case number and the second best shortest path as described above.Sample Input | Output for Sample Input |
2 3 3 1 2 100 2 3 200 1 3 50 4 4 1 2 100 2 4 200 2 3 250 3 4 100 | Case 1: 150 Case 2: 450 |
传送门:http://lightoj.com/volume_showproblem.php?problem=1099
题意:对第2短路做了一个新的定义,如果存在多条最短路,那么一条比最短路长,而且比其他所有路径短的路径才是第2短路。
思路:和上一题差不多,直接用A*,然后改变退出条件即可,即 cur.f > dist[src]
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> #include<algorithm> using namespace std; const int MAXN = 5555,MAXM = 111111; int inQ[MAXN],dist[MAXN],t,n,m,head[MAXN],ecnt; struct edge{ int v,w,nxt; edge(){nxt=-1;} edge(int vv,int ww,int nx):v(vv),w(ww),nxt(nx){} }next[MAXM<<1]; void init(){ memset(inQ,0,sizeof(inQ)); fill(dist,dist+MAXN,0x3fffffff); memset(head,-1,sizeof(head)); ecnt = 0; } void add(int u,int v,int w){ next[ecnt++] = edge(v,w,-1); next[ecnt++] = edge(u,w,-1); next[ecnt-2].nxt = head[u]; head[u] = ecnt-2; next[ecnt-1].nxt = head[v]; head[v] = ecnt-1; } struct node{ int f,g,v; node(){} node(int ff,int gg,int vv):f(ff),g(gg),v(vv){} bool operator < (const node &n) const{ return n.f < f; } }; void spfa(int src){ queue<int> Q; Q.push(src); inQ[src] = 1; dist[src] = 0; while(!Q.empty()){ int now = Q.front(); Q.pop();inQ[now]=0; for(int i=head[now];i!=-1;i=next[i].nxt){ if(dist[next[i].v]>dist[now]+next[i].w){ dist[next[i].v] = dist[now]+next[i].w; if(!inQ[next[i].v]){ inQ[next[i].v]=1; Q.push(next[i].v); } } } } } int Astar(int src){ priority_queue<node> PQ; memset(inQ,0,sizeof(inQ)); PQ.push(node(dist[src],0,src)); while(!PQ.empty()){ node cur = PQ.top(); PQ.pop();inQ[cur.v]++; //cout<<cur.f<<endl; if(cur.f>dist[src]){ return cur.f; } for(int i=head[cur.v];i!=-1;i=next[i].nxt){ node expand(cur.g+dist[next[i].v]+next[i].w,cur.g+next[i].w,next[i].v); PQ.push(expand); } } return -1; } int main(){ scanf("%d",&t); for(int cas=1;cas<=t;cas++){ scanf("%d%d",&n,&m); init(); while(m--){ int a,b,c; scanf("%d%d%d",&a,&b,&c);a--,b--; add(a,b,c); } spfa(n-1); printf("Case %d: %d\n",cas,Astar(0)); } return 0; }
相关文章推荐
- lightoj 1099 - Not the Best(次短路)
- LightOJ 1099 - Not the Best(Dijkstra次短路)
- lightoj 1099 - Not the Best (次短路)
- LightOJ - 1099 Not the Best(次短路)
- 【最短路】LightOJ 1099 - Not the Best 第二短路
- LightOj 1099 Not the Best(次短路)
- (intermediate) 最短路(第k短路) UVA 10740 - Not the Best
- uva 10740 Not the Best (最短路 A*算法)
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- UVA-10740 - Not the Best (K短路)
- uva_10740_Not the best(第k短路)
- uva 10740 - Not the Best(第k短路)
- poj 2449Remmarguts' Date uvaoj 10740 not the best dijkstra或spfa或bellman-ford k短路 A*
- LightOJ1002---Country Roads (最短路变形)
- hdu 2680 Choose the best route(最短路)
- HDOJ 2680 Choose the best route(最短路--dijkstra)
- LightOJ-1099-spaf,次最短路
- HDU2680 - Choose the best route - 最短路(多起点集合)
- HDU2680 Choose the best route(最短路)
- HDU 2680Choose the best route(dijk最短路)