Time Limit: 4000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
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
题意:给出一个有向图,起点终点以及k,求k短路
思路:这个题是最短路加A*算法算k短路,应该算k短路的入门题目
我认为A*算法是 搜索加一个估价函数, 本题中,估价函数f[u] = g[u] + d[u];
g[u]表示的是到从起点到u当前的距离,d[u]表示的是u到终点的最短路
用bfs遍历时用优先队列存,按f排序
首先push的是{s, 0}(表示u和g[u]), 后面用普通bfs直接push即可,当 终点算了第k次时,就是答案了
还有d数组需要用反边存一个图,在算A*前用spfa算好。
//poj 2449
//miaomiao_ymxl 9.4
//
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define MAXN (1000+5)
#define INF 0x3f3f3f3f
struct node{
int v, dis;
};
vector<node> G[MAXN], G_f[MAXN];
bool inq[MAXN];
int n, m, d[MAXN];
struct A{
int u, g;
bool operator <(const A &x)const{
return (g+d[u]) > (x.g+d[x.u]) || ((g+d[u]) == (x.g+d[x.u]) && g > x.g);
}
};
void spfa(int s){
queue<int> q;
for(int i = 1; i <= n; i++) d[i] = INF;
q.push(s);
d[s] = 0; inq[s] = true;
int u, v, vdis;
while(!q.empty()){
u = q.front(); q.pop();
inq[u] = false;
for(int i = 0; i < G_f[u].size(); i++){
v = G_f[u][i].v, vdis = G_f[u][i].dis;
if(d[v] > (d[u]+vdis)){
d[v] = d[u]+vdis;
if(!inq[v]){
q.push(v);
inq[v] = true;
}
}
}
}
}
int vis[MAXN];
int Astar(int s, int t, int k){
priority_queue<A> q;
q.push((A){s, 0});
if(s == t) k++; <span style="font-size:10px;">//起点可能与终点相同,此时0不算最短路,故k++</span>
int u, f, g, v, vdis, cnt = 0;
A now;
while(!q.empty()){
now = q.top(); q.pop();
u = now.u, f = now.g+d[u], g = now.g;
vis[u]++;
if(u == t){
cnt++;
if(cnt == k) return f;
}
if(vis[u] > k) continue;
for(int i = 0; i < G[u].size(); i++){
v = G[u][i].v, vdis = G[u][i].dis;
q.push((A){v, g+vdis});
}
}
return -1;
}
int main(){
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
scanf("%d%d", &n, &m);
int u, v, dis;
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &u, &v, &dis);
G[u].push_back((node){v, dis});
G_f[v].push_back((node){u, dis});
}
int s, t, k;
scanf("%d%d%d", &s, &t, &k);
spfa(t);
printf("%d\n", Astar(s, t, k));
return 0;
}
不忘初心,方得始终