Roadblocks
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10098   Accepted: 3620

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)

Source

套的A*模板,注意最短路可能不止一条
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define Max 10000
#define inf 1<<28
using namespace std;
int S,T,K,n,m;
int head[Max],rehead[Max];
int num,renum;
int dis[Max];
bool visit[Max];
int ans[Max];
int qe[Max*];
struct kdq{
int v,len,next;
} edge[],reedge[]; struct a_star { //A*搜索时的优先级队列;
int v;
int len;
bool operator<(const a_star &a)const{ //f(i)=d[i]+g[i]
return len+dis[v]>a.len+dis[a.v];
}
};
void insert(int u,int v,int len){//正图和逆图
edge[num].v=v;
edge[num].len=len;
edge[num].next=head[u];
head[u]=num;
num++;
reedge[renum].v=u;
reedge[renum].len=len;
reedge[renum].next=rehead[v];
rehead[v]=renum;
renum++;
} void init(){
memset(ans,,sizeof(ans));
for(int i=; i<=n; i++)
head[i]=-,rehead[i]=-;
num=,renum=;
}
int ans1;
void spfa(){//从T开始求出T到所有点的 dis[]
int i,j;
for(i=; i<=n; i++)
dis[i]=inf;
dis[T]=;
visit[T]=;
int num=,cnt=;
qe[num++]=T;
while(num>cnt){
int temp=qe[cnt++];
visit[temp]=;
for(i=rehead[temp]; i!=- ; i=reedge[i].next){
int tt=reedge[i].v;
int ttt=reedge[i].len;
if(dis[tt]>dis[temp]+ttt)
{
dis[tt]=dis[temp]+ttt;
if(!visit[tt])
{
qe[num++]=tt;
visit[tt]=;
}
}
}
}
}
int A_star(){
if(S==T)
K++;
if(dis[S]==inf)
return -;
a_star n1;
n1.v=S;
n1.len=;
priority_queue <a_star> q;
q.push(n1);
while(!q.empty()){
a_star temp=q.top();
q.pop();
ans[temp.v]++;
if(ans[T]==K){//当第K次取到T的时候,输出路程
if(temp.len==ans1)
K++;
else
return temp.len;
}
if(ans[temp.v]>K)
continue;
for(int i=head[temp.v]; i!=-; i=edge[i].next){
a_star n2;
n2.v=edge[i].v;
n2.len=edge[i].len+temp.len;
q.push(n2);
}
}
return -;
}
int main(){
int i,j,k,l;
int a,b,s;
while(scanf("%d%d",&n,&m)!=EOF){
init();
while(m--){
scanf("%d%d%d",&a,&b,&s);
insert(a,b,s);
insert(b,a,s);
}
S=,T=n,K=;
spfa();
ans1=dis[S];
// printf("%d\n",ans1);
printf("%d\n",A_star());
}
return ;
}

poj3255 Roadblocks 次短路的更多相关文章

  1. Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 969  Solved: 468[S ...

  2. BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 768  Solved: 369[S ...

  3. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

    从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...

  4. BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  5. 1726: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[S ...

  6. 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路

    1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...

  7. POJ3255 Roadblocks [Dijkstra,次短路]

    题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...

  8. POJ3255 Roadblocks 【次短路】

    Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7760   Accepted: 2848 Descri ...

  9. POJ3255 Roadblocks 严格次短路

    题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...

随机推荐

  1. python+selenium之中类/函数/模块的简单介绍和方法调用

    # coding=utf-8 class ClassA (object): string1 = "这是一个字符串." def instancefunc(self): print ( ...

  2. 11gR2 如何诊断节点重启问题

    本文对如何诊断11gR2 GI环境下的节点重启问题进行了一些介绍. 首先,像10g版本一样,我们首先介绍在GI中能够导致节点重启的进程.1.Ocssd.bin:这个进程的功能和10g版本的功能基本差不 ...

  3. CoreData介绍

    http://blog.csdn.net/zh952016281/article/details/52105683 写在前面 在CoreData中有一些常用的类,称呼可能各不相同.所以这里先约定一些关 ...

  4. CentOS安装RabbitMQ步骤

    1.安装gcc yum install gcc 安装 ncurses-devel yum install ncurses-devel 2.安装erlang 下载安装包 http://www.erlan ...

  5. Bootstrap历练实例:嵌套的媒体对象

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  6. LLDB详解

    LLDB的Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍 L ...

  7. Windows 10 建立wifi热点

    如果当前是台式机那么需要一个usb的无线网卡,这里要注意如果你是使用台式机并且通过有线的方式上网,但是你的无线网卡适配器不能在禁用状态. 这里首先打开[运行]输入cmd,打开cmd(注意,这里要使用管 ...

  8. (68)zabbix windows性能计数器使用详解

    概述 windows下的性能计数器让zabbix监控更加轻松,直接获取性能计数器的数值即可完成windows监控.性能计数器如下:   1 perf_counter["\Processor( ...

  9. centos里没有pip命令怎么办?

    刚刚入门python的嘛,然后这个centos也是不是那么熟悉!! pip在centos也没有,所以网上找来资料,3条语句就搞定啦! 1.查看是否安装依赖包,没安装先安装: yum install e ...

  10. 如何使用jmeter做接口测试

    1.传参:key=value形式 2.传参:json格式 3.jmeter上传文件 4.jmeter传cookie 或者使用 HTTP Cookie管理器