jdfz-2978 第k短路(强)

题意:

给出一个n个点m条边的有向图,求这个图点1到点n的严格第K短路;

n<=10000,m<=100000,k<=10000

边权<=10000;

链接


题解:

这是一个似乎十分经典的问题,但是普通的A*算法是会被卡的;

最坏复杂度会达到O(SPFA(n,m)+KMlog(K+M))的(大概);

所以这个算法还需要优化;

主要的算法就是俞鼎力大牛在《堆的可持久化》论文里的东西;

具体的解法详见论文;

只是我使用了可持久化左偏树来实现可并堆的持久化;

我理解的这个算法就是,所有的非最短路径,都是在最短路径树的非树边之间跳跃而来的;

这里的非树边就了中提到的sidetracks;

为了保证路径成立,还要使两条相邻非树边满足“性质1”;

这样的问题转化是十分巧妙的;

而之后的搜索过程和别的A*搜第K大值啥的很相似(还是说别的和这个相似?);

目前的状态是已经选了的边权和最后一次选的边;

可以转移到的状态和别的题差不多(也许可以看这个题解理解一下:Contest Hunter - OVOO)

时间复杂度这就很快了,是O(SPFA(n,m)+mlogm+klogm)吧= =,反正也就是一个log;

然而蒟蒻还是算不明白空间,所以写了指针版(大概是O(m+klogm)的什么鬼);


代码:


#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 11000
#define M 110000
#define pr pair<ll,heap*>
using namespace std;
typedef long long ll;
struct heap
{
	heap *l, *r;
	ll val;
	int dis, no;
	heap();
}*null = new heap(), *H[N];
heap::heap()
{
	l = r = null;
	val = no = 0;
}
heap* Insert(heap* x, heap* y)
{
	if (x == null || y == null)
		return x == null ? y : x;
	if (x->val > y->val)
		swap(x, y);
	x->r = Insert(x->r, y);
	if (x->l->dis < x->r->dis)
		swap(x->l, x->r);
	x->dis = x->r->dis + 1;
	return x;
}
heap* merge(heap* x, heap* y)
{
	if (x == null || y == null)
		return x == null ? y : x;
	if (x->val > y->val)
		swap(x, y);
	heap* p = new heap();
	*p = *x;
	p->r = merge(p->r, y);
	if (p->l->dis < p->r->dis)
		swap(p->l, p->r);
	p->dis = p->r->dis + 1;
	return p;
}
ll dis[N], val[M];
int next[M], from[M], to[M], head[N], pre[N], n, tot;
bool inq[N], isT[M];
queue<int>q;
priority_queue<pr, vector<pr>, greater<pr> >poq;
void add(int x, int y, ll v)
{
	to[++tot] = y;
	val[tot] = v;
	from[tot] = x;
	next[tot] = head[x];
	head[x] = tot;
}
void Spfa()
{
	int x, y, i;
	memset(dis, 0x3f, sizeof(dis));
	q.push(n);
	dis[n] = 0, inq[n] = 1;
	while (!q.empty())
	{
		x = q.front(), q.pop();
		inq[x] = 0;
		for (i = head[x]; i; i = next[i])
		{
			if (dis[y = to[i]] > dis[x] + val[i])
			{
				dis[y] = dis[x] + val[i];
				isT[pre[y]] = 0;
				isT[pre[y] = i] = 1;
				if (!inq[y])
					inq[y] = 1, q.push(y);
			}
		}
	}
}
void Build()
{
	q.push(n);
	int x, y, i;
	for (i = 1; i <= tot; i++)
	{
		if (isT[i])	continue;
		y = to[i];
		heap* temp = new heap();
		temp->no = i, temp->val = val[i] + dis[from[i]] - dis[to[i]];
		H[y] = Insert(H[y], temp);
	}
	while (!q.empty())
	{
		x = q.front(), q.pop();
		for (i = head[x]; i; i = next[i])
		{
			y = to[i];
			if (isT[i])
			{
				H[y] = merge(H[y], H[x]);
				q.push(y);
			}
		}
	}
}
void init()
{
	null->l = null->r = null;
	null->val = null->no = 0;
	null->dis = -1;
	for (int i = 1; i <= n; i++)
		H[i] = null;
}
int main()
{
	int m, i, j, k, x, y;
	ll v, ans;
	heap* now;
	scanf("%d%d%d", &n, &m, &k);
	init();
	for (i = 1; i <= m; i++)
	{
		scanf("%d%d%lld", &x, &y, &v);
		add(y, x, v);
	}
	Spfa();
	Build();
	poq.push(pr(H[1]->val, H[1]));
	for (i = 1, ans = 0; i < k; i++)
	{
		ans = poq.top().first, now = poq.top().second;
		poq.pop();
		v = now->val;
		heap* temp = merge(now->l, now->r);
		if (temp != null)
		{
			poq.push(pr(ans - v + temp->val, temp));
		}
		poq.push(pr(ans + H[from[now->no]]->val, H[from[now->no]]));
	}
	printf("%lld\n", ans + dis[1]);
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值