poj3255 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
Output
Sample Input
4 4
1 2 100
2 4 200
2 3 250
3 4 100
Sample Output
450
Hint
Source
#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 次短路的更多相关文章
- Bzoj 1726: [Usaco2006 Nov]Roadblocks第二短路 dijkstra,堆,A*,次短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 969 Solved: 468[S ...
- BZOJ1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 768 Solved: 369[S ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )
从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ---------------------------------------------------------------- ...
- BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- 1726: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 835 Solved: 398[S ...
- 最短路【bzoj1726】: [Usaco2006 Nov]Roadblocks第二短路
1726: [Usaco2006 Nov]Roadblocks第二短路 Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她 ...
- POJ3255 Roadblocks [Dijkstra,次短路]
题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...
- POJ3255 Roadblocks 【次短路】
Roadblocks Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7760 Accepted: 2848 Descri ...
- POJ3255 Roadblocks 严格次短路
题目大意:求图的严格次短路. 方法1: SPFA,同时求单源最短路径和单源次短路径.站在节点u上放松与其向量的v的次短路径时时,先尝试由u的最短路径放松,再尝试由u的次短路径放松(该两步并非非此即彼) ...
随机推荐
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
- 苹果ATS Win2008 R2 IIS7.5 HTTPS 证书的那些可能遇到的坑
前言:工作这么多年,每一次要弄https 都和苹果有关,上一次是苹果app的企业安装形式,ios7后 .plist 文件必须在一个https路径. 这一次则是苹果的ATS计划,无疑这是在推动网络安全上 ...
- cv2.bilateralFilter 双边滤波
双边滤波bilateralFilter 双边滤波是一种非线性的滤波方法,是结合图像的空间邻近度和像素值相似度的一种折衷处理,同时考虑空间与信息和灰度相似性,达到保边去噪的目的,具有简单.非迭代.局部处 ...
- CPP-网络/通信:经典HTTP协议详解
2008-11-03 09:11 by Hundre, 266688 阅读, 23 评论, 收藏, 编辑 转自:http://blog.csdn.net/gueter/archive/2007/03/ ...
- JQuery EasyUI学习记录(三)
1.jQuery EasyUI messager使用方式 1.1 alert方法 $(function(){ //1.alert方法---提示框 $.messager.alert("标题&q ...
- [].indexOf.call()学习
今天看到闭包一道题,就是一个li列表,点击列表控制台输出对应的索引.这里考察了var的作用域问题和闭包对外部变量的引用问题,有几种解决方法. html: <ul> <li>te ...
- NOIP模拟赛 篮球比赛2
篮球比赛2(basketball2.*) 由于Czhou举行了众多noip模拟赛,也导致放学后篮球比赛次数急剧增加.神牛们身体素质突飞猛进,并且球技不断精进.这引起了体育老师彩哥的注意,为了给校篮球队 ...
- Unity基础-脚本的优化
脚本的优化 object pool 避免频繁的内存分配和gc噩梦(字符串相加?) 是否有必要都写在update里?分帧? 需要的只取一次 使用editor内赋值,而不是find 复杂的物理 复杂的数学 ...
- 拼接Python字符串最常见的六种方式
最常见的六种方式拼接Python字符串 字符串是所有编程语言中都有的基本变量的类型,程序员基本每天都在和字符串打交道. 每种字符串拼接方式的使用场景各不相同,我们可以在开发过程中灵活运用. 一.用逗号 ...
- STM32CUBEMX入门学习笔记1:软件的简单介绍
STM32CUBEMX是ST公司设计的一款免费软件,软件可以通过其官网下载.现在已经下载到.通过STM32CUBEMX可以完成从单片机选型,程序初始化,中断配置……工作.并生成对应的"HAL ...