Codeforces Round #420 (Div. 2) D. Okabe and City(最短路)

来源:互联网 发布:js混淆怎么读 编辑:程序博客网 时间:2024/05/16 04:26

D. Okabe and City
time limit per test4 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn’t get beaten up by schoolchildren.

Okabe’s city is represented by a 2D grid of cells. Rows are numbered from 1 to n from top to bottom, and columns are numbered 1 to m from left to right. Exactly k cells in the city are lit by a street lamp. It’s guaranteed that the top-left cell is lit.

Okabe starts his walk from the top-left cell, and wants to reach the bottom-right cell. Of course, Okabe will only walk on lit cells, and he can only move to adjacent cells in the up, down, left, and right directions. However, Okabe can also temporarily light all the cells in any single row or column at a time if he pays 1 coin, allowing him to walk through some cells not lit initially.

Note that Okabe can only light a single row or column at a time, and has to pay a coin every time he lights a new row or column. To change the row or column that is temporarily lit, he must stand at a cell that is lit initially. Also, once he removes his temporary light from a row or column, all cells in that row/column not initially lit are now not lit.

Help Okabe find the minimum number of coins he needs to pay to complete his walk!

Input
The first line of input contains three space-separated integers n, m, and k (2 ≤ n, m, k ≤ 104).

Each of the next k lines contains two space-separated integers ri and ci (1 ≤ ri ≤ n, 1 ≤ ci ≤ m) — the row and the column of the i-th lit cell.

It is guaranteed that all k lit cells are distinct. It is guaranteed that the top-left cell is lit.

Output
Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it’s not possible.

Examples
input
4 4 5
1 1
2 1
2 3
3 3
4 3
output
2
input
5 5 4
1 1
2 1
3 1
3 2
output
-1
input
2 2 4
1 1
1 2
2 1
2 2
output
0
input
5 5 4
1 1
2 2
3 3
4 4
output
3
Note
In the first sample test, Okabe can take the path , paying only when moving to (2, 3) and (4, 4).

In the fourth sample, Okabe can take the path , paying when moving to (1, 2), (3, 4), and (5, 4).
题意:
现在给你N*M的一个矩阵,现在上边一共有K个永恒亮着的点,主人公从左上角出发,走到的点必须有亮光才行。但是现在不保证有亮光的点能够使得主人公到达右下角,所以他可以花费1单位金币去使得一行或者一列暂时性的亮着,如果他想再次使用魔法,那么之前暂时亮着的部分就必须灭掉了。问他最少花费多少金币,能够从左上角走到有下角。如果不能走到,输出-1.
题意: 对于这k个永亮点来说,相邻的是可以免费到达的。距离不超过2的可以通过暂时点亮中间的通过(代价为1)。如果(n,m)点不是一个永亮点,我们还需花费1。
代码:

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<vector>#include<queue>#include<algorithm>#include<map>using namespace std;typedef long long int ll;typedef pair<int,int>pa;const int N=2e5+10;const int MOD=1e9+7;const ll INF=1e18;int read(){    int x=0;    char ch = getchar();    while('0'>ch||ch>'9')ch=getchar();    while('0'<=ch&&ch<='9')    {        x=(x<<3)+(x<<1)+ch-'0';        ch=getchar();    }    return x;}/************************************************************/ll dis[N];int vis[N];int n,m,k;int a[N],b[N];void spfa(){    queue<int>q;    for(int i=1;i<=k;i++)        vis[i]=0,dis[i]=INF;     vis[1]=1,q.push(1),dis[1]=0;     while(!q.empty())     {         int u=q.front();         q.pop();         vis[u]=0;         for(int i=1;i<=k;i++)         {             ll val=INF;             if(i==u) continue;             int dx=abs(a[i]-a[u]);             int dy=abs(b[i]-b[u]);             if(dx+dy==1)                val=0;             else if(dx<=2||dy<=2)                val=1;             if(dis[i]>dis[u]+val)             {                 dis[i]=dis[u]+val;                 if(!vis[i])                 {                 vis[i]=1;                 q.push(i);                 }             }         }     }     if(dis[k]==INF) cout<<"-1"<<endl;     else cout<<dis[k]<<endl;}int main(){    cin>>n>>m>>k;    bool flag=false;    for(int i=1; i<=k; i++)    {        cin>>a[i]>>b[i];        if(a[i]==n&&b[i]==m) flag=true;    }    if(!flag)        a[++k]=n+1,b[k]=m+1;    spfa();}
阅读全文
0 0
原创粉丝点击