首页 > 代码库 > poj2449
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.
"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).
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
POJ Monthly,Zeyuan Zhu
题解:
k短路模板
AC代码:
#include<cstdio>#include<queue>using namespace std;const int N=50010;struct node{ int v,next; int w;}e1[N*10],e2[N*10];int tot,n,m,k,head1[N],head2[N];int dis[N];bool vis[N];struct data{ int f,g; int from; data(int x,double y,double z):from(x),f(y),g(z){} bool operator < (const data &a) const { if(f==a.f) return g>a.g; return f>a.f; }};inline void read(int &x){ register char ch=getchar();x=0; while(ch>‘9‘||ch<‘0‘) ch=getchar(); while(ch>=‘0‘&&ch<=‘9‘) x=(x<<3)+(x<<1)+ch-‘0‘,ch=getchar();}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){ for(int i=1;i<=n;i++) dis[i]=0x3f3f3f3f; dis[S]=0; queue<int>q; q.push(S); vis[S]=1; while(!q.empty()){ int h=q.front();q.pop(); vis[h]=0; for(int i=head2[h];i;i=e2[i].next){ int v=e2[i].v,w=e2[i].w; if(dis[v]>dis[h]+w){ dis[v]=dis[h]+w; if(!vis[v]){ vis[v]=1; q.push(v); } } } }}void a_star(int S,int T){ priority_queue<data>q; if(S==T) k++; int cnt=0; q.push(data(S,dis[S],0)); while(!q.empty()){ data h=q.top();q.pop(); if(h.from==T){ if(++cnt==k){ printf("%d",h.f); return ; } } for(int i=head1[h.from];i;i=e1[i].next){ q.push(data(e1[i].v,h.g+e1[i].w+dis[e1[i].v],h.g+e1[i].w));//最短路更新k短路 } } puts("-1");}int main(){ int x,y,z,S,T; read(n);read(m); for(int i=1;i<=m;i++){ read(x);read(y);read(z); add(x,y,z); } read(S);read(T);read(k); spfa(T); a_star(S,T); return 0;}
poj2449
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。