Remmarguts' Date
"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短路
算法分析:使用dij+A*求解第k短路的经典题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
/************************************************* Author :supermaker Created Time :2016/1/31 13:33:52 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1000 + 66; int n, m, dis[maxn], cnt[maxn]; vector <PII> edge[maxn], g[maxn]; struct Node { int g, v, f; Node () {} Node (int gg, int vv, int ff) : g (gg), v (vv), f (ff) {} bool operator < (const Node &a) const { return f > a.f; } }; void Dij (int s) { fill (dis, dis + maxn, INF); dis[s] = 0; priority_queue <PII, vector <PII>, greater <PII> > qu; qu.push (PII (0, s)); while (!qu.empty()) { PII tt = qu.top (); qu.pop (); int ta = tt.second, tb = tt.first; if (dis[ta] < tb) continue; for (int i = 0; i < edge[ta].size (); i++) { PII v = edge[ta][i]; if (v.first != s && dis[v.first] > v.second + dis[ta]) { dis[v.first] = v.second + dis[ta]; qu.push (PII (dis[v.first], v.first)); } } } } int A_star (int s, int k) { if (dis[s] == INF) return -1; priority_queue <Node> qu; qu.push (Node (0, s, dis[s])); Node nxt = Node (0, 0, 0); while (!qu.empty ()) { Node tt = qu.top (); qu.pop (); cnt[tt.v]++; if (cnt[tt.v] > k) continue; if (cnt[tt.v] == k) return tt.f; for (int i = 0; i < g[tt.v].size (); i++) { PII vv = g[tt.v][i]; nxt.g = tt.g + vv.second; nxt.v = vv.first; nxt.f = nxt.g + dis[vv.first]; qu.push (nxt); } } return -1; } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &n, &m) != EOF) { for (int i = 0; i < maxn; i++) edge[i].clear (), g[i].clear (); memset (cnt, 0, sizeof (cnt)); for (int i = 1; i <= m; i++) { int u, v, w; scanf ("%d%d%d", &u, &v, &w); edge[v].push_back (PII (u, w)); g[u].push_back (PII (v, w)); } int s, e, k; scanf ("%d%d%d", &s, &e, &k); if (s == e) k++; Dij (e); printf ("%d\n", A_star (s, k)); } return 0; } |
- « 上一篇:poj1066
- poj2139:下一篇 »