【BZOJ 4070】 [Apio2015]雅加達的摩天樓,bzojapio2015

來源:互聯網
上載者:User

【BZOJ 4070】 [Apio2015]雅加達的摩天樓,bzojapio2015
4070: [Apio2015]雅加達的摩天樓

Time limit: 1000 ms

Memory limit: 262144 KB

Description

The city of Jakarta has N skyscrapers located on a line, conveniently numbered 0 through N-1 from left to right. There are no other skyscrapers in Jakarta.

Jakarta is inhabited by M mystical creatures called “doge”s. The doges are conveniently numbered 0 through M-1. Doge i initially resides in skyscraper Bi. Doge i has a mystical power, represented with a positive integer Pi. This mystical power enables doges to jump between skyscrapers. In a single jump, a doge with superpower p that is currently in skyscraper b can move to either skyscraper b+p (if 0 ≤ b+p < N) or skyscraper b-p (if 0 ≤ b-p < N).

Doge 0 is the most awesome doge, and it is the leader of all the doges. It has an urgent news for doge 1, and wants the news to reach doge 1 as quickly as possible. Any doge that has received the news can do any of the following actions:

Make a jump to move to some other skyscraper.
Pass the news to another doge in the same skyscraper.
Please help the doges by calculating the minimum number of total jumps required by all doges to pass the news to doge 1, or if it is impossible to do so.

Input Format

The first line contains two integers N and M. Each of the next M lines contains two integers Bi and Pi.

Output Format

A single line containing the minimum number of total jumps, or -1 if it is impossible.

Sample Input

5 3
0 2
1 1
4 1
Sample Output

5
Explanation

Here is one of the possible scenarios to pass the news using 5 jumps:

Doge 0 jumps to skyscraper 2 and then to skyscraper 4 (2 jumps).
Doge 0 passes the news to doge 2.
Doge 2 jumps to skyscraper 3, and then to skyscraper 2, and then to skyscraper 1 (3 jumps).
Doge 2 passes the news to doge 1.
Subtasks

For each subtask,

0 ≤ Bi < N
Subtask 1 (10 points)

1 ≤ N ≤ 10
1 ≤ Pi ≤ 10
2 ≤ M ≤ 3
Subtask 2 (12 points)

1 ≤ N ≤ 100
1 ≤ Pi ≤ 100
2 ≤ M ≤ 2,000
Subtask 3 (14 points)

1 ≤ N ≤ 2,000
1 ≤ Pi ≤ 2,000
2 ≤ M ≤ 2,000
Subtask 4 (21 points)

1 ≤ N ≤ 2,000
1 ≤ Pi ≤ 2,000
2 ≤ M ≤ 30,000
Subtask 5 (43 points)

1 ≤ N ≤ 30,000
1 ≤ Pi ≤ 30,000
2 ≤ M ≤ 30,000

分塊+最短路。

這道題如果直接跑最短路,最壞可能會產生n2條邊。

考慮分塊:
①Pi>n‾‾√
可以直接暴力來做,因為最多隻會有nn‾‾√條邊

②Pi≤n‾‾√
從每個點來看,最多隻有n‾‾√種走法,所以一共只有nn‾‾√條邊。

我們可以在後面添加一些輔助點來實現預先處理:
枚舉這≤n‾‾√個長度,在後面分別建出n個輔助點,連向前面對應的位置,同時連向可以來自和到達的輔助點。

對於讀入直接連向他對應的輔助點即可。
(詳見代碼)

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <cstring>#include <cstdlib>#include <queue>#include <vector>#define M 30005#define pb push_back#define LL long longusing namespace std;queue<int> q;int B,tot=0,cnt,s,t,h[M*110],m,n,inq[M*110];int d[M*110],pos[105][M];struct data{    int b,p;}a[M];struct edge{    int y,ne,v;}e[M*500];void Addedge(int x,int y,int v){    e[++tot].y=y;    e[tot].v=v;    e[tot].ne=h[x];    h[x]=tot;}void SPFA(){    for (int i=0;i<=cnt;i++)        d[i]=1e9,inq[i]=0;    q.push(a[0].b);    d[a[0].b]=0,inq[a[0].b]=1;    while (!q.empty())    {        int x=q.front();        q.pop();        inq[x]=0;        for (int i=h[x];i;i=e[i].ne)        {            int y=e[i].y;            if (d[y]>d[x]+e[i].v)            {                d[y]=d[x]+e[i].v;                if (!inq[y]) q.push(y),inq[y]=1;            }        }    }}int main(){    scanf("%d%d",&n,&m);    for (int i=0;i<m;i++)        scanf("%d%d",&a[i].b,&a[i].p);    cnt=n-1;    B=min((int)sqrt(n),100);    for (int i=1;i<=B;i++)        for (int j=0;j<i;j++)            for (int k=j;k<n;k+=i)            {                pos[i][k]=++cnt;                Addedge(cnt,k,0);                if (k>=i)                {                    Addedge(cnt,cnt-1,1);                    Addedge(cnt-1,cnt,1);                }            }    for (int i=0;i<m;i++)    {        if (a[i].p<=B)            Addedge(a[i].b,pos[a[i].p][a[i].b],0);        else        {            for (int j=1;;j++)                if (j*a[i].p+a[i].b>=n) break;                else Addedge(a[i].b,j*a[i].p+a[i].b,j);            for (int j=1;;j++)                if (a[i].b-j*a[i].p<0) break;                else Addedge(a[i].b,a[i].b-j*a[i].p,j);        }    }    SPFA();    if (d[a[1].b]==1e9)        cout<<-1<<endl;    else cout<<d[a[1].b]<<endl;    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.