zoukankan      html  css  js  c++  java
  • P2658 汽车拉力比赛

    luogu 传送门

    这里写图片描述

    标准的二分答案。
    check中,我们用bfs判断以当前的标准是否能拓展出所有的路标。
    注:此题是m行n列。

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cmath> 
    #include<queue>
    #include<vector>
    #include<cstring>
    using namespace std;
    int m,n,a[509][509],num,sx,sy;
    struct H{int x,y;};
    bool f[509][509],f1[509][509];
    int dx[]={0,1,0,-1,0},dy[]={0,0,1,0,-1};
    bool check(int x)
    {
        memset(f1,false,sizeof(f1));
        f1[sx][sy]=1;
        queue <H> q;
        q.push((H){sx,sy});
    
        int s=1;
        while(!q.empty())
        {
            H k=q.front();q.pop();
            for(int i=1;i<=4;i++)
            {
                int tx=k.x+dx[i],ty=k.y+dy[i];
                if(tx>=1&&tx<=m&&ty>=1&&ty<=n)
                if(!f1[tx][ty]&&abs(a[tx][ty]-a[k.x][k.y])<=x)
                {
                    q.push((H){tx,ty});f1[tx][ty]=1;
                    if(f[tx][ty]==true) s++;
                    if(s==num) return 1;
                } 
            }
        }
        return s==num;
    }
    int main()
    {
        scanf("%d%d",&m,&n);
        for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]);
        for(int i=1;i<=m;i++) for(int j=1;j<=n;j++)
        {scanf("%d",&f[i][j]);if(f[i][j]==1) num++,sx=i,sy=j;}
    
        if(!sx&&!sy) {printf("0 0");return 0;} 
        int l=0,r=(int)1e9+1;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(check(mid)) r=mid-1;
            else l=mid+1;
        }
        printf("%d",l);
        return 0;
    }
  • 相关阅读:
    左偏树
    论在Windows下远程连接Ubuntu
    ZOJ 3711 Give Me Your Hand
    SGU 495. Kids and Prizes
    POJ 2151 Check the difficulty of problems
    CodeForces 148D. Bag of mice
    HDU 3631 Shortest Path
    HDU 1869 六度分离
    HDU 2544 最短路
    HDU 3584 Cube
  • 原文地址:https://www.cnblogs.com/dfsac/p/7587842.html
Copyright © 2011-2022 走看看