博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2449
阅读量:6271 次
发布时间:2019-06-22

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

 

Remmarguts' Date
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27699   Accepted: 7500

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

Source

,Zeyuan Zhu

题解:

k短路模板

AC代码:

#include
#include
#include
#include
using namespace std;inline const int read(){ register int x=0,f=1; register char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x*f;}const int N=1e3+10;const int M=1e5+10;const int inf=1e9;struct asd{ int v,w,next;}e1[M],e2[M];bool vis[N];struct node{ int id;///当前节点编号 int f;//f表示经过当前节点的最短路 int g;//g表示S->当前节点的最短路 node(int id=0,int f=0,int g=0):id(id),f(f),g(g){} bool operator <(const node &a)const{ if(f==a.f) return g>a.g; return f>a.f; }};int tot,n,m,K,head1[N],head2[N];int dis[N];//dis[i]表示当前点i到终点T的最短路径 void add(int x,int y,int z){ ++tot; e1[tot].v=y;e1[tot].w=z;e1[tot].next=head1[x];head1[x]=tot; e2[tot].v=x;e2[tot].w=z;e2[tot].next=head2[y];head2[y]=tot;}void Spfa(int S){
//更新每个点->n点的最短距离 queue
q; for(int i=1;i<=n;i++) dis[i]=inf; dis[S]=0; vis[S]=1; q.push(S); while(!q.empty()){ int x=q.front();q.pop(); vis[x]=0; for(int i=head2[x];i;i=e2[i].next){ int v=e2[i].v,w=e2[i].w; if(dis[v]>dis[x]+w){ dis[v]=dis[x]+w; if(!vis[v]){ vis[v]=1; q.push(v); } } } } }void A_Star(int S,int T){ if(S==T) K++;//坑 priority_queue
q; q.push(node(S,0,0)); int cnt=0; while(!q.empty()){ node h=q.top();q.pop(); if(h.id==T){ if(++cnt==K){ printf("%d",h.f);//返回第k短路 return ; } } for(int i=head1[h.id];i;i=e1[i].next){ q.push(node(e1[i].v,h.g+e1[i].w+dis[e1[i].v],h.g+e1[i].w));//最短路更新k短路 } } puts("-1");}int main(){ n=read();m=read(); for(int i=1,x,y,z;i<=m;i++){ x=read();y=read();z=read(); add(x,y,z); } int S,T; S=read();T=read();K=read(); Spfa(T);//预处理,反向遍历 A_Star(S,T); return 0;}

 

转载于:https://www.cnblogs.com/shenben/p/5880307.html

你可能感兴趣的文章
IIS7显示ASP的详细错误信息到浏览器
查看>>
使用fiddler对手机APP进行抓包
查看>>
exit和_exit的区别
查看>>
Javascript、Jquery获取浏览器和屏幕各种高度宽度(单位都为px)
查看>>
php不重新编译,安装未安装过的扩展,如curl扩展
查看>>
JavaScript编码encode和decode escape和unescape
查看>>
ppp点对点协议
查看>>
html5游戏开发-简单tiger机
查看>>
Codeforces 712C Memory and De-Evolution
查看>>
编写的windows程序,崩溃时产生crash dump文件的办法
查看>>
Ural2110 : Remove or Maximize
查看>>
Django REST framework 的TokenAuth认证及外键Serializer基本实现
查看>>
《ArcGIS Runtime SDK for Android开发笔记》——问题集:如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题(转载)...
查看>>
Spring Boot日志管理
查看>>
动态注册HttpModule管道,实现global.asax功能
查看>>
使用 ES2015 编写 Gulp 构建
查看>>
[转]Using NLog for ASP.NET Core to write custom information to the database
查看>>
BZOJ 4766: 文艺计算姬 [矩阵树定理 快速乘]
查看>>
MySQL 的instr函数
查看>>
Hibernate的核心对象关系映射
查看>>