Remmarguts' Date
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. 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. 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 Sample Output Source POJ Monthly,Zeyuan Zhu |
[Submit] [Go Back] [Status] [Discuss]
题目大意:让你求两个点之间的第K短路径。
解题思路:运用Astar算法解k短路问题,我就简单讲一下A*算法在求解k短路的过程,首先,需要求出每个点到最短路径,然后从起点开始搜,这个过程其实很像dijstra算法,我们在每个节点中存3个信息:1.当前节点(from),2.从起点走到当前节点的实际花费(g),3.从当前节点到终点的最终总花费估价(f) == g+当前节点到终点的最短距离,这样当我们第k次经过终点的时候我们的g就是k短路的长度了。这样我们我们就开始了k短路的搜索之旅,上代码。有兴趣的同学可以去深入学习下A*算法。
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<cstdlib>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, b) G.push_back(b);
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<LL, pair<int, LL> > LLppar;
typedef pair<double, int> dpar;
typedef pair<int, int> par;
typedef pair<LL, int> LLpar;
const LL mod = 1e9+7;
const int INF = 0x3f3f3f3f;
const int N = 1010;
const double pi = 3.1415926;
using namespace std;
int n, m, cnt, cntr, s, t, k, sum;
int head[1010], headr[1010], d[1010], vis[1010];
struct edge
{
int to;
int next;
int len;
}e[100010], er[100010];
void add(int u, int v, int w)
{
e[cnt].to = v;
e[cnt].len = w;
e[cnt].next = head[u];
head[u] = cnt ++;
er[cntr].to = u;
er[cntr].len = w;
er[cntr].next = headr[v];
headr[v] = cntr ++;
}
void dijstra() //反向跑
{
for(int i = 1; i <= n; i ++) {
d[i] = INF;
}
d[t] = 0;
priority_queue<par> q;
q.push(make_pair(0, t));
while(!q.empty()) {
par cur = q.top();
q.pop();
int u = cur.second;
int w = -cur.first;
if(vis[u]) continue;
vis[u] = 1;
for(int i = headr[u]; ~i; i = er[i].next) {
int en = er[i].to;
int len = w+er[i].len;
if(d[en] > len) {
d[en] = len;
q.push(make_pair(-len, en));
}
}
}
}
struct node
{
int from;
int g;
int f;
bool operator<(const node a) const { //注意写好重载
if(a.f == f) return a.g < g;
return a.f < f;
}
};
int Astar()
{
sum = 0;
node st;
if(s == t) k ++; //如果起点是终点的话k需要,否则当>=2时求出来的是k-1短路
if(d[s] == INF) return -1; //最短路都不存在,k短路肯定也不会存在
st.from = s;
st.g = 0;
st.f = 0; //这里有些人会按实际意义初始化为d[s],其实什么值都可以,反正用不到
priority_queue<node> q;
q.push(st);
while(!q.empty()) {
node cur = q.top();
q.pop();
int u = cur.from;
if(u == t) {
sum ++;
if(sum == k) {
return cur.g;
}
}
for(int i = head[u]; ~i; i = e[i].next) {
int en = e[i].to;
node next;
next.from = en;
next.g = cur.g + e[i].len; //到当前节点的实际花费
next.f = next.g + d[en]; //到终点的预估花费
q.push(next);
}
}
return -1;
}
void init()
{
mem1(head);
mem1(headr);
mem0(vis);
cnt = cntr = 0;
}
int main()
{
int x, y, z;
while(~scanf("%d%d", &n, &m)) {
init();
for(int i = 0; i < m; i ++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
}
scanf("%d%d%d", &s, &t, &k);
dijstra();
printf("%d\n", Astar());
}
return 0;
}