Bzoj1976: [beijing2010 team up] energy cube

Source: Internet
Author: User
1976: [beijing2010 team up] cubetime limit: 10 sec memory limit: 64 MB
Submit: 832 solved: 281
[Submit] [Status] Description little C has an energy cube, which is amazing. As long as you put different energy crystals in a specific way, you can generate a huge amount of energy. The cube of energy is a cube of N * n. A total of N3 spaces can be used to fill the energy crystal. There are two kinds of Energy crystals: Positive and Negative. When this cube is filled up, it will generate a huge amount of energy based on the relationship between filled Energy crystals. For two adjacent grids (adjacent grids have the same surface), if the two grids are filled with one positive and one negative crystals, one unit of energy is generated. The total energy of the cube is the sum of the energy produced. Now, little C has filled some crystals in the cube, and there are still some places left empty. He wants to know how much energy this cube can generate if the remaining space can be filled at will. The first line of input contains N, indicating the cube size. Next line N2, each line contains n characters, and each character has three possibilities: P: indicates that the square is filled with positive energy crystals; N: indicates that the square is filled with negative energy crystals ;? : Indicates the square to be filled. N * n rows above, the (I-1) * n + 1 ~ Line I * n describes the status of the layer I of the cube from top to bottom, from left to right. Each n rows are separated by null rows. The output contains only one row of data, indicating the maximum amount of energy that a cube can produce. Sample input2
P?
??

??
N? Sample output9hint

The following States produce the most energy.
Pn
NP

NP
Nn

[Data scale]
10% of Data n ≤ 3;
30% of Data n ≤ 4;
80% of Data n ≤ 10;
100% of the data is n ≤ 40.

Source

Question:

I am also drunk with this question...

Similar to the above labeling plan, we can dye the two points, then the Black Point S is positive T negative, the white point S is negative T positive, and the black and white points have been determined to connect the INF edge to the corresponding point, indicates that it must be included in the cut.

Then, an edge with a capacity of 1 is connected to other adjacent points, which is an undirected edge. (For convenience, you can add a directed edge to each of the black and white dots .)

Then OK. You still need to go into the undirected problem. (ANS> 1)

Code:

  1 #include<cstdio>  2 #include<cstdlib>  3 #include<cmath>  4 #include<cstring>  5 #include<algorithm>  6 #include<iostream>  7 #include<vector>  8 #include<map>  9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 100000 14 #define maxm 3000000 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define FOR for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)for(int k=1;k<=n;k++) 19 #define mod 1000000007 20 using namespace std; 21 inline int read() 22 { 23     int x=0,f=1;char ch=getchar(); 24     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 25     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();} 26     return x*f; 27 } 28 int  n,m,s,t,maxflow,tot=1,ans,mark[50][50][50],head[maxn],cur[maxn],h[maxn],q[maxn]; 29 struct edge{int go,next,v;}e[maxm]; 30 void ins(int x,int y,int z){e[++tot].go=y;e[tot].v=z;e[tot].next=head[x];head[x]=tot;} 31 void insert(int x,int y,int z){ins(x,y,z);ins(y,x,0);} 32 void ins2(int x,int y,int z){insert(x,y,z);insert(y,x,z);} 33 bool bfs() 34 { 35     for(int i=s;i<=t;i++)h[i]=-1; 36     int l=0,r=1;q[1]=s;h[s]=0; 37     while(l<r) 38     { 39         int x=q[++l]; 40         for(int i=head[x];i;i=e[i].next) 41          if(e[i].v&&h[e[i].go]==-1) 42          { 43             h[e[i].go]=h[x]+1;q[++r]=e[i].go; 44          } 45     } 46     return h[t]!=-1; 47 } 48 int dfs(int x,int f) 49 { 50     if(x==t) return f; 51     int tmp,used=0; 52     for(int i=head[x];i;i=e[i].next) 53      if(e[i].v&&h[e[i].go]==h[x]+1) 54     { 55         tmp=dfs(e[i].go,min(e[i].v,f-used)); 56         e[i].v-=tmp;if(e[i].v)cur[x]=i; 57         e[i^1].v+=tmp;used+=tmp; 58         if(used==f)return f;        59     } 60     if(!used) h[x]=-1; 61     return used; 62 } 63 void dinic() 64 { 65     maxflow=0; 66     while(bfs()) 67     { 68         for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf); 69     } 70 } 71 int get() 72 { 73     char ch=‘ ‘; 74     while(ch!=‘?‘&&ch!=‘P‘&&ch!=‘N‘)ch=getchar(); 75     if(ch==‘?‘)return 0;else if(ch==‘P‘)return 1;else return 2; 76 }     77 int main() 78 { 79     freopen("input.txt","r",stdin); 80     freopen("output.txt","w",stdout); 81     n=read(); 82     FOR mark[i][j][k]=(i-1)*n*n+(j-1)*n+k; 83     s=0;t=mark[n][n][n]+1; 84     FOR 85     { 86         int x=get(); 87         if((i+j+k)&1) 88         { 89         if(x==1)insert(s,mark[i][j][k],inf); 90         else if(x==2)insert(mark[i][j][k],t,inf); 91         } 92         else 93         { 94         if(x==1)insert(mark[i][j][k],t,inf); 95         else if(x==2)insert(s,mark[i][j][k],inf); 96         } 97         int res=0; 98         if(i<n)insert(mark[i][j][k],mark[i+1][j][k],1),ans++; 99         if(i>1)insert(mark[i][j][k],mark[i-1][j][k],1),ans++;100         if(j<n)insert(mark[i][j][k],mark[i][j+1][k],1),ans++;101         if(j>1)insert(mark[i][j][k],mark[i][j-1][k],1),ans++;102         if(k<n)insert(mark[i][j][k],mark[i][j][k+1],1),ans++;103         if(k>1)insert(mark[i][j][k],mark[i][j][k-1],1),ans++;104         //if(!x)insert(s,mark[i][j][k],res),insert(mark[i][j][k],t,res);105     }106     dinic();107     printf("%d\n",(ans>>1)-maxflow);108     return 0;109 }
View code

 

Bzoj1976: [beijing2010 team up] energy cube

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.