【poj2449】 Remmarguts' Date

http://poj.org/problem?id=2449 (题目链接)

题意

  求有向图K短路。

Solution

  A*。g(x)为当前节点到起点的步数,h(x)为当前节点到终点的最短距离(也就是估价函数)。

细节

  dijkstra求终点到各点最短路时要把边反向。原来起点和终点可以是同一个点,坑死了。。。

代码

// poj2499
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;

const int maxn=100010;
struct edge {int to,next,w;}e[maxn<<1];
int head[maxn],cnts[maxn],dis[maxn],vis[maxn];
int n,m,S,T,K,cnt,U[maxn],V[maxn],W[maxn];

struct data {
	int num,w;
	friend bool operator < (const data a,const data b) {
		return a.w>b.w;
	}
};
struct node {
	int num,g,h;
	friend bool operator < (const node a,const node b) {
		return a.g+a.h>b.g+b.h;
	}
};
void link(int u,int v,int w) {
	e[++cnt]=(edge){v,head[u],w};head[u]=cnt;
}
void Dijkstra() {
	for (int i=1;i<=n;i++) dis[i]=inf;dis[T]=0;
	priority_queue<data> q;
	data x=(data){T,0};q.push(x);
	while (!q.empty()) {
		x=q.top();q.pop();
		if (vis[x.num]) continue;
		vis[x.num]=1;
		for (int i=head[x.num];i;i=e[i].next) if (dis[e[i].to]>x.w+e[i].w) {
				dis[e[i].to]=e[i].w+x.w;
				q.push((data){e[i].to,dis[e[i].to]});
			}
	}
}
int Astar() {
	node x=(node){S,0,dis[S]};
	priority_queue<node> q;q.push(x);
	while (!q.empty()) {
		x=q.top();q.pop();
		cnts[x.num]++;
		if (cnts[x.num]>K) continue;
		if (cnts[T]==K) return x.g;
		for (int i=head[x.num];i;i=e[i].next) q.push((node){e[i].to,x.g+e[i].w,dis[e[i].to]});
	}
	return -1;
}
int main() {
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++) {
		scanf("%d%d%d",&U[i],&V[i],&W[i]);
		link(V[i],U[i],W[i]);
	}
	scanf("%d%d%d",&S,&T,&K);
	if (S==T) K++;
	Dijkstra();
	memset(head,0,sizeof(head));cnt=0;
	for (int i=1;i<=m;i++) link(U[i],V[i],W[i]);
	int ans=Astar();
	printf("%d",ans);
	return 0;
}

  

【poj2449】 Remmarguts' Date

时间: 2024-12-12 02:33:13

【poj2449】 Remmarguts' Date的相关文章

【POJ】【2449】Remmarguts&#39; Date

K短路/A* 经(luo)典(ti) K短路题目= = K短路学习:http://www.cnblogs.com/Hilda/p/3226692.html 流程: 先把所有边逆向,做一遍dijkstra,得到估价函数h(x)(x到T的最短路距离) f(x)=g(x)+h(x) 按f(x)维护一个堆……T第k次出堆时的g(T)即为ans 另外,需要特判:如果S==T,k++ 1 Source Code 2 Problem: 2449 User: sdfzyhy 3 Memory: 11260K T

【POJ 2449】 Remmarguts&#39; Date

[题目链接] http://poj.org/problem?id=2449 [算法] A*(启发式搜索) 首先,求第k短路可以用优先队列BFS实现,当T第k次入队时,就求得了第k短路,但是,这种做法的复杂度太高 考虑使用A*算法,每个点的估价函数就是这个点到T的最短路,不妨将所有的边反过来求最短路,这样就得到了所有点的估价函数 这种算法的复杂度是优秀的 [代码] #include <algorithm> #include <bitset> #include <cctype&g

【备忘录】javascript Date在ios上显示NAN

var dateStr = "2016-01-19 00:00:00"; var d = new Date(dateStr.replace(/-/g, '/')); or 参考:http://stackoverflow.com/questions/13363673/javascript-date-is-invalid-on-ios

【转】lua Date和Time

time和date两个函数在Lua中实现所有的时钟查询功能.函数time在没有参数时返回当前时钟的数值.(在许多系统中该数值是当前距离某个特定时间的秒数.)当为函数调用附加一个特殊的时间表时,该函数就是返回距该表描述的时间的数值.这样的时间表有如下的区间: year a full year month 01-12 day 01-31 hour 01-31 min 00-59 sec 00-59 isdst a boolean, true if daylight saving 前三项是必需的,如果

【转】PHP date(&quot;Y-m-d H:i:s&quot;);获取当前时间 差8小时解决办法

原因:                                                                                              网络资源整理 从php5.1.0开始,php.ini里加了date.timezone这个选项,并且默认情况下是关闭的也就是显示的时间(无论用什么php命令)都是格林威治标准时间和我们的时间(北京时间)差了正好8个小时. 关于timezone 大陆内地可用的值是:Asia/Chongqing ,Asia/S

【疑难杂症】new Date() 造成的线程阻塞问题

代码如下 package com.learn.concurrent.probolem; import java.util.Date; import java.util.concurrent.CountDownLatch; /** * @author wx * @Description * @date 2019/05/12 18:33 */ public class DateProblem { public static void main(String[] args) { new DatePro

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

【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&amp;#39; Date 【SPFA+Astar】【古典】

称号:poj 2449 Remmarguts' Date 意甲冠军:给定一个图,乞讨k短路. 算法:SPFA求最短路 + AStar 以下引用大牛的分析: 首先,为了说话方便,列出一些术语: 在启示式搜索中,对于每一个状态 x.启示函数 f(x) 一般是这种形式: f(x) = g(x) + h(x) 当中 g(x) 是从初始状态走到 x 所花的代价:h(x) 是从 x 走到目标状态所须要的代价的预计值. 相对于 h(x).另一个概念叫 h*(x),表示从 x 走到目标状态所须要的实际最小代价(