poj 3255 求次大最短路
来源:互联网 发布:linux常用浏览器 编辑:程序博客网 时间:2024/06/06 09:52
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
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
Sample Input
4 41 2 1002 4 2002 3 2503 4 100
Sample Output
450
Hint
Source
题目大意:在一个图上有许多个农场,有个人从1农场出发,到他的朋友n农场去,他不想走一条最短路径,这次他想换条路走,要你帮他找一条次短路径,次短路的定义是,比最短路径长度短(可能有多条),但是不会比其他的路径长度长。而且告诉你数据中一定存在至少一条次短路。
思路 :暴力每一条边
求出从起点到达该边的起点的最短距离 求终点到达该边的终点距离 相加再加上边长 判断是否比最短路长 且最接近最短路
#include<stdio.h>#include<queue>#include<algorithm>#include<vector>#include<string.h>using namespace std;const int INF=10000000;const int maxn=5002;struct Edge{ int from,to,dis; };struct node{ int d,u; bool operator <(const node &rhs) const { return d>rhs.d;//从小到大排列 }};int mn;struct Dij{ int n,m;//点数和边数 vector<Edge>edges;//边列表 vector<int>nd[maxn];//从每个节点出发的边编号 从0开始 bool vis[maxn];//是否参观过 int mmin[maxn];//s到各个点的最小距离 void init(int n) { this->n=n; for(int i=0;i<n;i++) nd[i].clear();//邻接表清空 edges.clear();//清空边表 } void addedge(int from,int to,int dis) //如果是无向图 每条无向边需要调用2次 { edges.push_back((Edge){from,to,dis});//是{ 不是( m=edges.size(); nd[from].push_back(m-1); } void dij(int s)//求s到所有点的距离 { priority_queue<node>que; for(int i=0;i<n;i++) mmin[i]=INF; mmin[s]=0; memset(vis,0,sizeof(vis)); que.push((node){0,s});//注意符号 不是括号 while(!que.empty()) { node x=que.top();que.pop(); int u=x.u; if(vis[u]) continue; vis[u]=true; for(int i=0;i<nd[u].size();i++) { Edge& e=edges[nd[u][i]]; int max1=0,max2=0; if(mmin[e.to]>mmin[u]+e.dis) { // printf("mmin[e.to]=%d\n",mmin[e.to]); max1=mmin[e.to]; mmin[e.to]=mmin[u]+e.dis; max2=mmin[e.to]; if(max1!=INF&&max2!=INF&&mn>max1-max2) mn=max1-max2; que.push((node){mmin[e.to],e.to}); } } } }};vector<int>path;int main(){ int n,m,x,y,z,s,e; while(scanf("%d %d",&n,&m)!=EOF) { mn=999999999; s=0;e=n-1; Dij solve[2]; solve[0].init(n); solve[1].init(n); while(m--) { scanf("%d %d %d",&x,&y,&z); x--;y--; solve[0].addedge(x,y,z);solve[0].addedge(y,x,z); solve[1].addedge(x,y,z);solve[1].addedge(y,x,z); } solve[0].dij(s);//求1到所有点的距离 solve[1].dij(e);//求n到所有点的距离 int ans=INF; for(int i=0;i<solve[0].edges.size();i++) { Edge &edge=solve[0].edges[i]; int st=edge.from,ed=edge.to; int mid=solve[0].mmin[st]+solve[1].mmin[ed]+edge.dis; if(mid>solve[0].mmin[e]) { if(ans>mid) ans=mid; } } printf("%d\n",ans); } return 0;}
参考:
http://blog.csdn.net/wangjian8006/article/details/7992280
- poj 3255 求次大最短路
- poj 3255(次短路)
- poj 3255(次短路)
- 次短路poj 3255
- POJ-3255次短路
- POJ 3255【次短路】
- POJ 3255 (次短路)
- poj 3255 最短路和次短路
- POJ 3255 Roadblocks(次短路)
- POJ 3255 Roadblocks 次短路
- POJ 3255 Roadblocks 次短路
- POJ-3255-次短路问题
- POJ 3255 Roadblocks 次短路
- poj 3255 Roadblocks【次短路】
- POJ 3255 (次短路)
- POJ - 3255 Roadblocks (次短路)
- POJ 3255 Roadblocks 【次短路】
- 【POJ 3255】Roadblocks 【次短路】
- 错误代码中文查询GetLastError返回代码的含义
- UIViewController Class Reference
- Scorm DataModel实现
- 《cocos2d-x权威指南》中第十一章范例(喵星战争)移植到android出现的问题
- 【为你剖析笔记本的内部结构】
- poj 3255 求次大最短路
- AJAX在PHP中的简单使用
- 与vim servername相关的两个指令
- android 加载大图片时报OOM的解决方案
- ARM上的linux如何实现无线网卡的冷插拔和热插拔
- Oracle处理日期时间
- Cinder使用中的注意事项
- 前台与后台数据交互问题
- IMAPI刻录机:多区段(multisession)续刻