poj——3255Roadblocks(优先队列 求最小次最短路径)
来源:互联网 发布:mac chili色号 编辑:程序博客网 时间:2024/06/05 05:27
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
#include<iostream>#include<string>#include<cstring>#include<queue>#include<vector>#include<cstdio>using namespace std; struct edge {int cost ;int to;};vector<edge> g[5005];typedef pair<int,int> p;int d1[5005];int d2[5005];int V,E;const int INF=100005;void dijkstra(){fill(d1,d1+V,INF);fill(d2,d2+V,INF);d1[0]=0;priority_queue<p,vector<p>,greater<p> > que;que.push(p(0,0));while(!que.empty()){p now=que.top();que.pop();int k=now.first;int v=now.second;//cout<<k<<" "<<v<<endl;//cout<<d2[v]<<" d2[v]"<<endl;//cout<<g[v].size()<<" size"<<endl;if(d2[v]<k)continue;for(int i=0;i<g[v].size();i++){//cout<<g[v].size()<<" size"<<endl;edge &e=g[v][i];int x=e.cost+k;if(d1[e.to]>x){//cout<<d1[e.to]<<" "<<x<<endl;swap(d1[e.to],x);//cout<<d1[e.to]<<" "<<x<<endl;que.push(p(d1[e.to],e.to));}if(d2[e.to]>x&&x>d1[e.to])//确认x比最小的边要大 并且d2要小于x 寻找次小边 {d2[e.to]=x;que.push(p(d2[e.to],e.to));}}}}int main(){cin>>V>>E;for(int i=0;i<E;i++){int k;int t,c;cin>>k>>t>>c;k=k-1;t=t-1;edge e1,e2;e1.to=t;e2.to=k;e1.cost=c;e2.cost=c; g[k].push_back(e1);g[t].push_back(e2);//由于是无向图 注意双向都可以走 不然会出错 }dijkstra();printf("%d\n",d2[V-1]);return 0;}
- poj——3255Roadblocks(优先队列 求最小次最短路径)
- [POJ](3255)Roadblocks---- 最(次)短路径(邻接表+dijkstra优先队列优化)
- poj 3255 Roadblocks(次最短路径)
- POJ, 3255 Roadblocks(次短路径)
- POJ 2051(最小堆/优先队列)
- POJ 3255 Roadblocks(Dijstra 求次短路长度)
- POJ 3255 Roadblocks (Dijkstra求次短路)
- POJ 3255 Roadblocks(A*求次短路)
- POJ 3255 Roadblocks (次短路径 + Dijkstra算法)
- poj 3255 Roadblocks Dijkstra求次短路
- POJ 3255 Roadblocks 求次短路
- POJ 3255 Roadblocks --次短路径
- POJ——2431(优先队列)
- POJ 3255 Roadblocks(Dijkstra)
- poj 3255 Roadblocks (SPFA)
- 笔记③:POJ Roadblocks 次短路问题代码解析(优先队列逆序排列两种方法)
- 刷题——Roadblocks POJ
- POJ 3614(最小优先队列 + 贪心算法)
- poj——2456Aggressive cows(二分搜索 求牛牛之间最大距离)
- prefuse同时做多个图---解决不连通图的坐标原点
- poj——1064Cable master(二分搜索 求绳子可以满足的最大长度)
- 体育之美
- poj——3169Layout(差分约束 求第一只牛到第n只牛最大距离)
- poj——3255Roadblocks(优先队列 求最小次最短路径)
- ajax异步请求刷新
- poj——1182食物链(并查集 每种动物扮演三种角色)
- python map, reduce, filter 函数
- 来自于PayPal的RESTful API标准
- 【学习Linux】输入输出的重定向,管道
- hdu-1715大菲波数(数组表示大数)
- IBM Bluemix Local new parts deliver additional units of capacity
- how to make React Native ListView When click on row make the row different