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: AB,
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

USACO 2006 November Gold

题意:

给出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的更多相关文章

  1. POJ 3255 Roadblocks(A*求次短路)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12167   Accepted: 4300 Descr ...

  2. POJ 3255 Roadblocks (次级短路问题)

    解决方案有许多美丽的地方.让我们跳回到到达终点跳回(例如有两点)....无论如何,这不是最短路,但它并不重要.算法能给出正确的结果 思考:而最短的路到同一点例程.spfa先正达恳求一次,求的最短路径的 ...

  3. POJ 3255 Roadblocks (次短路模板)

    Roadblocks http://poj.org/problem?id=3255 Time Limit: 2000MS   Memory Limit: 65536K       Descriptio ...

  4. 次最短路径 POJ 3255 Roadblocks

    http://poj.org/problem?id=3255 这道题还是有点难度 要对最短路径的算法非常的了解 明晰 那么做适当的修改 就可以 关键之处 次短的路径: 设u 到 v的边权重为cost ...

  5. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  6. POJ 3255 Roadblocks (Dijkstra求最短路径的变形)(Dijkstra求次短路径)

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16425   Accepted: 5797 Descr ...

  7. POJ 3255 Roadblocks --次短路径

    由于次短路一定存在,则可知次短路一定是最短路中某一条边不走,然后回到最短路,而且只是一条边,两条边以上不走的话,就一定不会是次短路了(即以边换边才能使最小).所以可以枚举每一条边,算出从起点到这条边起 ...

  8. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  9. POJ 3255 Roadblocks (次短路)

    题意:给定一个图,求一条1-n的次短路. 析:次短路就是最短路再长一点呗,我们可以和求最短路一样,再多维护一个数组,来记录次短路. 代码如下: #pragma comment(linker, &quo ...

随机推荐

  1. 关于插件管理器Alcatraz

    如何安装插件管理器Alcatraz:去github下载一个Alcatraz安装包,然后运行一下. 会弹出 记得选择左边的Load Bundle 退出Xcode 重新运行一下就OK 了. 然后就可以看到 ...

  2. js 事件处理程序 事件对象

    事件:用户或浏览器自身执行的动作: 事件处理程序:响应某个事件的函数: 事件流:从页面中接收事件的顺序. 1.DOM事件流 "DOM2级事件"规定的事件流包括三个阶段:事件捕获阶段 ...

  3. Java中用内存映射处理大文件

    在处理大文件时,如果利用普通的FileInputStream 或者FileOutputStream 抑或RandomAccessFile 来进行频繁的读写操作,都将导致进程因频繁读写外存而降低速度.如 ...

  4. C#程序猿电脑重装记录

    最近比较空了,闲的手痒,将自己的笔记本进行了重装,之前每次重装都没有记录,这次将本次重装过程记录下来,以便下次参考 1 首先不用说了WIN7旗舰版装好,驱动装好 2 开启Administrator用户 ...

  5. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 co

    package com.hanqi.test; public class Animal { private String name; private int legs; private int cou ...

  6. java -jar 执行 eclipse export 的 jar 包报错处理

    1. 错误1:打 jar 包执行,报错,找不到 类库的 jar 包 F:\>java -jar remoteLogin.jarException in thread "AWT-Even ...

  7. Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件

    最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...

  8. MySQL调优系列基础篇

    前言 有一段时间没有写博客了,整天都在忙,上班,录制课程,恰巧最近一段时间比较清闲,打算弄弄MYSQL数据库. 关于MySQL数据库,这里就不做过多的介绍,开源.免费等特性深受各个互联网行业喜爱,尤其 ...

  9. 根据网站所做的SEO优化整理的一份文档

    今日给合作公司讲解本公司网站SEO优化整理的一份简单文档 架构 ########################################## 1.尽量避免Javascript和flash导航. ...

  10. Android 横竖屏切换小结

    (自己体会:每次横竖屏自动切时都会run Activity的onCreate,即相当后重新进入Activity初始化一样:) 转自:http://www.cnblogs.com/franksunny/ ...