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. BUG严重级别定义及注意事项

  2. 挂sqlserver计划,系统自动分配拣货任务

    USE [P2WMS_WH43] GO /****** Object: StoredProcedure [dbo].[sp_fru_CalcAllocatePickData] Script Date: ...

  3. BFS 简单思想以及代码

    BFS(广搜思想) 广度优先搜索 广度优先搜索是图论的搜索算法之一,以下便进行简单叙述 对于每一个顶点来说,都存在着三种颜色 白色,灰色,黑色 而对于每个顶点,都有三种数据类型 颜色类型,前驱或者父节 ...

  4. Android颜色选择器介绍

    使用Android的颜色选择器可以让我们的view在不同状态下显示不同的颜色. 1.Android中ListView 选择某项改变该行字体颜色 2.文件位置 res/color/filename.xm ...

  5. SC || Chapter 1

    第一章的重中之重就是这张图吧 (具体参见笔记) ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 区分哪些属性是外部的(面向用户 ...

  6. Ubuntu编译Android源码过程中的空间不足解决方法

    Android源码一般几十G,就拿Android5.0来说,下载下来大概也有44G左右,和编译产生的文件以及Ubuntu系统占用的空间加起来,源码双倍的空间都不够有.编译源码前能分配足够的空间再好不过 ...

  7. x86,x64,i386,i686

    x64其实就是64位, x86其实就是32位. 1. i386 适用于intel和AMD所有32位的cpu.以及via采用X86架构的32的cpu. intel平台包括8086,80286,80386 ...

  8. ps基础实例

    一:合并多个图片 1.先新件一个图片)CTRL+N),大小定成你想要的大小 2.把你要放入的照片用PS打开 3.把放入的照片用移动工具(V)拉到新件的图片里面 4.用CTRL+T调整大小(按住SHIF ...

  9. *运算和&运算

    /* &:取地址运算符 *:指针运算符(或称为间接运算符),取指针所指向的对象的内容 */ int a,b; int *pointer_1, *pointer_2; pointer_1 = & ...

  10. JavaScript递归实现对象深拷贝

    let personOne = { name:"张三", age:18, sex:"male", children:{ first:{ name:"z ...