Los P1402 Valley Hotel King

 Topic Portal: https://www.luogu.org/problem/P1402

 

Title Description

XX hotel owner wants to be Hotel King, in the hope that this first step in the hotel you want to become human. As many visitors to stay in the room tone has its own preferences, such as the sun, but also their love of food, but the hotel is only room p room one day different dishes only fixed q Road.

One day came n guests, each guest room to say what they like, like what dish. But unfortunately, it may not do so that all customer satisfaction (satisfaction of the conditions is like to live in a room, eat like the food).

Here to how allocation, customer satisfaction make the most of it?

 

Input Format

The first three rows are given a positive integer n, p, q (<= 100).

After n rows, each row comprising a number p of 0 or 1, the number i represents the i-th liked room (1 likes, 0 dislike).

After n lines, each line number q, represents the i liked dish.

 

Output Format

The maximum number of customer satisfaction.

 

Sample input and output
Input # 1
2 2 2
1 0
1 0
1 1
1 1
Output # 1
 1
 

Solution:

  ~~ The title is a look at the board network flow problem Well ~ ~

This problem is beginning to see, apart from anything else, the first composition, super source connected to the room, the room is connected to the person, who then connect to the meal, the last meal before connecting to Royal Meeting Point. The results of a pay up, Mom yeah! WA seven. Read the code over and over again, I could not find BUG. Could it be, this question pit?

Then point to open the solution to a problem, I know that is true food, network flow simply did not learn. Reader you use network streams, please note that this question has a huge pit, look at this picture the reader will understand

                                                

A person can only eat a dish, live in a house thing! ! ! (Nest dishes, such a simple question did not take into account the QwQ)

Then, how to deal with it?

In fact, a person eating a dish, live in a house, in essence is a person through only once, we can think of, can a person assigned on a per flow, split in two points, so nobody will only flow once it!

Figure:

                                              

 

This, the reader should also be very clear ideas, and did not talk much to say, the Code:

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<queue>
  7 #define R register
  8 #define next kkksc03
  9 using namespace std;
 10 typedef long long ll;
 11 typedef long double ld;
 12 typedef unsigned long long ull;
 13 inline ll read();
 14 ll n,p,q;
 15 ll to[1000000],next[1000000],c[1000000],tot,head[1000000];
 16 inline void add(ll x,ll y,ll z){
 17     to[++tot]=y;c[tot]=z;next[tot]=head[x];head[x]=tot;
 18 }
 19 ll S,T;
 20 ll d[1000000];
 21 queue<ll>Q;
 22 bool bfs(){
 23     memset(d,0,sizeof d);
 24     while(!Q.empty()) Q.pop();
 25     Q.push(S);
 26     d[S]=1;
 27     while(!Q.empty()){
 28         ll x=Q.front();
 29         Q.pop();
 30         for(R ll i=head[x];i;i=next[i]){
 31             ll ver=to[i];
 32             if(!d[ver]&&c[i]){
 33                 d[ver]=d[x]+1;
 34                 Q.push(ver);
 35                 if(ver==T) return true;
 36             }
 37         }
 38     }
 39     return false;
 40 }
 41 ll dinic(ll x,ll flow){
 42     if(x==T||!flow) return flow;
 43     ll k,rest=flow;
 44     for(R ll i=head[x];i&&rest;i=next[i]){
 45         ll ver=to[i];
 46         if(d[ver]==d[x]+1&&c[i]){
 47             k=dinic(ver,min(rest,c[i]));
 48             if(!k) d[ver]=0;
 49             rest-=k;
 50             c[i]-=k;
 51             c[i^1]+=k;
 52         }
 53     }
 54     return flow-rest;
 55 }
 56 int main(){
 57     n=read();p=read();q=read();
 58     S=0;T=n+n+p+q+1;tot=1;
 59     for(R ll i=1;i<=n;i=-~i){
 60         for(R ll j=1,x;j<=p;j=-~j){
 61             x=read();
 62             if(!x) continue;
 63             add(n+j,i,1);
 64             add(i,n+j,0);
 65         }
 66     }
 67     for(R ll i=1;i<=n;i=-~i){
 68         for(R ll j=1,x;j<=q;j=-~j){
 69             x=read();
 70             if(!x) continue;
 71             add(i+p+q+n,n+p+j,1);
 72             add(n+p+j,i+p+q+n,0);
 73         }
 74     }
 75     for(R ll i=1;i<=p;i=-~i){
 76         add(S,n+i,1);
 77         add(n+i,S,0);
 78     }
 79     for(R ll i=1;i<=n;i=-~i){
 80         add(i,n+p+q+i,1);
 81         add(n+p+q+i,i,0);
 82     }
 83     for(R ll i=1;i<=q;i=-~i){
 84         add(n+p+i,T,1);
 85         add(T,n+p+i,0);
 86     }
 87     ll _flow,maxflow=0;
 88     while(bfs()){
 89         while(_flow=dinic(S,0x7fffffff)) maxflow+=_flow;
 90     }
 91     printf("%lld\n",maxflow);
 92 }
 93 inline ll read(){
 94     ll x=0,t=1;char ch=getchar();
 95     while(ch<'0'||ch>'9'){
 96         if(ch=='-') t=-1;
 97         ch=getchar();
 98     }
 99     while(ch>='0'&&ch<='9'){
100         x=(x<<1)+(x<<3)+(ch^48);
101         ch=getchar();
102     }
103     return x*t;
104 }

 

 

Guess you like

Origin www.cnblogs.com/ylwtsq/p/11745558.html