博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3255 Roadblocks 次短路
阅读量:5282 次
发布时间:2019-06-14

本文共 3841 字,大约阅读时间需要 12 分钟。

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 41 2 1002 4 2002 3 2503 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 
#include
#include
#include
#include
#define Max 10000#define inf 1<<28using 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*10];struct kdq{ int v,len,next;} edge[200005],reedge[200005];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,0,sizeof(ans)); for(int i=0; i<=n; i++) head[i]=-1,rehead[i]=-1; num=1,renum=1;}int ans1;void spfa(){
//从T开始求出T到所有点的 dis[] int i,j; for(i=1; i<=n; i++) dis[i]=inf; dis[T]=0; visit[T]=1; int num=0,cnt=0; qe[num++]=T; while(num>cnt){ int temp=qe[cnt++]; visit[temp]=0; for(i=rehead[temp]; i!=-1 ; 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]=1; } } } }}int A_star(){ if(S==T) K++; if(dis[S]==inf) return -1; a_star n1; n1.v=S; n1.len=0; priority_queue
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!=-1; i=edge[i].next){ a_star n2; n2.v=edge[i].v; n2.len=edge[i].len+temp.len; q.push(n2); } } return -1;}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=1,T=n,K=2; spfa(); ans1=dis[S]; // printf("%d\n",ans1); printf("%d\n",A_star()); } return 0;}

 

转载于:https://www.cnblogs.com/13224ACMer/p/4857566.html

你可能感兴趣的文章
熟悉项目需求,要知道产品增删修改了哪些内容,才会更快更准确的在该项目入手。...
查看>>
JavaScript 变量
查看>>
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>
C# Stream 和 byte[] 之间的转换
查看>>
OMG: daily scrum nine
查看>>
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>