SRM 558 div1 1000 surroundinggame (minimum cut composition)

Source: Internet
Author: User
Problem Statement
  Surrounding game is a single-player game played on a rectangular grid of cells. cells are considered adjacent if they share a common side. (hence, each cell has at most four adjacent cells. the cells on the sides and in the corners of the grid have fewer
Adjacent cells than the ones inside the grid .)

The game is played by placing stones into some of the cells. Each cell may only contain at most one stone. A cell is called dominated if at least one of the following two conditions holds:

  • The cell contains a stone.
  • All Cells adjacent to the cell contain stones.

Each cell of the grid contains two numbers, each from 0 to 61, random sive: The cost of placing a stone into the cell, and the benefit from dominating the cell. at the end of the game, the overall score of the player is the sum of all benefits minus the sum
All costs.

You are given the vector <string> SCostAndBenefit. The charactersCost[I] [J] andBenefit[I] [J] represent the two numbers written in the cell (I, j), using the following encoding:

  • Characters '0'-'9' represent numbers 0-9.
  • Characters 'a'-'Z' represent numbers 10-35.
  • Characters 'a'-'Z' represent numbers 36-61.

For example, if character 7 of element 4CostIs 'B', the cost of placing a stone into the cell (4, 7) is 37.

Calculate and return the maximal possible score for the given grid.

Definition
 
Class: Surroundinggame
Method: Maxscore
Parameters: Vector <string>, vector <string>
Returns: Int
Method signature: Int maxscore (vector <string> Cost, vector <string> benefit)
(Be sure your method is public)
 
 
Constraints
- CostWill contain between 2 and 20 elements, inclusive.
- CostAndBenefitWill contain the same number of elements.
- Each elementCostWill contain between 2 and 20 characters, inclusive.
- Each elementCostWill contain the same number of characters.
- Each elementBenefitWill contain the same number of characters as element 0Cost.
- Each character inCostAndBenefitWill be a character from '0'-'9', 'a'-'Z', or 'A'-'Z '.
Examples
0)  
 
{"21","12"}
{"21","12"}
Returns: 4
The optimal solution is to put stones into (0, 1) and (1, 0). All the cells will be dominated and the overall score will be 6-2 = 4.
1)  
 
{"ZZ","ZZ"}
{"11","11"}
Returns: 0
The optimal solution is to put no stones. It is impossible to get a positive score.
2)  
 
{"XXX","XXX","XXX"}
{"aaa","aZa","aaa"}
Returns: 2
The optimal solution is to put a stone into (1, 1 ).
3)  
 
{"asam","atik"}
{"123A","45BC"}
Returns: 71
 
4)  
 
{"IIIIIIII", "IIWWWWII", "IIWIIIII", "IIWIIIII", "IIWWWWII", "IIIIIWII", "IIIIIWII", "IIWWWWII", "IIIIIIII"}
{"IIIIIIII", "II0000II", "II0II0II", "II0II0II", "II0000II", "II0II0II", "II0II0II", "II0000II", "IIIIIIII"}
Returns: 606

Question: give you a square matrix of N * M. Each square matrix can be placed with stones, and each grid needs to be put with stones. If a grid is placed with stones, or, if the grids around it are all placed with stones, it will bring benefits... Q: How to put stones to maximize the cost of income?


Analysis: I have done a lot of things like this. Soon after reading this, I thought it could be a minimal cut application. Then I am struggling for a long time. Let's talk about other people's compositions, I haven't fully understood it yet = I have read the code of many people. The following figure is simple and I understand it a little bit.

First, color all the grids in black and white, divide each grid into two nodes, and then virtual Source Vertex S and sink vertex T. For the Black Grid I, create two sides, s to I, the capacity is spent, I to I 'capacity is the benefit. For the white lattice J, it is also two sides, J to T, the capacity is the cost, and the capacity from J to J is the benefit. For the adjacent I to J, we also set up two sides, I to J', and I to J. The capacity is infinite. To do this, the answer is all benefits-the value of the maximum stream.

After careful consideration, this model assumes that the benefits of all grids are obtained, and then how to place stones to minimize the cost, that is, this model is used to minimize the cost, the two sides of the middle capacity are just to limit the cost to no more than the profit. On this premise, the total cost is minimized, which is my rough understanding.

I hope someone will give me the guidance on this question.

PS: As there are 20 minutes left to finish a question during the competition, the weak dishes are powerless... After reading this question, there will be 6 minutes left. I immediately posted a template and made a diagram. The final examples won't be called. I regret it and should not relax in the middle, after the competition, I found that I could not do it even if I did it. This is not a simple question t_t.

After two days of hard thinking, I still didn't figure out how to make a chart. I had no choice but to read other people's code. I am still ignorant. But this time the ID turned yellow. I will post it as a souvenir.


Code:

// BEGIN CUT HERE// END CUT HERE#line 5 "SurroundingGame.cpp"#include<cstdlib>#include<cctype>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<sstream>#include<map>#include<set>#include<queue>#include<stack>#include<fstream>#include<numeric>#include<iomanip>#include<bitset>#include<list>#include<stdexcept>#include<functional>#include<utility>#include<ctime>using namespace std;#define PB push_back#define MP make_pair#define REP(i,n) for(i=0;i<(n);++i)#define FOR(i,l,h) for(i=(l);i<=(h);++i)#define FORD(i,h,l) for(i=(h);i>=(l);--i)typedef vector<int> VI;typedef vector<string> VS;typedef vector<double> VD;typedef long long LL;typedef pair<int,int> PII;const int mm=1000000;const int mn=22222;const int oo=1000000000;int node,src,dest,edge;int reach[mm],flow[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){    return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1;    edge=0;}inline void addedge(int u,int v,int c){    reach[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    reach[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=next[i])            if(flow[i]&&dis[v=reach[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=next[i])        if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}int Dinic_flow(){    int i,ret=0,delta;    while(Dinic_bfs())    {        for(i=0;i<node;++i)work[i]=head[i];        while(delta=Dinic_dfs(src,oo))ret+=delta;    }    return ret;}int get(char a){    if(a>='0'&&a<='9')return a-'0';    if(a>='a'&&a<='z')return a-'a'+10;    if(a>='A'&&a<='Z')return a-'A'+36;    return 0;}int dx[]={0,0,-1,1};int dy[]={-1,1,0,0};class SurroundingGame{public:    int maxScore(vector <string> cost, vector <string> benefit)    {        int i,j,k,x,y,n=cost.size(),m=cost[0].size(),sum=0;        prepare(n*m*2+2,0,n*m*2+1);        for(i=0;i<n;++i)            for(j=0;j<m;++j)            {                sum+=get(benefit[i][j]);                if((i+j)%2)                {                    addedge(src,i*m+j+1,get(cost[i][j]));                    addedge(i*m+j+1,n*m+i*m+j+1,get(benefit[i][j]));                }                else                {                    addedge(n*m+i*m+j+1,i*m+j+1,get(benefit[i][j]));                    addedge(i*m+j+1,dest,get(cost[i][j]));                }                for(k=0;k<4;++k)                {                    x=i+dx[k];                    y=j+dy[k];                    if(x<0||x>=n||y<0||y>=m)continue;                    if((i+j)%2)addedge(n*m+i*m+j+1,x*m+y+1,oo);                    else addedge(x*m+y+1,n*m+i*m+j+1,oo);                }            }        sum-=Dinic_flow();        return sum;    }// BEGIN CUT HEREpublic:void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }private:template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }void test_case_0() { string Arr0[] = {"21","12"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"21","12"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 4; verify_case(0, Arg2, maxScore(Arg0, Arg1)); }void test_case_1() { string Arr0[] = {"ZZ","ZZ"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"11","11"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 0; verify_case(1, Arg2, maxScore(Arg0, Arg1)); }void test_case_2() { string Arr0[] = {"XXX","XXX","XXX"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"aaa","aZa","aaa"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 2; verify_case(2, Arg2, maxScore(Arg0, Arg1)); }void test_case_3() { string Arr0[] = {"asam","atik"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"123A","45BC"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 71; verify_case(3, Arg2, maxScore(Arg0, Arg1)); }void test_case_4() { string Arr0[] = {"IIIIIIII", "IIWWWWII", "IIWIIIII", "IIWIIIII", "IIWWWWII", "IIIIIWII", "IIIIIWII", "IIWWWWII", "IIIIIIII"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"IIIIIIII", "II0000II", "II0II0II", "II0II0II", "II0000II", "II0II0II", "II0II0II", "II0000II", "IIIIIIII"}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 606; verify_case(4, Arg2, maxScore(Arg0, Arg1)); }// END CUT HERE};// BEGIN CUT HEREint main(){    SurroundingGame ___test;    ___test.run_test(-1);    return 0;}// END CUT HERE

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.