poj 3255 Roadblocks
Roadblocks
Time Limit: 2000MS |
Memory Limit: 65536K |
|
Total Submissions: 13216 |
Accepted: 4660 |
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
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)
Source
题意:
给出n个点,m条双向边,求严格次短路。
AC代码:
#include<cstdio>
#include<cstring>
#include<queue>
#define R register
using namespace std;
inline int read(){
R int x=;bool f=;
R char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
}
const int N=1e5+;
struct node{
int u,v,w,next;
}e[N<<];
int n,m,tot,head[N],dis1[N],dis2[N];
bool vis[N];
void add(int x,int y,int z){
e[++tot].u=x;
e[tot].v=y;
e[tot].w=z;
e[tot].next=head[x];
head[x]=tot;
}
void spfa1(int S){
queue<int>q;
memset(vis,,sizeof vis);
memset(dis1,/,sizeof dis1);
q.push(S);
dis1[S]=;vis[S]=;
while(!q.empty()){
int x=q.front();q.pop();
vis[x]=;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(dis1[v]>dis1[x]+w){
dis1[v]=dis1[x]+w;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
}
void spfa2(int S){
queue<int>q;
memset(vis,,sizeof vis);
memset(dis2,/,sizeof dis2);
q.push(S);
dis2[S]=;vis[S]=;
while(!q.empty()){
int x=q.front();q.pop();
vis[x]=;
for(int i=head[x];i;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(dis2[v]>dis2[x]+w){
dis2[v]=dis2[x]+w;
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
}
void Cl(){
tot=;
memset(e,,sizeof e);
memset(head,,sizeof head);
}
void work(){
Cl();
for(int i=,x,y,z;i<=m;i++){
x=read();y=read();z=read();
add(x,y,z);
add(y,x,z);
}
spfa1();
spfa2(n);
int shortest=dis1[n],shorter=0x7fffffff;
for(int i=;i<=m*;i++){
int len=dis1[e[i].u]+dis2[e[i].v]+e[i].w;
if(len>shortest&&len<shorter) shorter=len;
}
printf("%d\n",shorter);
}
int main(){
while(scanf("%d%d",&n,&m)==) work();
return ;
}
poj 3255 Roadblocks的更多相关文章
- POJ 3255 Roadblocks(A*求次短路)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12167 Accepted: 4300 Descr ...
- POJ 3255 Roadblocks (次级短路问题)
解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...
- POJ 3255 Roadblocks (次短路模板)
Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS Memory Limit: 65536K Descriptio ...
- 次最短路径 POJ 3255 Roadblocks
http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...
- poj 3255 Roadblocks 次短路(两次dijksta)
Roadblocks Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total S ...
- POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16425 Accepted: 5797 Descr ...
- POJ 3255 Roadblocks --次短路径
由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- POJ 3255 Roadblocks (次短路)
题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...
随机推荐
- Android对话框
这周过的实在是艰辛,自打这周二起我的本本就开始闹"罢工",最后还是重装系统了事. . . 只是可怜了我的那些被格了的软件(悲伤辣么大)! 往事不要再提,人生几度风雨... 简 ...
- static和extern对函数的作用
- C语言中的循环结构与选择结构
1. 为什么使用循环? 重复执行某段代码 2. while(条件){ 循环体: } 当条件成立的时候就执行循环体,条件不成立,就退出循环,继续执行while后面的语句 3. for ( 初始表达式 : ...
- Team Leader炖完石头汤后干嘛
在万众创业的互联网年代,挖人组建全明星团队过于奢侈.面对水平参差不齐的团队咋办? 命运真是捉弄,半年前在大美团打工时准备做个NABC的教学项目 ,结果自己就被挖到"Competitors 竞 ...
- 从MVC框架看MVC架构的设计
尽管MVC早已不是什么新鲜话题了,但是从近些年一些优秀MVC框架的设计上,我们还是会发现MVC在架构设计上的一些新亮点.本文将对传统MVC架构中的一些弊病进行解读,了解一些优秀MVC框架是如何化解这些 ...
- 设计模式C#实现(十一)——组合模式
意图 0 适用性 1 结构 2 实现 3 效果 4 意图 将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 适用性 你想表示对象的部 ...
- 烂泥:rsync配置文件详解
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 对于rsync服务器来说,最重要和复杂的就是它的配置了.rsync服务器的配置文件为/etc/rsyncd.conf,其控制认证.访问.日志记录等等. ...
- MySQL的Explain命令
Explain命令是查看查询优化器如何决定执行查询的主要办法. 调用 EXPLAIN 要使用EXPLAIN,只需在查询中的SELECT关键字之前增加EXPLAIN.MySQL会在查询上设置一个 ...
- 七、Android学习第六天——SQLite与文件下载(转)
(转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 七.Android学习第六天——SQLite与文件下载 SQLite SQ ...
- DirectX API 编程起步 #01 项目设置
=========================================================== 目录: DirectX API 编程起步 #02 窗口的诞生 DirectX A ...