4070: [Apio2015]雅加达的摩天楼

13 篇文章 0 订阅

4070: [Apio2015]雅加达的摩天楼

Time Limit: 10 Sec   Memory Limit: 256 MB
Submit: 415   Solved: 146
[ Submit][ Status][ Discuss]

Description

 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1。除了这 N 座摩天楼外,雅加达市没有其他摩天楼。

有 M 只叫做 “doge” 的神秘生物在雅加达市居住,它们的编号依次是 0 到 M−1。编号为 i 的 doge 最初居住于编号为 Bi 的摩天楼。每只 doge 都有一种神秘的力量,使它们能够在摩天楼之间跳跃,编号为 i 的 doge 的跳跃能力为 Pi (Pi>0)。
在一次跳跃中,位于摩天楼 b 而跳跃能力为 p 的 doge 可以跳跃到编号为 b−p (如果 0≤b−p<N)或 b+p (如果 0≤b+p<N)的摩天楼。
编号为 0 的 doge 是所有 doge 的首领,它有一条紧急的消息要尽快传送给编 号为 1 的 doge。任何一个收到消息的 doge 有以下两个选择:
跳跃到其他摩天楼上;
将消息传递给它当前所在的摩天楼上的其他 doge。
请帮助 doge 们计算将消息从 0 号 doge 传递到 1 号 doge 所需要的最少总跳跃步数,或者告诉它们消息永远不可能传递到 1 号 doge。

Input

输入的第一行包含两个整数 N 和 M。

接下来 M 行,每行包含两个整数 Bi 和 Pi。

Output

输出一行,表示所需要的最少步数。如果消息永远无法传递到 1 号 doge,输出 −1。

Sample Input

5 3
0 2
1 1
4 1

Sample Output

5
explanation
下面是一种步数为 5 的解决方案:
0 号 doge 跳跃到 2 号摩天楼,再跳跃到 4 号摩天楼(2 步)。
0 号 doge 将消息传递给 2 号 doge。
2 号 doge 跳跃到 3 号摩天楼,接着跳跃到 2 号摩天楼,再跳跃到 1 号摩天楼(3 步)。
2 号 doge 将消息传递给 1 号 doge。

HINT

 子任务


所有数据都保证 0≤Bi<N。


子任务 1 (10 分)

1≤N≤10

1≤Pi≤10

2≤M≤3

子任务 2 (12 分)

1≤N≤100

1≤Pi≤100

2≤M≤2000

子任务 3 (14 分)

1≤N≤2000

1≤Pi≤2000

2≤M≤2000

子任务 4 (21 分)

1≤N≤2000

1≤Pi≤2000

2≤M≤30000

子任务 5 (43 分)

1≤N≤30000

1≤Pi≤30000

2≤M≤30000

Source

[ Submit][ Status][ Discuss]

很容易想到用spfa解决
但是暴力建图是O(n^2)条边,TLE没商量--
学习了网上的解法,对于p分块
如果p>=sqrt,那么这样的点暴力建边,一共nsqrt条
否则对于每栋摩天大楼,我们假象它真的变成一栋摩天大楼~
对于每栋楼建sqrt层,第一层代表p = 1时的走法,第二层......
最底层就代表doge居住的地方
对于楼上的每层,前后顺次连边,即第i个点的第k层向第i-k点的第k层及i+k点的第k层连边,权值为1
我们假象doge正常都是在最底层跳,一般是不上楼的,,
但是如果doge的p值很小,那么我们就让它先上楼再跳
这样建边一共nsqrt条

苟蒻第一发代码常数简直巨大。。。。去掉vector和pairing_heap_tag以后就跑得飞起了。。
迷之STL
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

/*struct data{
	int x,y;
	data(){}
	data(int x,int y): x(x),y(y){}
	bool operator < (const data &B) const {return y > B.y;}
};*/

struct E{
	int to,w;
	E(){}
	E(int to,int w): to(to),w(w){}
};

const int maxn = 3E4 + 30;
const int maxm = maxn*450;
const int INF = ~0U>>1;
//typedef __gnu_pbds::priority_queue<data,less<data>,__gnu_pbds::pairing_heap_tag> Heap;

int n,m,cnt,s,t,Sqrt,tot,dis[maxn*110],pos[110][maxn]
	,tail[maxm],to[maxm],va[maxm],from[maxm];
bool inq[maxm];

//Heap Q;
//Heap::point_iterator id[maxm];
queue <int> Q2;

int getint()
{
	char ch = getchar();
	int ret = 0;
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9')
		ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}

void Add(int x,int y,int w)
{
	from[++tot] = tail[x];
	tail[x] = tot;
	to[tot] = y;
	va[tot] = w;
}

void Build()
{
	for (int i = 1; i <= Sqrt; i++)
		for (int j = 0; j < i; j++)
			for (int k = j; k < n; k += i) {
				pos[i][k] = ++cnt;
				if (k - i >= 0) Add(cnt,cnt - 1,1);
				if (k + i < n) Add(cnt,cnt + 1,1);
				Add(cnt,k,0);
			}
			
	for (int i = 0; i < m; i++) {
		int B = getint(),P = getint();
		if (!i) s = B;
		if (i == 1) t = B;
		if (P > Sqrt) {
			int Nex = B + P,w = 1;
			while (Nex < n) {
				Add(B,Nex,w);
				Nex += P; ++w;
			} 
			Nex = B - P; w = 1;
			while (Nex >= 0) {
				Add(B,Nex,w);
				Nex -= P; ++w;
			}
		}
		else Add(B,pos[P][B],0);
	}
}

int main()
{
	//freopen("4070.in","r",stdin);
	//freopen("4070.out","w",stdout);
	
	cin >> n >> m;
	Sqrt = min((int)sqrt(n),100);
	cnt = n - 1;
	Build();
	for (int i = 0; i <= cnt; i++) dis[i] = INF;
	
	
	/*id[s] = Q.push(data(s,0));
	inq[s] = 1; dis[s] = 0;	
	while (!Q.empty()) {
		data k = Q.top(); Q.pop();
		for (int i = 0; i < v[k.x].size(); i++) {
			int to = v[k.x][i].to;
			int w = v[k.x][i].w;
			if (dis[to] > dis[k.x] + w) {
				dis[to] = dis[k.x] + w;
				if (!inq[to]) {
					inq[to] = 1;
					id[to] = Q.push(data(to,dis[to]));
				}
				else Q.modify(id[to],data(to,dis[to]));
			}
		}
	}*/
	
	Q2.push(s);
	inq[s] = 1;
	dis[s] = 0;
	while (!Q2.empty()) {
		int k = Q2.front(); 
		inq[k] = 0; Q2.pop();
		for (int i = tail[k]; i; i = from[i]) {
			int nex = to[i];
			int w = va[i];
			if (dis[nex] > dis[k] + w) {
				dis[nex] = dis[k] + w;
				if (!inq[nex]) Q2.push(nex),inq[nex] = 1;
			}
		}
	}
	if (dis[t] < INF) cout << dis[t];
	else cout << -1;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值