POJ 2449 Remmarguts’ Date
Time Limit: 4000MS Memory Limit: 65536K
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
思路
- 先用SPFA对终点求反向最短路(这里是有向图,要把边反向存储在另一空间内),得到了终点到各个点的最短路径
- 若不能到达源点说明无解返回-1,若终点和源点相同,那么要把k+ 1,因为这个路径为0,无效。
- 然后BFS中的优先队列重写比较函数,选取评估结果 + dist 最优的点进入下一步,实现A*的思路。
Code
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 1e3 + 3;
const int maxm = 1e5 + 3;
const int inf = 0x3f3f3f3f;
int f[maxn], head[maxn], reverse_head[maxn];
bool inq[maxn];
struct way {
int node, dist, f;
way(int node = 0, int dist = 0, int f = 0) : node(node), dist(dist), f(f) {}
bool operator < (const way& other) const{
if (f == other.f) return dist > other.dist;
return f > other.f;
}
};
struct edge {
int to, val, next;
edge(int to = 0, int val = 0, int next = 0) : to(to), val(val), next(next){}
}edges[maxm], reverse_edges[maxm];
inline int read() {
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
return x * f;
}
void spfa(int s) {
memset(f, inf, sizeof(f));
memset(inq, false, sizeof(inq));
f[s] = 0;
queue<int> q;
q.push(s);
inq[s] = true;
while (q.size()) {
int now = q.front(); q.pop();
inq[now] = false;
for (int i = reverse_head[now]; i; i = reverse_edges[i].next) {
int to = reverse_edges[i].to, val = reverse_edges[i].val;
if (f[to] > f[now] + val) {
f[to] = f[now] + val;
if (!inq[to]) {
q.push(to);
inq[to] = true;
}
}
}
}
}
int bfs(int s, int e, int k) {
spfa(e);
if (f[s] == inf) return -1;
int now_k = 0;
if (s == e) k++;
priority_queue<way> pq;
pq.push(way(s, 0, f[s]));
while (pq.size()) {
way top = pq.top(); pq.pop();
int now = top.node, dist = top.dist;
if (now == e) {
++now_k;
if (now_k == k) return dist;
}
for (int i = head[now]; i; i = edges[i].next) {
int to = edges[i].to, new_dist = dist + edges[i].val;
pq.push(way(to, new_dist, new_dist + f[to]));
}
}
return -1;
}
int main() {
int n, m, a, b, t, s, e, k;
while (cin >> n >> m) {
memset(edges, 0, sizeof(edges));
memset(reverse_edges, 0, sizeof(reverse_edges));
memset(head, 0, sizeof(head));
memset(reverse_head, 0, sizeof(reverse_head));
for (int i = 1; i <= m; i++) {
a = read(); b = read(); t = read();
edges[i] = edge(b, t, head[a]);
head[a] = i;
reverse_edges[i] = edge(a, t, reverse_head[b]);
reverse_head[b] = i;
}
s = read(); e = read(); k = read();
cout << bfs(s, e, k) << endl;
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)