【Astar】poj2449 Remmarguts' Date

题目点这里


纯属练代码の题。。orz估价函数是反向spfa后到目标点的距离

神奇的是我这渣代码竟然还是statu第12?


#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

int read()
{
	int sign = 1, n = 0; char c = getchar();
	while(c < '0' || c > '9'){ if(c == '-') sign = -1; c = getchar(); }
	while(c >= '0' && c <= '9') { n = n*10 + c-'0'; c = getchar(); }
	return sign*n;
}

const int maxN = 1005;
const int maxM = 100005;

int N, M;
int S, T, K;

struct ed{
	int v, w, next;
}e[maxM * 2]; 
int k = 1, head[maxN], heee[maxN];

inline void adde(int u, int v, int w)
{
	e[k] = (ed){ v, w, head[u] };
	head[u] = k++;
	e[k] = (ed){ u, w, heee[v] };
	heee[v] = k++; 
}

int dis[maxN];
bool vis[maxN];

queue <int> q;

inline void spfa(int s, int t)
{
	memset(dis, 0x3f, sizeof(dis));
	q.push(s); dis[s] = 0;
	
	while(q.size())
	{
		int u = q.front(); q.pop(); vis[u] = 0;
		for(int i = heee[u]; i; i = e[i].next)
		{
			int v = e[i].v;
			if(dis[v] > dis[u] + e[i].w)
			{
				dis[v] = dis[u] + e[i].w;
				if(!vis[v]) { vis[v] = 1; q.push(v); }
			} 
		}
	}
} 

struct node{
	int n, g, h;
	bool operator < (const node &b) const{ return g + h > b.g + b.h; }
};

priority_queue <node> Q;

int Astar()
{
	Q.push((node){ S, 0, dis[S] });
	int cnt = 0;
	
	while(Q.size())
	{
		node temp = Q.top(); Q.pop();
		int u = temp.n;
		if(u == T && ++cnt == K) return temp.g;
		
		for(int i = head[u]; i; i = e[i].next)
		{
			int v = e[i].v;
			node ex = (node){ v, temp.g + e[i].w, dis[v] };
			Q.push(ex);
		} 
	}
	
	return -1;
}

int main()
{
	N = read(); M = read();
	for(int i = 1; i <= M; ++i)
	{
		int u = read(), v = read(), w = read();
		adde(u, v, w);
	}
	S = read(); T = read(); K = read();
	spfa(T, S); if(T == S) ++K;
	
	printf("%d\n", Astar());
	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值