首页 > 代码库 > POJ 2449 Remmarguts' Date
POJ 2449 Remmarguts' Date
Remmarguts‘ Date
Time Limit: 4000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 244964-bit integer IO format: %lld Java class name: Main
"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
解题:A*求第k短路。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 1010;18 struct arc{19 int to,w,next;20 arc(int x = 0,int y = 0,int z = -1){21 to = x;22 w = y;23 next = z;24 }25 };26 struct node{27 int g,h,to;28 node(int x = 0,int y = 0,int z = 0){29 g = x;30 h = y;31 to = z;32 }33 bool operator < (const node &x) const{34 return x.g + x.h < g+h;35 }36 };37 arc e[200010];38 int head[maxn],tail[maxn],d[maxn],tot,n,m,s,t,kth;39 int cnt[maxn];40 void add(int u,int v,int w){41 e[tot] = arc(v,w,head[u]);42 head[u] = tot++;43 e[tot] = arc(u,w,tail[v]);44 tail[v] = tot++;45 }46 priority_queue< pii,vector< pii >,greater< pii > >q;47 priority_queue<node>qq;48 void dijkstra(){49 for(int i = 1; i <= n; i++) d[i] = INF;50 while(!q.empty()) q.pop();51 d[t] = 0;52 q.push(make_pair(d[t],t));53 while(!q.empty()){54 int u = q.top().second;55 int w = q.top().first;56 q.pop();57 if(w > d[u]) continue;58 for(int i = tail[u]; ~i; i = e[i].next){59 if(d[e[i].to] > d[u] + e[i].w){60 d[e[i].to] = d[u] + e[i].w;61 q.push(make_pair(d[e[i].to],e[i].to));62 }63 }64 }65 }66 int Astar(){67 while(!qq.empty()) qq.empty();68 memset(cnt,0,sizeof(cnt));69 qq.push(node(0,d[s],s));70 while(!qq.empty()){71 node now = qq.top();72 qq.pop();73 if(++cnt[now.to] > kth) continue;74 if(cnt[t] == kth) return now.g;75 for(int i = head[now.to]; ~i; i = e[i].next)76 qq.push(node(now.g+e[i].w,d[e[i].to],e[i].to));77 78 }79 return -1;80 }81 int main() {82 int u,v,w;83 while(~scanf("%d %d",&n,&m)){84 memset(head,-1,sizeof(head));85 memset(tail,-1,sizeof(tail));86 for(int i = 0; i < m; i++){87 scanf("%d %d %d",&u,&v,&w);88 add(u,v,w);89 }90 scanf("%d %d %d",&s,&t,&kth);91 dijkstra();92 if(s == t) ++kth;93 printf("%d\n",Astar());94 }95 return 0;96 }
POJ 2449 Remmarguts' Date
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。