BZOJ3890【A*算法】

/* I will wait for you*/

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <string>
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;

const int maxn = 100010;
const int maxm = 1010;
const int maxs = 26;
const int inf = 0x3c3c3c3c;
const int P = 1000000007;
const double error = 1e-9;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (ch <= 47 || ch >= 58)
		f = (ch == 45 ? -1 : 1), ch = getchar();
	while (ch >= 48 && ch <= 57)
		x = x * 10 + ch - 48, ch = getchar();
	return x * f;
}

struct edge
{
	int v, w, next;
} e1[maxn], e2[maxn];

struct node
{
	int f, dis, hope;
};

bool operator < (node a, node b)
{
	return a.dis > b.dis || a.dis == b.dis && a.hope < b.hope;
}

int n, m, cnt1, cnt2, top, head1[maxn], head2[maxn], 
    dis[maxn], u[maxn], v[maxn], vis[maxn], q[maxn], 
    w1[maxn], w2[maxn], ans[maxn], ans1[maxn], ans2[maxn];

void insert(int u, int v, int w)
{
	e1[cnt1] = (edge) {v, w, head1[u]}, head1[u] = cnt1++;
	e2[cnt2] = (edge) {u, w, head2[v]}, head2[v] = cnt2++;
}

void spfa()
{
	memset(dis, 0x3c, sizeof dis);
	memset(vis, 0, sizeof vis);

	int a = 0, b = 1;
	dis[n] = 0, q[0] = n;

	while (a != b) {
		int u = q[a++];
		vis[u] = 0, a %= maxn;

		for (int i = head2[u]; i != -1; i = e2[i].next) {
			int v = e2[i].v;

			if (dis[v] > dis[u] + e2[i].w) {
				dis[v] = dis[u] + e2[i].w;
				if (!vis[v])
					vis[v] = 1, q[b++] = v, b %= maxn;
			}
		}
	}
	
	if (dis[1] == inf)
		return;
	
	priority_queue<node> q;

	q.push((node) {1, 0, dis[1]});

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

		if (u.f == n)
			ans[++top] = u.dis;
		
		for (int i = head1[u.f]; i != -1; i = e1[i].next) {			
			int v = e1[i].v, tmp = u.dis + e1[i].w;
			q.push((node) {v, tmp, tmp + dis[v]});
		}
	}
}

int main()
{
	n = read(), m = read();

	for (int i = 1; i <= m; i++) {
		u[i] = read(), v[i] = read();
		w1[i] = read(), w2[i] = read();
	}

	cnt1 = cnt2 = top =0;
	memset(head1, -1, sizeof head1);
	memset(head2, -1, sizeof head2);

	for (int i = 1; i <= m; i++)
		insert(u[i], v[i], w1[i]);

	spfa();

	for (int i = 1; i <= top; i++)
		ans1[i] = ans[i];

	cnt1 = cnt2 = top =0;
	memset(head1, -1, sizeof head1);
	memset(head2, -1, sizeof head2);

	for (int i = 1; i <= m; i++)
		insert(u[i], v[i], w2[i]);

	spfa();

	for (int i = 1; i <= top; i++)
		ans2[i] = ans[i];

	int res = inf;

	for (int i = 1; i <= 20; i++)
		for (int j = 1; j <= 20; j++)
			if (ans1[i] == ans2[j] && ans1[i])
				res = min(res, ans1[i]);

	if (res == inf)
		printf("IMPOSSIBLE");
	else
		printf("%d\n", res);
		
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值