POJ
来源:互联网 发布:阿里php招聘 编辑:程序博客网 时间:2024/06/06 16:47
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 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
Lines 2..R+1: Each line contains three space-separated integers: A, B, and D that describe a road that connects intersections A and B and has length D (1 ≤ D ≤ 5000)
Output
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
Source
题意:求1到n的次短路
保留每一个节点的最短路和次短路,最后输出次短路的结果
#include <iostream>#include <string.h>#include <stdio.h>#include <queue>#include <utility>using namespace std;typedef pair<int, int> pii;const int N = 2e5+10;const int INF = 0x3f3f3f3f;int n,m,x,y,z,dis[N][2];int ft[N],nt[N],u[N],cost[N],sz;struct cmp{ bool operator () (pii a,pii b){ return a.second > b.second; }};void dij(){ priority_queue<pii,vector<pii>,cmp>q; memset(dis,INF,sizeof dis); dis[1][0] = 0; q.push(make_pair(1, 0)); while(!q.empty()){ pii st = q.top(); q.pop(); int d = st.second, now = st.first; if(d>dis[now][1]) continue; for(int i=ft[now];;i=nt[i]){ int dist = d + cost[i]; if(dist<dis[u[i]][0]){ int temp = dis[u[i]][0]; dis[u[i]][0] = dist; dis[u[i]][1] = temp; q.push(make_pair(u[i], dist)); }else if(dist<dis[u[i]][1]&&dist!=dis[u[i]][0]){ dis[u[i]][1] = dist; q.push(make_pair(u[i], dist)); } if(nt[i]==-1) break; } }}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ sz = 0; memset(ft,-1,sizeof ft); for(int i=0;i<m;i++){ scanf("%d%d%d",&x,&y,&z); u[sz] = y; cost[sz] = z; nt[sz] = ft[x]; ft[x] = sz++; u[sz] = x; cost[sz] = z; nt[sz] = ft[y]; ft[y] = sz++; } dij(); printf("%d\n",dis[n][1]); } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- Android性能优化之提高ListView性能的技巧
- 判断一个数是不是2的整数次幂(两种方法)
- Struts2学习笔记(6)-异常处理
- 文件类型与权限
- shell排序法-改良的插入排序(C)
- POJ
- sorted
- 通讯录草稿
- ZOJ2165 Red and Black(dfs)
- Android:ListView之ViewHolder
- Android之:了解MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST)
- 关于SSH整合的那些事
- 按位操作的应用
- 总结-2017/11/24