poj2449 Remmarguts' Date(第k短路,A*)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhanghaoxian1/article/details/83098059

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.

“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

题意:给出n个点m条边的有向图,求s到t的第k短路。

分析:估价函数f(x),在这题中为x到t的最短路,即把反向图求t到x的最短路;实际距离g(x)为f(x)+dist(x),dist为s到x的第i短路。将g(x)放到一个堆中,每次取出g(x)最小的点,扩展与x相连的每条边,直到第k次取出t,此时的dist(t)即为第k短路长度。

代码

#include <cstdio>
#include <queue>
#define N 200005
using namespace std;

struct arr
{
	int to, nxt, w;
}a[N],b[N];
struct node
{
	int x, dis, g;
	bool operator <(const node a)const
	{
		if (a.g == g) return a.dis < dis;
		return a.g < g;
	}
};
int n,m,s,t,k;
int d[N],lsa[N],la,lsb[N],lb;
bool vis[N];

void add(int x, int y, int z)
{
	a[++la].w = z;
	a[la].to = y;
	a[la].nxt = lsa[x];
	lsa[x] = la;
	b[++lb].w = z;
	b[lb].to = x;
	b[lb].nxt = lsb[y];
	lsb[y] = lb;
}

void spfa()
{
	queue<int> q;
	while (q.size()) q.pop();
	q.push(t);
	vis[t] = true;
	for (int i = 1; i <= n; i++) d[i] = 1e9;
	d[t] = 0;
	while (q.size())
	{
		int u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = lsb[u]; i; i = b[i].nxt)
			if (d[b[i].to] > d[u] + b[i].w)
			{
				d[b[i].to] = d[u] + b[i].w;
				if (!vis[b[i].to]) 
				{
					vis[b[i].to] = true;
					q.push(b[i].to);
				}
			}
	}
}

int Astar()
{
	if (d[s] == 1e9) return -1;
	if (s == t) k++;
	int cnt = 0;
	priority_queue<node> Q;
	while (Q.size()) Q.pop();
	node u;
	u.x = s;
	u.dis = 0;
	u.g = d[s];
	Q.push(u);
	while (Q.size())
	{
		node v = Q.top();
		Q.pop();
		if (v.x == t)
		{
			cnt++;
			if (cnt == k) return v.dis;
		}
		for (int i = lsa[v.x]; i; i = a[i].nxt)
		{
			u.x = a[i].to;
			u.dis = v.dis + a[i].w;
			u.g = u.dis + d[u.x];
			Q.push(u);
		}
	}
	return -1;
}

int main()
{
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= m; i++)
	{
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		add(x, y, z);
	}
	scanf("%d%d%d", &s, &t, &k);
	spfa();
	printf("%d", Astar());
}

猜你喜欢

转载自blog.csdn.net/zhanghaoxian1/article/details/83098059