博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2449:第k短路问题
阅读量:5097 次
发布时间:2019-06-13

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

Description

"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he told them a story. 
"Prince Remmarguts lives in his kingdom UDF – United Delta of Freedom. One day their neighboring country sent them Princess Uyuw on a diplomatic mission." 
"Erenow, the princess sent Remmarguts a letter, informing him that she would come to the hall and hold commercial talks with UDF if and only if the prince go and meet her via the K-th shortest path. (in fact, Uyuw does not want to come at all)" 
Being interested in the trade development and such a lovely girl, Prince Remmarguts really became enamored. He needs you - the prime minister's help! 
DETAILS: UDF's capital consists of N stations. The hall is numbered S, while the station numbered T denotes prince' current place. M muddy directed sideways connect some of the stations. Remmarguts' path to welcome the princess might include the same station twice or more than twice, even it is the station with number S or T. Different paths with same length will be considered disparate. 

Input

The first line contains two integer numbers N and M (1 <= N <= 1000, 0 <= M <= 100000). Stations are numbered from 1 to N. Each of the following M lines contains three integer numbers A, B and T (1 <= A, B <= N, 1 <= T <= 100). It shows that there is a directed sideway from A-th station to B-th station with time T. 
The last line consists of three integer numbers S, T and K (1 <= S, T <= N, 1 <= K <= 1000).

Output

A single line consisting of a single integer number: the length (time required) to welcome Princess Uyuw using the K-th shortest path. If K-th shortest path does not exist, you should output "-1" (without quotes) instead.

Sample Input

2 21 2 52 1 41 2 2

Sample Output

14 题解 题目就是一道裸的第k短路。。。 dij中做A* 该点到终点最小距离作为估价函数,dij的时候每次挑最小的f=g+h出来拓展,g就是起点到该点的最小距离。 要注意一下,如果终点和起点相同,那么k++,因为距离为0不算是最短的路。
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 #define maxn 1005 7 #define maxm 100005 8 #define inf 1<<29 9 int s,t,k,n,cnt[maxn];10 int head[maxn],ecnt,headc[maxn],vis[maxn],d[maxn];11 struct edge{12 int u,v,next,w;13 }E[maxm],Ec[maxm];14 struct Node{15 int g,v,f;16 Node(int x,int y,int z):f(x),g(y),v(z){}17 bool operator < (const Node& a) const{
return a.f
q;36 for(int i=1 ; i<=n ; ++i)37 d[i]=inf;38 d[x]=0;39 vis[x]=1;40 q.push(x);41 while(!q.empty())42 {43 int dd=q.front();q.pop();44 vis[dd]=0;45 for(int i=headc[dd] ; i ; i=Ec[i].next )46 {47 int v=Ec[i].v;48 int val=Ec[i].w;49 if(d[v]>d[dd]+val)50 {51 d[v]=d[dd]+val;52 if(!vis[v])53 {54 vis[v]=1;55 q.push(v);56 }57 }58 }59 }60 }61 int A_star(int x)62 {63 if(d[x]==inf)return -1;64 if(s==t)k++;65 priority_queue
que;66 que.push(Node(d[x],0,x));67 Node next(0,0,0);68 while(!que.empty())69 {70 Node now=que.top();que.pop();71 int dd=now.v;72 cnt[dd]++;73 if(cnt[t]==k)return now.g ;74 if(cnt[dd]>k)continue; 75 for(int i=head[dd] ; i ; i=E[i].next )76 {77 if(d[E[i].v]==inf)continue;78 next.v=E[i].v;79 next.g=now.g+E[i].w;80 next.f=d[next.v]+next.g; 81 que.push(next);82 }83 }84 return -1;85 }86 int main()87 {88 int m,u,v,w;89 scanf("%d%d",&n,&m);90 for(int i=1 ; i<=m ; ++i )91 {92 scanf("%d%d%d",&u,&v,&w);93 addedge(u,v,w);94 }95 scanf("%d%d%d",&s,&t,&k);96 spfa(t);97 printf("%d",A_star(s)); 98 return 0;99 }

 

 

转载于:https://www.cnblogs.com/fujudge/p/7496766.html

你可能感兴趣的文章
2019 Multi-University Training Contest 2 - Keen On Everything But Triangle
查看>>
centos7 中samba 开机启动命令
查看>>
对“点餐系统”的阅读、分析与运行
查看>>
Linux 安装Mysql
查看>>
给定两个有序的整形数组,找出里边的相同元素
查看>>
C语言指针加1问题以及字节对齐问题
查看>>
【NLP】揭秘马尔可夫模型神秘面纱系列文章(二)
查看>>
rest-work-eat-study-rest-work-eat or rest-rest-work-work-eat-eat..
查看>>
用EnableMenuItem不能使菜单变灰的原因
查看>>
Mac OS X Yosemite安装Hadoop 2.6记录
查看>>
Tomcat全攻略
查看>>
闰年的定义
查看>>
探索Scala(1)-- 运算符重载
查看>>
【LDAP】LDAP 中 CN, OU, DC 的含义
查看>>
Buy Tickets(线段树)
查看>>
SharePoint 2013 图文开发系列之列表定义高级篇
查看>>
20145219 《Java程序设计》实验五 Java网络编程及安全实验报告
查看>>
微软:我们关于Silverlight战略转移[原文]
查看>>
java多线程之内存的可见性介绍(备用1)
查看>>
基于ML算法KNN与OpenCV的数独识别与自动填充___By 何子辰
查看>>