【K短路】【A星】Remmarguts' Date POJ 2449 A-Star

Remmarguts' Date

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 17296 Accepted: 4746

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短路裸体,第一次写K短路,WA了两次


每个点的 g 值不用说了,然后 h 值由当前点到终点 T 的最短路,所以要先把边反向,以T为源点来一次spfa


判断是否为K短路的时候应该在出队的时候判断,而不是入队的时候【应该很好理解,但是我一开始就错了。。。。】


然后这一题的S可能等于T,但一开始在S加一的时候并不算T加一,但实际上加了。。。。所以这种情况我们把他减去,或者直接把K+1,改成求K+1短路


测评情况(POJ)

第一次是判断K短路出错,第二次是没考虑S=T




C++ AC Code

/*http://blog.csdn.net/jiangzh7
By Jiangzh*/
#include<cstdio>
#include<cstring>
#include<queue>
using std::queue;
using std::priority_queue;
#define min(a,b) ((a)<(b)?(a):(b))
const int N=1000+10;
const int inf=0x3f3f3f3f;

int n,m;
struct Link{int y,z;Link *next;}*head[N],*tail[N];
int S,T,K;
int dist[N],inQ[N];
queue<int> que;
int enque_time[N];

struct ND{
	int x,g,h;
	bool operator < (const ND &a) const 
	{
		return g+h>a.g+a.h;
	}
};
priority_queue<ND> Q;

void inhead(int x,int y,int z)
{
	Link *p=new Link;
	p->y=y;p->z=z;
	p->next=head[x];
	head[x]=p;
}
void intail(int x,int y,int z)
{
	Link *p=new Link;
	p->y=y;p->z=z;
	p->next=tail[x];
	tail[x]=p;
}

void read()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		inhead(x,y,z);
		intail(y,x,z);
	}
	scanf("%d%d%d",&S,&T,&K);
}

bool spfa()
{
	memset(dist,0x3f,sizeof(dist));
	memset(inQ,0,sizeof(inQ));
	dist[T]=0; que.push(T);
	while(!que.empty())
	{
		int x=que.front();que.pop();
		inQ[x]=0;
		for(Link *p=tail[x];p;p=p->next)
			if(dist[p->y]>dist[x]+p->z)
			{
				dist[p->y]=dist[x]+p->z;
				if(!inQ[p->y])
				{
					inQ[p->y]=1;
					que.push(p->y);
				}
			}
	}
	return dist[S]!=inf;
}

int solve()
{
	if(!spfa()) return -1;
	if(S==T) K++;
	memset(enque_time,0,sizeof(enque_time));
	ND start; start=(ND){S,0,dist[S]};
	Q.push(start);
	while(!Q.empty())
	{
		ND tmp=Q.top(); Q.pop();
		int x=tmp.x;
		enque_time[x]++;
		if(enque_time[T]==K) return tmp.g;
		for(Link *p=head[x];p;p=p->next)
		{
			ND tt=(ND){p->y,tmp.g+p->z,dist[p->y]};
			Q.push(tt);
		}
	}
	return -1;
}

int main()
{
	freopen("2449.in","r",stdin);
	freopen("2449.out","w",stdout);
	read();
	printf("%d\n",solve());
	return 0;
}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值