[POJ2449] Remmarguts' Date(裸k短路)(A*搜索)

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


题目大意:给出一张有向图,求起点S到终点T的第k短路。

我最初的想法是先将环缩点,然后每个点开一个大小为K的堆来记录起点到这里的前K短距离,按着拓扑排序的顺序来递推,用类似归并的方法来统计到每个点的前K段路。但是对于环内部不是很好处理。(其实如果这个图无环的话这样做比本题正解还快。。咳咳)

正解:上午用A*做了八数码,这道题继续A*。用优先队列维护每个点的估值,保证每次搜到终点得到的路径依次是最短,次短。。。估值是A*的精髓,分为g函数和h函数,分别记录从原点来的路径长(已知)和到终点的估值(要求<=实际长度)。在这道题中,g函数记录从原点出发的距离,h函数记录该点到终点的最短路(=实际长度)即可。先边反向跑一次最短路即可获取所有节点的h函数值,然后A*,第K次到达终点即是第K短路。


#include<cstdio>
#include<queue>
using namespace std;
#define MAXM 100005
#define MAXN 1005
const int INF = 1<<28;
int N, M, S, T, K;

struct Node {
	int to, len; Node *next;
}Edge[MAXM*4], *ecnt = Edge, *adj[MAXN], *radj[MAXN];
void addedge(int a, int b, int c)
{
	++ecnt;
	ecnt->to = b;
	ecnt->len = c;
	ecnt->next = adj[a];
	adj[a] = ecnt;
	++ecnt;
	ecnt->to = a;
	ecnt->len = c;
	ecnt->next = radj[b];
	radj[b] = ecnt;
}

struct dijs {
	int u, dis;
	dijs () {}
	dijs (int a,int b) {u=a; dis=b;}
	bool operator < (const dijs&a) const {
		return dis > a.dis;
	}
};
int rdis[MAXN];
bool vis[MAXN];
priority_queue<dijs> dij;
void rDijkstra() //边反向跑最短路以获取H函数值
{
	for (int i = 1; i<=1000; ++i) rdis[i] = INF;
	rdis[T] = 0;
	dij.push(dijs(T, 0));
	dijs t;
	while (!dij.empty()) {
		do {
			if (dij.empty()) return;
			t = dij.top(); dij.pop();
		} while (vis[t.u]);
		vis[t.u] = 1;
		for (Node *p = radj[t.u]; p; p=p->next)
			if (rdis[p->to] > t.dis + p->len) {
				rdis[p->to] = t.dis + p->len;
				dij.push(dijs(p->to, rdis[p->to]));
			}
	}
}

struct ss {
	int u, pre;
	ss () {}
	ss (int a, int b) { u=a; pre=b; }
	bool operator < (const ss&a) const {
		return pre+rdis[u] > a.pre+rdis[a.u]; //pre是G函数值,rdis是H函数值,根据两个之和判定优先级。
	}
};
int Tvisn; //第几次到达T点
priority_queue<ss> as;
int Astar()
{
	if (S==T) Tvisn = -1; //如果起点终点相等,一开始不算到达终点
	as.push(ss(S, 0));
	ss t;
	while (!as.empty()) {
		t = as.top();
		as.pop();
		if (t.u == T) {
			++Tvisn;
			if (Tvisn == K) return t.pre;
		}
		for (Node *p = adj[t.u]; p; p=p->next)
			as.push(ss(p->to, t.pre + p->len));
	}
	return -1;
}

int main()
{
	int i, a, b, c;
	scanf("%d%d", &N, &M);
	for (i = 1; i<=M; ++i)
	{
		scanf("%d%d%d", &a, &b, &c);
		addedge(a, b, c);
	}
	scanf("%d%d%d", &S, &T, &K);
	rDijkstra();
	printf("%d\n", Astar());
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值