Topcoder--srm570div1--1000--curvyonrails

Source: Internet
Author: User

A problem with a big strong

For the determination of the feasible solution, consider for each city there will be two railway breakpoints, and each railway breakpoint must be combined with another railway breakpoint, and the checkerboard chart is a natural dichotomy, so the first phase of the decision to consider the feasibility of the proposed solution is: checkerboard black and white dyeing, the source point to all white dots, all black dots to the meeting point edge The capacity is 2. Then all white points to the adjacent black dot capacity of 1-point edge, if the maximum flow to make the Yuanhui is full flow is the original solution.

It is easy to observe that if feasible, the number of black and white dots must be the same, and the traffic is black number.

Later, in order to solve the second problem in the topic, we considered that it would be very halal if we could turn the problem into a cost stream.

How does it translate into a cost stream?

Because the curve has 4 cases and there are only 2 straight lines, consider calculating the cost of the straight line.

It can be seen that if the line is straight, then for a grid within the railway must be linked horizontally or vertically linked, then we have all the small animal points to split two points, representing the matching horizontal row and matching vertical line, and then for the two matching degrees are matched to the same direction can be easily counted.

See the code for the concrete map.

Code (no cost Flow section):

intp[ -][ -],n,m,fix,fix2,mk[ -][ -],sz; intWT,BL; intGetmin (vector<string>field) {S=maxn-2; t=maxn-3; cnt=1; N=field.size (); m=field[0].size (); Fix=n*m;fix2=fix<<1;  for(intI=0; i<n;i++)                  for(intj=0; j<m;j++) P[i+1][j+1]=field[i][j]=='.'?1:(field[i][j]=='C'?2:0);  for(intT,i=1; i<=n;i++) for(intj=1; j<=m;j++) {Mk[i][j]=++sz; if(!p[i][j])Continue; T= (i+j) &1; if(t) {Insert (S,mk[i][j],2,0,1); wt++; }                Else{Insert (mk[i][j],t,2,0,1); bl++; }                if(p[i][j]==1) {Insert (Mk[i][j],mk[i][j]+fix,2,0, T); Insert (Mk[i][j],mk[i][j]+FIX2,2,0, T); }                Else{Insert (Mk[i][j],mk[i][j]+fix,1,0, T); Insert (Mk[i][j],mk[i][j]+fix,1,1, T); Insert (Mk[i][j],mk[i][j]+FIX2,1,0, T); Insert (Mk[i][j],mk[i][j]+FIX2,1,1, T); }            }             for(intT,i=1; i<=n;i++) for(intj=1; j<=m;j++) {T= (i+j) &1;if(!t)Continue; if(p[i-1][J]) Insert (mk[i][j]+fix,mk[i-1][j]+fix,1,0,1); if(p[i+1][J]) Insert (mk[i][j]+fix,mk[i+1][j]+fix,1,0,1); if(p[i][j-1]) Insert (mk[i][j]+fix2,mk[i][j-1]+FIX2,1,0,1); if(p[i][j+1]) Insert (mk[i][j]+fix2,mk[i][j+1]+FIX2,1,0,1);            } MSMF (); if(bl!=wt| | flow!=bl*2)return-1; returnCost ; }

Topcoder--srm570div1--1000--curvyonrails

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.