POJ 2449 Remmarguts' Date (dijkstra + A*)

                  Remmarguts' Date(点击查看题目)

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 41070 Accepted: 11295

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

题目分析

题意:求图中第k小的最短路的长度

思路:使用A*算法求第k小的最短路,不懂得可以参考这里,A*算法是一种优化的bfs算法,在A*算法中,有 F = G + H ,G表示从起点到指定点的实际代价,H表示从指定点到终点的预估代价,F表示起点借助指定点到终点的预估代价,由于我们这里需要求的不是最短路,而是第k小的最短路,为此我们需要对H进行准确预估,而为了得到准确的预估,我们反向跑一次最短路就可以得到了,最后用A*的思路求出第K小的最短路。(A*的思想...)

不过这里有一个问题,那就是起点和终点重合的情况,在这里WA了无数次.....看了大神们的说明,发现起点和终点重合的时候不能算一条最短路...QAQ,为此,当起点和终点重合的时候,k++,相当于除去了重合的情况,再找第k小的最短路。

代码区

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip>
#define bug cout << "**********" << endl
#define show(x,y) "["<<x<<","<<y<<"]"
//#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e8;
const int Max = 1e5 + 10;

struct Edge
{
	int to;		//下一个点,这条边的终点
	int last;	//上一个点,这条边的起点,用于存反向图
	int cost;	//边权值
	int next;	//同一个起点的另一个结点
	int pre;	//同一个终点的起一个结点
}edge[Max<<1];

struct Node
{
	int x;
	int real;	//距离起点或者终点的真实距离
	int guess;	//预估值
	bool operator<(const Node& node) const
	{
		return this->real + this->guess > node.real + node.guess;
	}
};

int n, m;
int S, T, K;
int head1[Max], head2[Max], tot;
int guess[Max];	//记录各点到终点的最短距离,即预估值
bool vis[Max];

void init()
{
	tot = 0;
	memset(vis, 0, sizeof(vis));
	memset(head1, -1, sizeof(head1));
	memset(head2, -1, sizeof(head2));
	fill(guess + 1, guess + 1 + n, inf);
}

void add(int u,int v,int cost)
{
	edge[tot].to = v;edge[tot].last = u;
	edge[tot].cost = cost;
	edge[tot].next = head1[u];
	edge[tot].pre = head2[v];
	head1[u] = head2[v] = tot;tot++;
}

void dijkstra(int start)
{
	priority_queue<Node>q;
	guess[start] = 0;
	q.push({ start,0,0 });		//这里不需要加入预估值,普通的dijkstra
	while(!q.empty())
	{
		Node node = q.top();q.pop();
		if (vis[node.x]) continue;
		vis[node.x] = true;
		for(int i = head2[node.x] ;i != -1 ; i = edge[i].pre)	//反向
		{
			int last = edge[i].last, cost = edge[i].cost;
			if(guess[last] > guess[node.x] + cost)
			{
				guess[last] = guess[node.x] + cost;
				q.push({ last,guess[last],0 });
			}
		}
	}
}

int A_start(int start)
{
	priority_queue<Node>q;
	q.push({ start,0,guess[start]});
	while(!q.empty())
	{
		Node node = q.top();q.pop();
		if(node.x ==  T)			//按照路径大小,依次经过终点k次
		{
			if (K > 1)
				K--;
			else
				return node.real + node.guess;
		}
		for(int i = head1[node.x]; i != -1 ; i = edge[i].next)
		{
			q.push({ edge[i].to,node.real + edge[i].cost,guess[edge[i].to] });
		}
	}
	return -1;
}

int main()
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		for(int i = 1 ;i <= m ;i ++)
		{
			int u, v, cost;
			scanf("%d%d%d", &u, &v, &cost);
			add(u,v,cost);
		}
		scanf("%d%d%d", &S, &T, &K);
		dijkstra(T);
		if(guess[S] == inf)
		{
			printf("-1\n");
			continue;
		}
		if (S == T) K++;
		printf("%d\n", A_start(S));
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值