POJ2449 Remmarguts' Date
Posted 嘒彼小星
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2449 Remmarguts' Date相关的知识,希望对你有一定的参考价值。
Remmarguts‘ Date
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 31080 | Accepted: 8486 |
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 2 1 2 5 2 1 4 1 2 2
Sample Output
14
Source
POJ Monthly,Zeyuan Zhu
【题解】
这个破题数据是肯定有错误的
s与t相等时,最短路是0,但数据并没有算上
调了一晚上,早上看了网上博客才发现的这个问题
A*算法,估价函数是当前路径长度+这个点到终点的最短路长度。
每次取值最小的进行拓展,每拓展到终点一次代表找到下一条最短路。
第k次拓展到终点表示找到第k短路
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cstdio> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 9 inline void read(int &x) 10 { 11 x = 0;char ch = getchar(), c = ch; 12 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 13 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 14 if(c == ‘-‘)x = -x; 15 } 16 17 const int MAXN = 50000 + 10; 18 const int MAXM = 500000 + 10; 19 20 struct Edge 21 { 22 int u,v,w,next; 23 Edge(int _u, int _v, int _w, int _next){u = _u;v = _v; w = _w; next = _next;} 24 Edge(){} 25 }edge[MAXM << 2], edge2[MAXM << 2]; 26 int head[MAXN], head2[MAXN], cnt, cnt2; 27 28 inline void insert(int a, int b, int c) 29 { 30 edge[++cnt] = Edge(a,b,c,head[a]); 31 head[a] = cnt; 32 } 33 34 inline void insert2(int a, int b, int c) 35 { 36 edge2[++cnt2] = Edge(a,b,c,head2[a]); 37 head2[a] = cnt2; 38 } 39 40 int n,m,s,t,k; 41 42 int b[MAXN], d[MAXN]; 43 44 struct Node 45 { 46 int p, d; 47 Node(int _p, int _d){p = _p;d = _d;} 48 Node(){} 49 }; 50 51 struct cmp 52 { 53 bool operator()(Node a, Node b) 54 { 55 return a.d > b.d; 56 } 57 }; 58 59 std::priority_queue<Node, std::vector<Node>, cmp> q; 60 61 void dijkstra() 62 { 63 while(q.size())q.pop(); 64 memset(d, 0x3f, sizeof(d)); 65 q.push(Node(t, 0)); 66 d[t] = 0; 67 register Node tmp; 68 while(q.size()) 69 { 70 tmp = q.top(), q.pop(); 71 if(b[tmp.p])continue; 72 b[tmp.p] = 1; 73 for(register int pos = head[tmp.p];pos;pos = edge[pos].next) 74 { 75 int v = edge[pos].v; 76 if(b[v])continue; 77 if(d[v] > tmp.d + edge[pos].w) 78 d[v] = tmp.d + edge[pos].w, q.push(Node(v, d[v])); 79 } 80 } 81 } 82 83 struct cmpp 84 { 85 bool operator()(Node a, Node b) 86 { 87 return a.d + d[a.p] > b.d + d[b.p]; 88 } 89 }; 90 91 std::priority_queue <Node, std::vector<Node>, cmpp> q2; 92 93 int step;//拓展次数 94 95 void abfs() 96 { 97 step = 0; 98 while(q2.size())q2.pop(); 99 q2.push(Node(s, 0)); 100 register Node tmp; 101 if(d[s] == d[0]) 102 { 103 printf("-1\n"); 104 return; 105 } 106 while(q2.size()) 107 { 108 tmp = q2.top(),q2.pop(); 109 if(tmp.p == t) ++ step; 110 if(step == k) 111 { 112 printf("%d\n", tmp.d); 113 return; 114 } 115 for(register int pos = head2[tmp.p];pos;pos = edge2[pos].next) 116 { 117 int v = edge2[pos].v; 118 q2.push(Node(edge2[pos].v, tmp.d + edge2[pos].w)); 119 } 120 } 121 printf("-1\n"); 122 } 123 124 int main() 125 { 126 register int tmp1, tmp2, tmp3; 127 while(scanf("%d %d", &n, &m) != EOF && (n + m)) 128 { 129 memset(b, 0, sizeof(b)); 130 memset(head, 0, sizeof(head)); 131 memset(head2, 0, sizeof(head2)); 132 memset(edge, 0, sizeof(edge)); 133 memset(edge2, 0, sizeof(edge2)); 134 cnt = cnt2 = 0; 135 for(register int i = 1;i <= m;++ i) 136 { 137 read(tmp1), read(tmp2), read(tmp3); 138 insert2(tmp1, tmp2, tmp3); 139 insert(tmp2, tmp1, tmp3); 140 } 141 read(s), read(t), read(k); 142 if(t == s)++ k; 143 dijkstra(); 144 abfs(); 145 } 146 return 0; 147 }
以上是关于POJ2449 Remmarguts' Date的主要内容,如果未能解决你的问题,请参考以下文章