zoukankan      html  css  js  c++  java
  • POJ 3020 Antenna Placement 匈牙利算法,最大流解法 难度:1

    http://poj.org/problem?id=3020

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <vector>
     4 using namespace std;
     5 char maz[62][62];
     6 int e[500][500];
     7 int d[4][2]={0,1,0,-1,1,0,-1,0};
     8 int n,m;
     9 vector <int > G[500];
    10 bool vis[500];
    11 void addedge(int from,int to){
    12     e[from][to]=1;
    13     G[from].push_back(to);
    14 }
    15 int dfs(int s){
    16     vis[s]=true;
    17     if(s==n*m+1){//printf("dfs %d ok
    ",s);
    18     return 1;}
    19     for(int i=0;i<G[s].size();i++){
    20         if(!vis[G[s][i]]&&e[s][G[s][i]]&&dfs(G[s][i])){
    21             e[s][G[s][i]]=0;
    22             e[G[s][i]][s]=1;
    23           //  printf("dfs %d ok
    ",s);
    24             return 1;
    25         }
    26     }
    27     //printf("dfs %d failed 
    ",s);
    28     return 0;
    29 }
    30 int maxflow(){
    31     int ans=0,f;
    32     while(1){
    33         f=dfs(n*m);
    34         memset(vis,0,sizeof(vis));
    35         if(f==0)break;
    36         ans+=f;
    37     }
    38     return ans;
    39 }
    40 void printe(){
    41     for(int i=0;i<m*n+2;i++){
    42         bool f=false;
    43         for(int j=0;j<m*n+2;j++){
    44             if(e[i][j]){printf("e[%d][%d] ",i,j);f=true;}
    45         }
    46        if(f) printf("
    ");
    47     }
    48 }
    49 int main(){
    50     int t;
    51     scanf("%d",&t);
    52     while((t--)&&scanf("%d%d",&n,&m)==2){
    53         for(int i=0;i<=n*n+1;i++){G[i].clear();}
    54         memset(e,0,sizeof(e));
    55 
    56         for(int i=0;i<n;i++){
    57             scanf("%s",maz[i]);
    58         }
    59         int ans=0;
    60         int star=0;
    61         for(int i=0;i<n;i++){
    62             for(int j=0;j<m;j++){
    63                 if(maz[i][j]=='*'){
    64                     star++;
    65                     if((i+j)&1){
    66                         addedge(n*m,i*m+j);//n*n s n*n+1 t
    67                        // printf("addedge %d %d %d %d:%d to %d
    ",n,0,i,j,n*m,i*m+j);
    68                         for(int k=0;k<4;k++){
    69                             int nx=i+d[k][0];
    70                             int ny=j+d[k][1];
    71                             if(nx>=0&&nx<n&&ny>=0&&ny<m&&maz[nx][ny]=='*'){
    72                                 addedge(i*m+j,nx*m+ny);
    73                          //       printf("addedge %d %d %d %d:%d to %d
    ",i,j,nx,ny,i*m+j,nx*m+ny);
    74                             }
    75                         }
    76                     }
    77                     else {
    78                         addedge(i*m+j,n*m+1);
    79                         //printf("addedge %d %d %d %d:%d to %d
    ",i,j,n,1,i*m+j,n*m+1);
    80                         for(int k=0;k<4;k++){
    81                             int nx=i+d[k][0];
    82                             int ny=j+d[k][1];
    83                             if(nx>=0&&nx<n&&ny>=0&&ny<m&&maz[nx][ny]=='*'){
    84                                 addedge(i*m+j,nx*m+ny);
    85                                 e[i*m+j][nx*m+ny]=0;
    86                           //      printf("addedge %d %d %d %d:%d to %d
    ",i,j,nx,ny,i*m+j,nx*m+ny);
    87                             }
    88                         }
    89                     }
    90                 }
    91             }
    92         }
    93 
    94         ans=maxflow();
    95         printf("%d
    ",star-ans);
    96     }
    97     return 0;
    98 }
    View Code
  • 相关阅读:
    Calling a parent window function from an iframe
    JSON with Java
    Posting array of JSON objects to MVC3 action method via jQuery ajax
    What's the difference between jquery.js and jquery.min.js?
    jquery loop on Json data using $.each
    jquery ui tabs详解(中文)
    DataTables warning requested unknown parameter
    Datatables 1.10.x在命名上与1.9.x
    jQuery 1.x and 2.x , which is better?
    DataTabless Add rows
  • 原文地址:https://www.cnblogs.com/xuesu/p/4755033.html
Copyright © 2011-2022 走看看