zoukankan      html  css  js  c++  java
  • luogu P1661 扩散

    原题链接:https://www.luogu.org/problem/show?pid=1661

    虽然此题并没有非常明显的边的概念,甚至只从数据无法直接得到任意点的距离。

    但是经过模拟之后,就能发现,假设两点同时沿着一条线外扩散,他们共同走过的距离,就是他们纵坐标之差与横坐标之差的和。

    因为两边是同时走的,所以时间只有距离的一半,而且要向上取整。

    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int cnt,tot,n,f[55],ans;
    struct edge
    {
        int u,v,w;
    }e[3005];
    bool cmp(edge x,edge y)
    {
        return x.w<y.w;
    }
    int find(int x)
    {
        if(f[x]==x) return x;
        return f[x]=find(f[x]);
    }
    int max(int x,int y)
    {
        return x>y ? x : y;
    }
    int abs(int x)
    {
        return x<0 ? -x : x;
    } 
    int main()
    {
        int x[55],y[55];
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d %d",&x[i],&y[i]);
        for(int i=1;i<n;i++)
        {
            f[i]=i;
            for(int j=i+1;j<=n;j++)
            {
                e[++cnt].u=i;
                e[cnt].v=j;
                e[cnt].w=abs(x[i]-x[j])+abs(y[i]-y[j]);
            }
        }
        sort(e+1,e+cnt+1,cmp);
        for(int i=1;i<=cnt;i++)
        {
            if(find(e[i].v)==find(e[i].u)) continue;
            f[find(e[i].u)]=e[i].v;
            tot++;
            ans=e[i].w;
            if(tot==n-1) break;
        }
        printf("%d",(1+ans)/2);
        return 0;
    }
  • 相关阅读:
    根据界面上的button增加、删除、重命名文件夹,名字是数据库下面某一表单的某一列的名字
    打包测试的过程记录
    java中return的作用
    UVA
    UVA
    UVA
    HDU
    HDU
    spring技术详解
    Java对象的生命周期与垃圾回收以及四种引用
  • 原文地址:https://www.cnblogs.com/zeroform/p/7603466.html
Copyright © 2011-2022 走看看