隐藏页面特效

poj2449

 

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 2 1 2 5 2 1 4 1 2 2

Sample Output

14

Source

POJ Monthly,Zeyuan Zhu

题解:

k短路模板

AC代码:

#include<cstdio> #include<cstring> #include<queue> #include<iostream> 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<int>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<node>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; }

 


__EOF__

本文作者shenben
本文链接https://www.cnblogs.com/shenben/p/5880307.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   神犇(shenben)  阅读(1585)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示