poj 2449 Remmarguts' Date(K短路,A*算法)

http://poj.org/problem?id=2449

大致题意:给出一个有向图,求从起点到终点的第K短路。

K短路与A*算法详解  学长的博客。。。

算法过程

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#define LL long long
#define _LL __int64
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 1010;
const int maxm = 100010;

struct node
{
	int u,v,w;
};

int s,t,k;
int n,m;
vector <struct node> edge[maxn],edge1[maxn]; //邻接表存图以及反向图
int dis[maxn]; // 终点到所有点的最短路
int time[maxn];// 每个点的出队列次数
int ans;

bool operator > (const struct node &a, const struct node &b)
{
	return a.w+dis[a.v] > b.w + dis[b.v];
}
priority_queue < node, vector<node>, greater<node> >q;

void init()
{
	for(int i = 1; i <= n; i++)
	{
		edge[i].clear();
		edge1[i].clear();
	}
}

//spfa求终点到其他左右点的最短路
void spfa()
{
	int inque[maxn];
	queue<int> que;
	while(!que.empty()) que.pop();
	memset(inque,0,sizeof(inque));
	memset(dis,INF,sizeof(dis));

	dis[t] = 0;
	inque[t] = 1;
	que.push(t);

	while(!que.empty())
	{
		int u = que.front();
		que.pop();
		inque[u] = 0;
		for(int i = 0; i < (int)edge1[u].size(); i++)
		{
			int v = edge1[u][i].v;
			int w = edge1[u][i].w;
			if(dis[v] > dis[u] + w)
			{
				dis[v] = dis[u] + w;
				if(!inque[v])
				{
					inque[v] = 1;
					que.push(v);
				}
			}
		}
	}
}

void solve()
{
	while(!q.empty()) q.pop();
	memset(time,0,sizeof(time));
	struct node tmp;
	bool flag = false;

	//起点进队列
	tmp.v = s;
	tmp.w = 0;
	q.push(tmp);

	while(!q.empty())
	{
		struct node u = q.top();
		q.pop();

		time[u.v]++;
		if(time[u.v] >= k) //出队次数大于等于K时
		{
			if(u.v == t) //如果是终点,判断与起点是否相同
			//若不相同,当前值便是第K短路,否则第K+1次才是最短路
			{
				if(t != s || (t == s && time[u.v] == k+1))
				{
					flag = true;
					ans = u.w;
					break;
				}
			}
			if(time[u.v] > k)//如果不是终点,当出队次数大于K次就不再进队列
				continue;
		}

		int now = u.v;
		for(int i = 0; i < (int)edge[now].size(); i++)
		{
			struct node tmp;
			tmp.v = edge[now][i].v;
			tmp.w = u.w + edge[now][i].w;
			q.push(tmp);
		}
	}
	if(!flag)
		ans = -1;
}

int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		init();
		int u,v,w;
		for(int i = 1; i <= m; i++)
		{
			scanf("%d %d %d",&u,&v,&w);
			edge[u].push_back( (struct node){u,v,w} );
			edge1[v].push_back( (struct node) {v,u,w} );
		}
		scanf("%d %d %d",&s,&t,&k);

		spfa();

		solve();

		printf("%d\n",ans);

	}

	return 0;
}

poj 2449 Remmarguts' Date(K短路,A*算法),布布扣,bubuko.com

poj 2449 Remmarguts' Date(K短路,A*算法)

时间: 2024-10-24 22:24:55

poj 2449 Remmarguts' Date(K短路,A*算法)的相关文章

poj 2449 Remmarguts&#39; Date k短路

/*poj 2449 k短路 A* 估价函数是 s到i的距离+i到t的距离 */ #include<cstdio> #include<queue> #include<vector> #define inf 1e7 #define maxn 100010 using namespace std; int n,m,S,T,K,num1,num2,head1[maxn],head2[maxn],dis[maxn]; int q[maxn],hea,tai,f[maxn],cn

POJ 2449 Remmarguts&#39; Date(k短路模板)

link:https://vjudge.net/problem/POJ-2449 前面输入与大多最短路题相同 最后一行输入s,t,k 求从s到t的第K短路 #include <iostream> #include <cstring> #include <queue> using namespace std; const int MAXN=1010; struct node { int p,g,h; bool operator < (node a) const {

图论(A*算法,K短路) :POJ 2449 Remmarguts&#39; Date

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

POJ 2449 Remmarguts&#39; Date ( 第 k 短路 &amp;&amp; A*算法 )

题意 : 给出一个有向图.求起点 s 到终点 t 的第 k 短路.不存在则输出 -1 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 1024; const int maxm = 100008; struct EDGE{ int v

poj 2449 Remmarguts&#39; Date 求第k短路 Astar算法

=.=好菜 #include <iostream> #include <cstdio> #include <string.h> #include <cstring> #include <queue> using namespace std; const int N = 1e3+10; const int M = 100000+10; typedef long long ll; const ll INF = 1e15; int n,m,head[N

POJ 2449 Remmarguts&#39; Date (第k短路 A*搜索算法模板)

Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 22412   Accepted: 6085 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, h

poj 2449 Remmarguts&#39; Date A*+spfa求第k短路

题意: 经典的第k短路,A*算法的经典应用之一. 分析: A*,已走的路程g+到终点的最短距离为启发函数,搜索过程中不判重,第k次到t节点时就求出了第k短路. 代码: //poj 2449 //sep9 #include <iostream> #include <queue> using namespace std; const int maxN=1024; const int maxM=100024; int n,m,s,t,k,e,ne; int head[maxN],nhea

POJ 2449 Remmarguts&#39; Date ( Dijkstra + A* 求解第K短路 )

#include <iostream> #include <cstring> #include <queue> #include <fstream> using namespace std; #define E 100005 #define V 1005 #define INF 1 << 30 int heads[V], r_heads[V]; int dists[V]; bool visits[V]; int nEdgeNum, nNodeNu

poj 2449 Remmarguts&#39; Date (k短路模板)

Remmarguts' Date http://poj.org/problem?id=2449 Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30772   Accepted: 8397 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly tou