POJ 3255 Roadblocks (次短路 SPFA )
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
Line 1: Two space-separated integers: N and R
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
Line 1: The length of the second shortest path between node 1 and node N
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Two routes: 1 -> 2 -> 4 (length 100+200=300) and 1 -> 2 -> 3 -> 4 (length 100+250+100=450)
分析:
次短路不可能与最短路完全重合,那么就一定会有一段比较绕路。绕路的地方不可能超过两处,那样就有一条路短于这条路而长于最短路,矛盾。 因此可以对所有的边加以枚举。
首先用Dijkstra或SPFA以原点和终点为源分别做一次单源最短路,并把答案存在dist_0和dist_n两个数组中。那么,对于任何一条边(i, j),
下面的二者之一就有可能是次短路的长:
dist_0[i] + len(i, j) + dist_n[j] 和 dist_0[j] + len(i, j) + dist_n[i]
注意,如果其中一个的长度等于最短路的长度(即dist_n[0]),就一定不能选,因为这违反次短路的定义。两个都要枚举,因为可能有其中一个等于最短路的长,如果只取较小的值另外一个就废掉了。
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
const int INF=0x3f3f3f3f;
using namespace std;
int n,m,cnt;
struct Node
{
int to,val;
int next;
} node[200100];
int head[5009],vis[5009];//寻找头结点,在找最短路的时候这个点有没有访问过
int dis_1[5009],dis_n[5009];//到1号节点的最短路,到n号节点的最短路
void add(int a,int b,int w)//将边的信息采用头插法保存下来
{
node[cnt].to=b;
node[cnt].val=w;
node[cnt].next=head[a];
head[a]=cnt;
cnt++;
}
void spfa(int st,int dis[])//spfa求最短路的模板
{
memset(vis,0,sizeof(vis));
queue<int>q;
dis[st]=0;
vis[st]=1;
q.push(st);
while(!q.empty())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=node[i].next)
{
int y=node[i].to;
if(dis[y]>dis[x]+node[i].val)
{
dis[y]=dis[x]+node[i].val;
if(vis[y]==0)
{
vis[y]=1;
q.push(y);
}
}
}
}
}
int main()
{
int a,b,w;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<=n;i++)//初始化
{
head[i]=-1;
dis_1[i]=dis_n[i]=INF;
}
cnt=0;//代表边数
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&w);
add(a,b,w);
add(b,a,w);
}
spfa(1,dis_1);
spfa(n,dis_n);
int ans=INF;
//一条边一条边的通过绕路寻找次短路
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=node[j].next)
{
b=node[j].to;
w=node[j].val;
int temp=dis_1[i]+w+dis_n[b];
if(temp>dis_1[n]&&ans>temp)
{
ans=temp;
}
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 3255 Roadblocks (次短路 SPFA )的更多相关文章
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- poj - 3225 Roadblocks(次短路)
http://poj.org/problem?id=3255 bessie 有时会去拜访她的朋友,但是她不想走最快回家的那条路,而是想走一条比最短的路长的次短路. 城镇由R条双向路组成,有N个路口.标 ...
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- poj 3255 Roadblocks
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13216 Accepted: 4660 Descripti ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
随机推荐
- go 面试题总结
1.什么是goroutine,他与process, thread有什么区别? 2. 什么是channel,为什么它可以做到线程安全? 3. 了解读写锁吗,原理是什么样的,为什么可以做到? 4. 如何用 ...
- Qt之美(一):d指针/p指针详解(解释二进制兼容,以及没有D指针就会崩溃的例子。有了D指针,所使用的对象大小永远不会改变,它就是该指针的大小。这个指针就被称作D指针)good
Translated by mznewfacer 2011.11.16 首先,看了Xizhi Zhu 的这篇Qt之美(一):D指针/私有实现,对于很多批评不美的同路人,暂且不去评论,只是想支持 ...
- 半夜思考之查漏补缺, Spring 中的容器后处理器
之前学 Spring 的时候 , 还没听过容器后处理器 , 但是一旦写出来 , 就会觉得似曾相识 . 容器配置器通常用于对 Spring 容器进行处理 , 并且总是在容器实例化任何其他 Bean 之前 ...
- 【BZOJ 3652】大新闻 数位dp+期望概率dp
并不难,只是和期望概率dp结合了一下.稍作推断就可以发现加密与不加密是两个互相独立的问题,这个时候我们分开算就好了.对于加密,我们按位统计和就好了;对于不加密,我们先假设所有数都找到了他能找到的最好的 ...
- BZOJ 3527 力 | FFT
BZOJ 3527 力 | 分治 题意 给出数组q,$E_i = \sum_{i < j} \frac{q_i}{(i - j) ^ 2} - \sum_{i > j} \frac{q_i ...
- Treat wchar_t as built-in type不一致导致的链接错误
今天用VS2013新建了一个工程,生成时出现很多怪异的链接错误,比如: error LNK2019: unresolved external symbol "__declspec(dllim ...
- Mininet 系列实验(六)
写在前面 这次实验遇到了非常多问题,非常非常多,花了很多时间去解决,还是有一些小问题没有解决,但是基本上能完成实验.建议先看完全文再开始做实验. 实验内容 先看一下本次实验的拓扑图: 在该环境下,假设 ...
- 【hdu3555】 Bomb
http://acm.hdu.edu.cn/showproblem.php?pid=3555 (题目链接) 题意 求区间${[1,n]}$含有49的数的个数. Solution 数位dp,先求出不含4 ...
- Java之File与递归
File类的使用和递归思想 File类 概述 文件: 存储数据 文件夹: 管理文件与文件夹 构造方法 public File(String pathname) :通过将给定的路径名字符串转换为抽象路径 ...
- 如何修改Windows程序的权限?
修改程序的权限需要用到3个函数: 1. 获取进程的令牌句柄: OpenProcessToken 2. 查找特权类型的ID: LookupPrivilegeValue 3. 修改进程的特权:Adjust ...