【NOIP2016A Group Simulation 7.13】The Palace of King Arthur

topic

write picture description here

analyze

We define \(dis_{x,y,x1,y2}\) to represent the distance from \((x,y)\) to \((x1,y1)\) . Use spfa for this.
Next, enumerate the two rendezvous points \((x,y), (x1,y1)\) , get the distance from these two rendezvous points to the knight, put \(dis1\) and \(dis2\) .
Then consider greed,
assuming that all \(dis1\) are selected , and the sum is \(sum\) . Sort \(dis2- dis1 \) from small to large, add the value of the previous \(\dfrac{n}{2}\) to \(sum\) , the answer is \(\min(sum)\)

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxlongint=2147483647;
const int mo=1000000007;
const int N=205;
using namespace std;
int dis[21][21][21][21],a[N],n,m,r,c,ans=maxlongint,b[N][2],d[N*100][4],tot;
int z[8][2]=
{
    {2,1},
    {2,-1},
    {-2,1},
    {-2,-1},
    {1,2},
    {1,-2},
    {-1,-2},
    {-1,2}
};
bool bz[21][21];
int spfa(int x,int y)
{
    int head=0,tail=1,xx,yy;
    d[1][1]=x;
    d[1][2]=y;
    dis[x][y][x][y]=0;
    while(head<tail)
    {
        xx=d[++head][1];
        yy=d[head][2];
        bz[xx][yy]=true;
        for(int i=0;i<=7;i++)
        {
            if(dis[x][y][xx+z[i][0]][yy+z[i][1]]>dis[x][y][xx][yy]+1)
            {
                dis[x][y][xx+z[i][0]][yy+z[i][1]]=dis[x][y][xx][yy]+1;
                if(bz[xx+z[i][0]][yy+z[i][1]])
                {
                    bz[xx+z[i][0]][yy+z[i][1]]=false;
                    d[++tail][1]=xx+z[i][0];
                    d[tail][2]=yy+z[i][1];
                }
            }

        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&r,&c);
    for(int i=1;i<=n;i++)
        for(int j=0;j<=1;j++)
            scanf("%d",&b[i][j]);
    memset(dis,43,sizeof(dis));
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++)
        {
            memset(bz,true,sizeof(bz));
            spfa(i,j);
        }
    for(int x=1;x<=r;x++)
        for(int y=1;y<=c;y++)
        {
            int sum=0;
            for(int i=1;i<=n;i++)
            {
                sum+=dis[x][y][b[i][0]][b[i][1]];
            }
            int o=sum;
            for(int xx=1;xx<=r;xx++)
                for(int yy=1;yy<=c;yy++)
                    if(x!=xx || y!=yy)
                    {
                        sum=o;
                        tot=0;
                        for(int i=1;i<=n;i++)
                        {
                            a[++tot]=dis[xx][yy][b[i][0]][b[i][1]]-dis[x][y][b[i][0]][b[i][1]];
                        }
                        sort(a+1,a+tot+1);
                        for(int i=1;i<=n/2;i++)
                        {
                            sum+=a[i];
                        }
                        if(sum<ans)
                            ans=sum;
                    }
        }
    printf("%d",ans);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326032634&siteId=291194637