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. CF Gym 100187D Holidays (数学,递推)

    题意:给n个元素,从n中选两个非空集合A和B.问有多少中选法? 递推: dp[n]表示元素个数为n的方案数,对于新来的一个元素,要么加入集合,要么不加入集合自成一个集合.加入集合有三种选择,A,B,E ...

  2. CPP-基础:extern"C"

    简介:extern "C" 包含双重含义,从字面上即可得到:首先,被它修饰的目标是“extern”的:其次,被它修饰的目标是“C”的.让我们来详细解读这两重含义. 含义: 1.被e ...

  3. FTP服务器建立windows与Linux的文件共享与读写操作

    centos7搭建vsftpd  2018-11-15 我们有时想要windows与Linux互传文件,就要用到vsftpd了.它仅仅在windows上面操作,就可以实现与Linux的通信,详情如下: ...

  4. c#和Java中的接口

    使用场景: 在c#和Java中: 1.接口可以实现“多继承”(多实现),一个类只能继承自一个父类,但是可以实现多个接口. 2.接口解决了不同类型之间的多态问题,比如鱼与船不是同一类型,但是都能在水里“ ...

  5. sql server 定时备份 脚本

    ) DECLARE @date DATETIME SELECT @date = GETDATE() SELECT @filename = 'G:\backup\NewPlulishSQL-' + CA ...

  6. Windows平台下MySQL常用操作与命令

    Windows平台下MySQL常用操作与命令 Windows平台下MySQL常用操作与命令,学习mysql的朋友可以参考下. 1.导出整个数据库 mysqldump -u 用户名 -p --defau ...

  7. 删除sqlserver管理器登录信息缓存

    在Windows10下测试有效: C:\Users\<user>\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shel ...

  8. JVM内存模型与GC算法(简介)

    JVM内存模型如上图,需要声明一点,这是<Java虚拟机规范(Java SE 7版)>规定的内容,实际区域由各JVM自己实现,所以可能略有不同.以下对各区域进行简短说明. 1.1程序计数器 ...

  9. mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错

    升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...

  10. 解决 cocos2dx iOS/mac 设置纹理寻址模式后纹理变黑的问题

    sprite:getTexture():setTexParameters(gl.LINEAR,gl.LINEAR,gl.REPEAT,gl.REPEAT) 在安卓设备上,设置了纹理自定义寻址模式,纹理 ...