UVA, 11380Down Went the Titanic (max. Flow, split)

Source: Internet
Author: User

Maximum Flow, split

Test instructions: @ can walk infinitely, * person's initial position, others cannot walk. ~ Water, can't go, ... Can only walk once, #终点, can accommodate the number of p; Ask at most how many people were rescued (arrived #)

This question is the biggest flow problem, mainly to build the diagram. How to build a diagram? Mainly with a split point, a point is split into two points, point (I,J) can be expressed as: Front point (i-1) *a+j, after the point (i-1) *a+j+m (m for a larger number, to ensure that M is greater than or equal to s*a on the line), and then connect the front and back points, the direction is from point to point, @ and Their front and back-point weights are set infinitely large.

Adjacent points, the graph is non-direction, with the point of the back point of the adjacent points to connect the front point, if the adjacent point is @ or #, set the weight of the edge infinity.

Connect all the front points with a super source point, connect all the # points with a super endpoint, the weights for all sides are p, the other didn't see? edge weights Default to 1, and the graph is built. Then use the maximum flow algorithm that you are familiar with.

Ford-fulkerson Maximum Flow Algorithm code reference: http://blog.csdn.net/itaskyou/article/details/51331344




#include <iostream> #include <vector> #include <cstring> #include <map> #include <string>    using namespace std; #define N 2005#define INF 1000000struct edge{int to,cap,rev;        Edge (int a,int b,int c) {to=a;        Cap=b;    Rev=c;  }};vector<edge>v[n];void add_edge (int from,int to,int cap); int dfs (int a,int t,int f); int max_flow (int s,int t); int    Used[n];int q[4][2]= {1,0,0,1,-1,0,0,-1};string s[n];int main () {int x,y,p;         while (cin>>x>>y>>p) {for (int i=0;i<n;i++) {v[i].clear ();//Initialize;}        for (int i=0;i<x;i++) {cin>>s[i]; } for (int i=0;i<x;i++)//split {for (int j=0;j<y;j++) {if (s[i][j]== '                    # ') {Add_edge (i*y+j,i*y+j+x*y,inf);                Add_edge (I*Y+J+X*Y,X*Y*2+1,P);             } else if (s[i][j]== ' @ ') {       Add_edge (I*y+j,i*y+j+x*y,inf);                    } else if (s[i][j]== ' * ') {Add_edge (i*y+j,i*y+j+x*y,1);                Add_edge (x*y*2,i*y+j,1);                 } else if (s[i][j]== '. ')                 {Add_edge (i*y+j,i*y+j+x*y,1);                     } for (int k=0;k<4;k++) {int k1=i+q[k][0];                     int k2=j+q[k][1]; if (k1>=0&&k1<x&&k2>=0&&k2<y) {if (s[k1][k2 ]=='@'||                         s[k1][k2]== ' # ') Add_edge (I*y+j+x*y,k1*y+k2,inf);                     else Add_edge (i*y+j+x*y,k1*y+k2,1);        }}}} int Ans=max_flow (x*y*2,x*y*2+1);    cout<<ans<<endl; } return 0;} void Add_edge (int from,int to,int cap) {V[from].push_back (Edge (To,cap,v[to].size ())); V[to].push_back (Edge (From,0,v[from].size ()-1));}    int dfs (int a,int t,int f) {if (a==t) return F;    Used[a]=1;        for (int i=0;i<v[a].size (); i++) {Edge &e=v[a][i];            if (!used[e.to]&&e.cap>0) {int D=dfs (e.to,t,min (F,e.cap));                if (d>0) {e.cap-=d;                V[e.to][e.rev].cap+=d;            return D; }}} return 0;}    int max_flow (int s,int t) {int flow=0;        while (1) {memset (used,0,sizeof (used));        int F=dfs (s,t,inf);        if (f==0) return flow;    Flow+=f; }}



UVA, 11380Down Went the Titanic (max. Flow, split)

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.