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;
}

 

posted @ 2017-09-27 19:29  Excim  阅读(123)  评论(0编辑  收藏  举报