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. python3操作excel02(对excel的基础操作,进行简单的封装)3

    #!/usr/bin/env python# -*- coding:UTF-8 -*- import requestsfrom bs4 import BeautifulSoupfrom bs4 imp ...

  2. Android(java)学习笔记126:判断SD卡状态和SD卡容量

    1. 判断SD卡状态和SD卡存储空间大小 当我们在使用SD卡时候,如果我们想往SD卡里读写数据,我们必须在这之前进行一个逻辑判断,那就是判断SD卡状态和SD存储空间大小: 核心代码: String s ...

  3. ArrayList 源码分析(JDK1.8)

    ArrayList简介  ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess ...

  4. Ball Coloring

    6552: Ball Coloring 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 7[提交][状态][讨论版][命题人:admin] 题目描述 There are N ...

  5. CAD控件的鼠标事件(网页版)

    _DMxDrawXEvents::MouseEvent CAD控件中的鼠标事件. 参数 说明 LONG lType 事件类型,1鼠标移动,2是鼠标左键按下,3是鼠标右键按下,4是鼠标左键双击 5是鼠标 ...

  6. 【转】Popclip的JSON格式化扩展

    http://liuyunclouder.github.io/2016/09/29/JSONizer:Popclip的JSON格式化扩展 作为一个MAC党,不好好利用MAC的神兵利器,简直就是罪过.A ...

  7. Google 出品的 Java 编码规范,强烈推荐,权威又科学!

    原文:google.github.io/styleguide/javaguide.html 译者:Hawstein 来源:hawstein.com/2014/01/20/google-java-sty ...

  8. Python中文编码问题(字符串前面加'u')

    中文编码问题是用中文的程序员经常头大的问题,在python下也是如此,那么应该怎么理解和解决python的编码问题呢? 我们要知道python内部使用的是unicode编码,而外部却要面对千奇百怪的各 ...

  9. mysql5.7.24 解压版安装步骤以及遇到的问题

    1.下载 https://dev.mysql.com/downloads/mysql/ 2.解压到固定位置,如D:\MySQL\mysql-5.7.24 3.添加my.ini文件 跟bin同级 [my ...

  10. VS快捷键总结(开发中经常遇到)

    1.窗口快捷键  (大家有没有发现但凡跟窗口挂上钩的快捷键当中都有一个W,那是因为W代表Windows也就是窗口的意思) Ctrl+W,W: 浏览器窗口 (浏览橱窗用有道的翻译是window shop ...