码迷,mamicode.com
首页 > 其他好文 > 详细

我再也不相信正解了……

时间:2019-10-05 22:35:35      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:tmp   oid   cout   ||   code   val   air   lin   using   

RT,正解居然是暴搜,我用的是Hamilton,结果炸了

Code

Mine

#include<bits/stdc++.h>
#pragma GCC optimize("O2")
#define fr(i,a,b) for(int i=a;i<=b;++i)
#define rf(i,a,b) for(int i=a;i>=b;--i)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define N 305
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s,p;
int x[N],y[N],mp[N][N],mx[N][N];
inline int read(){int fh=0;char c=getchar();while(!isdigit(c))c=getchar();while(isdigit(c))fh=(fh<<1)+(fh<<3)+(c^48),c=getchar();return fh;}

int f[5][N][N];
bool valid(int x,int y){return (x>0&&x<=n&&y>0&&y<=m&&mp[x][y]==0);}
bool can(int k,int x,int y){
    if(!valid(x-k,y-k)||!valid(x+k,y+k))return false;
    fr(i,y-k,y+k)if(mp[x-k][i]||mp[x+k][i])return false;
    fr(i,x-k,x+k)if(mp[i][y-k]||mp[i][y+k])return false;
    return true;
}
void pre2(){
    fr(i,1,n)
        fr(j,1,m){
         int k=0;
         while(can(k,i,j))k++;
         mx[i][j]=min(k-1,s);
     }
}
int bs[N][N][2],fx[]={1,0,-1,0},fy[]={0,1,0,-1},dis[20][20][2];
struct node{int x,y,s1,s2;};

#define pr pair<int,int>
#define fs first
#define sc second
queue<pr> q;
bool b[N][N];
void bfs(int sx,int sy,int ind){
    while(!q.empty())q.pop();
    fr(i,1,n)fr(j,1,m)bs[i][j][0]=inf,b[i][j]=0;
    q.push(make_pair(sx,sy));bs[sx][sy][0]=0;bs[sx][sy][1]=0;b[sx][sy]=1;
    while(!q.empty()){
        pr tmp=q.front();q.pop();
        b[tmp.fs][tmp.sc]=0;
        fr(i,0,3){
            int nx=tmp.fs+fx[i],ny=tmp.sc+fy[i];
            if(!valid(nx,ny))continue;
            int ns1=bs[tmp.fs][tmp.sc][0]+1,ns2=bs[tmp.fs][tmp.sc][1]+mx[nx][ny];
            if(bs[nx][ny][0]>ns1){
                bs[nx][ny][0]=ns1;bs[nx][ny][1]=ns2;
                if(!b[nx][ny])q.push(make_pair(nx,ny)),b[nx][ny]=1;
            }else if(bs[nx][ny][0]==ns1&&bs[nx][ny][1]<ns2){
                bs[nx][ny][1]=ns2;
                if(!b[nx][ny])q.push(make_pair(nx,ny)),b[nx][ny]=1;
            }
        }
    }
    fr(i,0,p)dis[ind][i][0]=bs[x[i]][y[i]][0],dis[ind][i][1]=bs[x[i]][y[i]][1];
}
const int P=20;
int ff[P][1<<P][2];
void hamilton(){
    ++p;
    fr(i,0,p)fr(j,0,(1<<p)-1)ff[i][j][0]=inf;
    ff[0][1][0]=ff[0][1][1]=0;
    fr(i,1,(1<<p)-1)fr(j,0,p)
     if(i>>j&1)
      fr(k,0,p)
       if((i^(1<<j))>>k&1)
        if(ff[j][i][0]>=ff[k][i^(1<<j)][0]+dis[j][k][0]){
            if(ff[j][i][0]>ff[k][i^(1<<j)][0]+dis[j][k][0])ff[j][i][1]=ff[k][i^(1<<j)][1]+dis[j][k][1];
            else ff[j][i][1]=max(ff[j][i][1],ff[k][i^(1<<j)][1]+dis[j][k][1]);
            ff[j][i][0]=ff[k][i^(1<<j)][0]+dis[j][k][0];
        }
    int ans=1e9,ans2=0;
    fr(i,0,p-1)
            if(ans>ff[i][(1<<p)-1][0])ans=ff[i][(1<<p)-1][0],ans2=ff[i][(1<<p)-1][1];
            else if(ans==ff[i][(1<<p)-1][0])ans2=max(ans2,ff[i][(1<<p)-1][1]);
    cout<<ans<<" "<<ans2+mx[x[0]][y[0]]<<endl;
}
bool vis[N];
int ans=1e9,lim;
int main(){
    freopen("expand.in","r",stdin);
    //freopen("expand.out","w",stdout);
    n=read(),m=read(),s=read();
    fr(i,1,n)fr(j,1,m)mp[i][j]=read();
    pre2();
    x[0]=read()+1,y[0]=read()+1,p=read();
    fr(i,1,p)x[i]=read()+1,y[i]=read()+1;
    fr(i,0,p)bfs(x[i],y[i],i);
    hamilton();
    //solve();
    return 0;
}

正解

#include<bits/stdc++.h>
#pragma GCC optimize("O2")
#define fr(i,a,b) for(int i=a;i<=b;++i)
#define rf(i,a,b) for(int i=a;i>=b;--i)
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define N 305
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s,p;
int x[N],y[N],mp[N][N],mx[N][N];
inline int read(){int fh=0;char c=getchar();while(!isdigit(c))c=getchar();while(isdigit(c))fh=(fh<<1)+(fh<<3)+(c^48),c=getchar();return fh;}

int f[5][N][N];
void pre1(){
    fr(i,1,n)fr(j,1,m) if(!mp[i][j])f[1][i][j]=min(s,min(f[1][i-1][j],min(f[1][i][j-1],f[1][i-1][j-1]))+1);
    rf(i,n,1)rf(j,m,1) if(!mp[i][j])f[2][i][j]=min(s,min(f[2][i+1][j],min(f[2][i][j+1],f[1][i+1][j+1]))+1);
    fr(i,1,n)rf(j,m,1) if(!mp[i][j])f[3][i][j]=min(s,min(f[3][i-1][j],min(f[3][i][j+1],f[3][i-1][j+1]))+1);
    rf(i,n,1)fr(j,1,m) if(!mp[i][j])f[4][i][j]=min(s,min(f[4][i+1][j],min(f[4][i][j-1],f[4][i+1][j-1]))+1);
    fr(i,1,n)fr(j,1,m) if(!mp[i][j])mx[i][j]=max(0,min(min(f[1][i][j],f[2][i][j]),min(f[3][i][j],f[4][i][j]))-1);
}
bool valid(int x,int y){return (x>0&&x<=n&&y>0&&y<=m&&mp[x][y]==0);}
bool can(int k,int x,int y){
    if(!valid(x-k,y-k)||!valid(x+k,y+k))return false;
    fr(i,y-k,y+k)if(mp[x-k][i]||mp[x+k][i])return false;
    fr(i,x-k,x+k)if(mp[i][y-k]||mp[i][y+k])return false;
    return true;
}
void pre2(){
    fr(i,1,n)
        fr(j,1,m){
         int k=0;
         while(can(k,i,j))k++;
         mx[i][j]=min(k-1,s);
     }
}
int bs[N][N][2],fx[]={1,0,-1,0},fy[]={0,1,0,-1},dis[20][20][2];
struct node{int x,y,s1,s2;};

#define pr pair<int,int>
#define fs first
#define sc second
queue<pr> q;
bool b[N][N];
void bfs(int sx,int sy,int ind){
    while(!q.empty())q.pop();
    fr(i,1,n)fr(j,1,m)bs[i][j][0]=inf,b[i][j]=0;
    q.push(make_pair(sx,sy));bs[sx][sy][0]=0;bs[sx][sy][1]=0;b[sx][sy]=1;
    while(!q.empty()){
        pr tmp=q.front();q.pop();
        b[tmp.fs][tmp.sc]=0;
        fr(i,0,3){
            int nx=tmp.fs+fx[i],ny=tmp.sc+fy[i];
            if(!valid(nx,ny))continue;
            int ns1=bs[tmp.fs][tmp.sc][0]+1,ns2=bs[tmp.fs][tmp.sc][1]+mx[nx][ny];
            if(bs[nx][ny][0]>ns1){
                bs[nx][ny][0]=ns1;bs[nx][ny][1]=ns2;
                if(!b[nx][ny])q.push(make_pair(nx,ny)),b[nx][ny]=1;
            }else if(bs[nx][ny][0]==ns1&&bs[nx][ny][1]<ns2){
                bs[nx][ny][1]=ns2;
                if(!b[nx][ny])q.push(make_pair(nx,ny)),b[nx][ny]=1;
            }
            
        }
    }
//    puts("");
//    cout<<sx<<" "<<sy<<" "<<ind<<endl;
//    fr(i,1,n){
//        fr(j,1,m)cout<<bs[i][j][1]<<" ";
//        puts("");
//    }
    fr(i,0,p)dis[ind][i][0]=bs[x[i]][y[i]][0],dis[ind][i][1]=bs[x[i]][y[i]][1];
}

const int P=20;
int ff[P][1<<P][2];
void hamilton(){
    ++p;
    fr(i,0,p)fr(j,0,(1<<p)-1)ff[i][j][0]=inf;
    ff[0][1][0]=ff[0][1][1]=0;
    fr(i,0,(1<<p)-1)fr(j,0,p)
     if(i>>j&1)
      fr(k,0,p-1)
       if((i^(1<<j))>>k&1)
        if(ff[j][i][0]>ff[k][i^(1<<j)][0]+dis[j][k][0]){
            ff[j][i][0]=ff[k][i^(1<<j)][0]+dis[j][k][0];
            ff[j][i][1]=ff[k][i^(1<<j)][1]+dis[j][k][1];
        }
        else if(ff[j][i][0]==ff[k][i^(1<<j)][0]+dis[j][k][0])
             ff[j][i][1]=max(ff[j][i][1],ff[k][i^(1<<j)][1]+dis[j][k][1]);
    int ans=1e9,ans2=0;
    fr(i,1,p-1){
            if(ans>ff[i][(1<<p)-1][0])ans=ff[i][(1<<p)-1][0],ans2=ff[i][(1<<p)-1][1];
         else if(ans==ff[i][(1<<p)-1][0])ans2=max(ans2,ff[i][(1<<p)-1][1]);
    }
    cout<<ans<<" "<<ans2+mx[x[0]][y[0]]<<endl;
}
bool vis[N];
int ans=1e9,lim;
inline void dfs(int now,int step,int dist,int sum){
    if(dist>ans) return;
    if(step==p){
        if(dist<ans) ans=dist,lim=sum;
        else if(dist==ans) lim=max(lim,sum);
    }
    fr(i,1,p) if(!vis[i]){
        vis[i]=true;
        dfs(i,step+1,dist+dis[now][i][0],sum+dis[now][i][1]);
        vis[i]=false;
    }
}
void solve(){
    fr(i,1,p){
        vis[i]=true;
        dfs(i,1,dis[0][i][0],dis[0][i][1]);
        vis[i]=false;
    }
    printf("%d %d\n",ans,lim+mx[x[0]][y[0]]);
    return;
}
int main(){
    freopen("expand.in","r",stdin);
    freopen("expand.out","w",stdout);
    n=read(),m=read(),s=read();
    fr(i,1,n)fr(j,1,m)mp[i][j]=read();
    pre2();
    x[0]=read()+1,y[0]=read()+1,p=read();
    fr(i,1,p)x[i]=read()+1,y[i]=read()+1;
    fr(i,0,p)bfs(x[i],y[i],i);
    //hamilton();
    solve();
    return 0;
}

太炸心态了

我再也不相信正解了……

标签:tmp   oid   cout   ||   code   val   air   lin   using   

原文地址:https://www.cnblogs.com/coder-cjh/p/11625755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!