poj2449 Remmarguts' Date,第K短路

点击打开链接

SPFA  + A*

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;

struct node {
	int v, dis, f, next;
	friend bool operator <(node a, node b){
		return a.f>b.f;
	}
};

const int INF = 1e9;
const int maxn = 1005;
const int maxm = 100005;
node edge[maxm], edgef[maxm];
int head[maxn], e, headf[maxn], dis[maxn], n, m, s, t, k;
void Add(int u, int v, int dis){
	edge[e].v = v;
	edge[e].dis = dis;
	edge[e].next = head[u];
	head[u] = e;

	edgef[e].v = u;
	edgef[e].dis = dis;
	edgef[e].next = headf[v];
	headf[v] = e++;
}

void init(){
	int i, u, v, dis;
	memset(head, -1, sizeof head );
	memset(headf, -1, sizeof headf );
	e = 0;
	for(i=1; i<=m; ++i){
		scanf("%d%d%d", &u, &v, &dis);
		Add(u, v, dis);
	}
}

int cnt[maxn], vis[maxn];

void SPFA(int s, int t){
	int u, v, i, j;
	queue<int> q;
	memset(vis, 0, sizeof vis );
	memset(cnt, 0, sizeof cnt );
	for(i=0; i<=n; ++i) dis[i] = INF;
	vis[s]=1; dis[s]=0; q.push(s);
	while(!q.empty()){
		u = q.front();
		q.pop();
		vis[u] = 0;
		cnt[u]++;
		if(cnt[u]>=n) return ;
		for(j=headf[u];~j;j=edgef[j].next)
			if(dis[v=edgef[j].v]>dis[u]+edgef[j].dis){
				dis[v] = dis[u] + edgef[j].dis;
				if(!vis[v]) vis[v]=1,q.push(v);
			}
	}
}

int A_STAR(int s, int t, int k){
	int cnt=0, i;
	node e, next;
	priority_queue<node> q;
	if(s==t) k++;
	if(dis[s]==INF) return -1;
	e.v = s; e.dis = 0; e.f = dis[s]; q.push(e);
	while(!q.empty()){
		e = q.top();
		q.pop();
		if(e.v==t&&++cnt==k) return e.dis;
		for(i=head[e.v]; ~i; i=edge[i].next){
			next.v = edge[i].v;
			next.dis = e.dis + edge[i].dis;
			next.f = next.dis + dis[next.v];
			q.push(next);
		}
	}
	return -1;
}

void solve(){
	scanf("%d%d%d", &s, &t, &k);
	SPFA(t, s);
	printf("%d\n", A_STAR(s, t, k));
}

int main(){
	while(~scanf("%d%d", &n, &m)){
		init();
		solve();
	}
	return 0;
}

参考链接:

http://www.cnblogs.com/vongang/archive/2012/07/17/2594737.html

http://yzmduncan.iteye.com/blog/1162759

http://ycool.com/post/krb8pah

poj2449 Remmarguts' Date,第K短路

时间: 2024-10-06 09:52:12

poj2449 Remmarguts' Date,第K短路的相关文章

POJ2449 Remmarguts&#39; Date 【k短路】

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

POJ2449 Remmarguts&#39; Date 第K短路

POJ2449 比较裸的K短路问题 K短路听起来高大上 实际思路并不复杂 首先对终点t到其他所有点求最短路 即为dist[] 然后由起点s 根据当前走过的距离+dist[]进行A*搜索 第k次到达t即为第K短路 代码也很简单 //数组开的不够 不一定是运行时错误! 可能也会WA #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath&g

【POJ】2449 Remmarguts&#39; Date(k短路)

http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k 首先我们建反向边,跑一次从汇到源的最短路,将跑出来的最短路作为估价函数h 根据f=g+h 我们将源s先走,此时实际价值g为0,估价为最短路(他们的和就是s-t的最短路) 将所有s所连的边都做相同的处理,加入到堆中(假设此时到达的点为x,那么x的g等于s到这个点的边权,因为根据最优,g+h此时是从x

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(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> #in

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

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

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

POJ2449 Remmarguts&#39; Date

题目链接:http://poj.org/problem?id=2449 题目描述: 其实题目的大意就是求 第k短路, 存在就输出, 不存在就输出-1. 注意当起点和终点一致的时候,需要k++, 因为在OUTPUT时提到the length (time required) to welcome Princess Uyuw using the K-th shortest path. 可以看出是需要时间的,并且并没有描述相同点直接的时间为0这一条件,因此在计算路径的时候,为0的路径是不能够算到里面的: