POJ 3255(次短路+SPFA)
来源:互联网 发布:苹果6s怎么设置4g网络 编辑:程序博客网 时间:2024/06/05 10:19
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<cstdio>#include<cstring>#include<string>#include<algorithm>#include<vector>#include<queue>using namespace std;int n,m;int d1[5005];int d2[5005];struct node{ int x,y,w;};struct Node{ int v,w;};vector<Node> vec[5005];void spfa(int flag,int cur){ queue<int> q; q.push(cur); int temp[5005]; memset(temp,0x3f3f3f3f,sizeof(temp)); int id[5005]; memset(id,0,sizeof(id)); temp[cur]=0; id[cur]=1; while(q.empty()==0){ int u=q.front(); int len=vec[u].size(); q.pop(); id[u]--; for(int i=0;i<len;i++){ int v=vec[u][i].v; if(temp[v]>temp[u]+vec[u][i].w){ temp[v]=temp[u]+vec[u][i].w; if(id[v]==0){ id[v]++; q.push(v); } } } } if(flag==1){ for(int i=1;i<=n;i++){ d1[i]=temp[i]; } } else if(flag==2){ for(int i=1;i<=n;i++){ d2[i]=temp[i]; } }}int main(){ //freopen("test.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ int u,v,w;; node a[100005]; for(int i=1;i<=m;i++){ Node temp; scanf("%d%d%d",&u,&v,&w); temp.v=v; temp.w=w; vec[u].push_back(temp); temp.v=u; vec[v].push_back(temp); a[i].x=u; a[i].y=v; a[i].w=w; } memset(d1,0x3f3f3f3f,sizeof(d1)); memset(d2,0x3f3f3f3f,sizeof(d2)); spfa(1,1); spfa(2,n); int ans=0x3f3f3f3f; //printf("%d\n",d2[1]); for(int i=1;i<=m;i++){ int u=a[i].x; int v=a[i].y; int w=a[i].w; //printf("%d %d %d\n",u,v,w); if(d1[u]+d2[v]+w>d1[n]){ ans=min(ans,d1[u]+d2[v]+w); } if(d2[u]+d1[v]+w>d1[n]){ ans=min(ans,d2[u]+d1[v]+w); } } printf("%d\n",ans); } return 0;}
- POJ 3255(次短路+SPFA)
- poj 3255 Roadblocks(无向图次短路 SPFA)
- poj 3255 Roadblocks(spfa次短路 || Dijsktra)
- POJ 3255 Roadblocks 次短路 SPFA
- poj 3255 spfa(求次短路)
- POJ 3255 Roadblock(SPFA寻找次短路)
- poj 3255 次短路(第k短路) A* + spfa 或 dijkstra
- poj 3255 Roadblocks 次短路 spfa 解题报告
- POJ 3255 (次短路)
- nyoj1006(最短路次短路spfa)
- poj 3255(次短路)
- poj 3255(次短路)
- 次短路poj 3255
- POJ-3255次短路
- POJ 3255【次短路】
- POJ 3255 (次短路)
- Roadblocks (SPFA次短路求法)
- Poj 3255(Dijkstra求次短路)
- C++用双链表检测回文数(palindrome number)
- 设计模式初探-中介者模式
- C++类中变量也可以是引用哈------要用初始化列表来初始化
- hdu 6050 Funny Function
- HDU6070 Dirt Ratio(线段树+二分)
- POJ 3255(次短路+SPFA)
- mybatis generator插件自定义修改
- 安卓代码中常用的代码以及问题收集
- hdu 6060 RXD and dividing (dfs)
- 机器学习(1)-分类
- JS 实现checkbox 全选 全不选
- leedcode前50道题总结
- 1js动态拼接表格,2获取表格内属性,并将table表格元素封装成json,3后台将json转成list
- 1机器学习概况及学习MAP