第k短路(POJ 2449: Remmarguts' Date)

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 30980 Accepted: 8462

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条边的有向图,给定起点终点和k,求出从起点到终点的第k短路


设起点为S,终点为T,步骤:

①求出反向图中T到所有点的最短路,bet[x]表示从T到x的最短路

②定义结构体s,里面包含x(当前点);d(从S到x总共走的路程);

f(估价函数,f = d+bet[x]

③创建结构体s的优先队列,其中队列顶端f最小(按f从大到小排)

④SPFA,如果当前是第p次到达终点,那么结构体里的d就是第p短路的长度!

复杂度:O(nk)


#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define inf 1044266558
int n, m, k, s, t;
int head[2010], bet[2010], vis[2010], head2[2010], cnt, cnt2;
typedef struct Edge
{
	int to, c;
	int next;
}Edge;
Edge G2[300050], G[300050];
void Add(int a, int b, int c)
{
	cnt++;
	G[cnt].next = head[a];
	head[a] = cnt;
	G[cnt].to = b;
	G[cnt].c = c;
}
void Add1(int a, int b, int c)
{
	cnt2++;
	G2[cnt2].next = head2[a];
	head2[a] = cnt2;
	G2[cnt2].to = b;
	G2[cnt2].c = c;
}
queue<int> q;
typedef struct Res
{
	int f;				//f是估价函数,等于d加上从x到终点T的最短路
	int x, d;			//x是当前到达的点,d表示从起点s到x总共走的路程
	bool operator < (const Res &b) const			//队列的顶端f最小
	{
		if(f>b.f)
			return 1;
		return 0;
	}
}Res;
Res u, v;
priority_queue<Res> vq;
void Solve()
{
	int now, i, tc;
	now = 0;
	u.f = bet[s], u.x = s, u.d = 0;
	vq.push(u);
	while(vq.empty()==0)
	{
		u = vq.top();
		vq.pop();
		if(u.x==t)
			now++;
		if(now==k)
		{
			printf("%d\n", u.d);
			return;
		}
		for(i=head[u.x];i;i=G[i].next)
		{
			tc = G[i].to;
			v = u;
			v.x = tc, v.d += G[i].c, v.f = bet[v.x]+v.d;
			vq.push(v);
		}
	}
	printf("-1\n");
}
int main(void)
{
	int i, x, y, c;
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		memset(head, 0, sizeof(head));
		memset(head2, 0, sizeof(head2));
		cnt = cnt2 = 0;
		for(i=1;i<=m;i++)
		{
			scanf("%d%d%d", &x, &y, &c);
			Add(x, y, c);
			Add1(y, x, c);
		}
		scanf("%d%d%d", &s, &t, &k);
		if(s==t)
			k++;
		memset(bet, 62, sizeof(bet));
		memset(vis, 0, sizeof(vis));
		bet[t] = 0, vis[t] = 1;
		q.push(t);
		while(q.empty()==0)
		{
			x = q.front();
			q.pop();
			vis[x] = 0;
			for(i=head2[x];i;i=G2[i].next)
			{
				y = G2[i].to;
				if(bet[y]>bet[x]+G2[i].c)
				{
					bet[y] = bet[x]+G2[i].c;
					if(vis[y]==0)
					{
						q.push(y);
						vis[y] = 1;
					}
				}
			}
		}
		if(bet[s]==inf)
			printf("-1\n");
		else
		{
			Solve();
			while(vq.empty()==0)
				vq.pop();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值