The King of the Hotel (P1402 Valley)

Source: Internet
Author: User

Title Description DescriptionXX Hotel's boss wants to become the king of the hotel, in this hope, the first step to the hotel become humane. Since a lot of visitors to stay have their favorite room tones, sunshine, etc., but also have their own favorite dishes, but the hotel only p room, a day only fixed Q-channel different dishes.
A day came to n guests, each guest said he likes what room, like which dish. Unfortunately, it may not be possible to make all the customers happy (the condition is to live in a favorite room and eat a favorite dish).
How to allocate here, can make the most customer satisfaction? input/output format input/output Input Format:
The first line gives three positive integers representing n,p,q (<=100).
After the n rows, the number of p in each row contains 0 or 1, the number of I indicates that I do not like the room I (1 likes, 0 means not like).
After n rows, the number of Q per line indicates that I do not like the I-dish.
output Format:
The largest number of customer satisfaction. input and Output sample sample Input/output sample Test point # # Input Sample:

2 2 2
1 0
1 0
1 1
1 1

Sample output:1 Analysis:
Constructs a diagram, simulates the source point S and the meeting point T, splits each person into two parts, according to the s-> room and so on person-to-man and Rice->t's order constructs the diagram, runs once the biggest stream. can be solved perfectly. Dinic:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespacestd; intn,p,q,s,t,m,ans,a[ +][ +],room[201][201],dish[201][201],dis[ +],sq[ +]; BOOLBFS () {memset (DIS,0xFF,sizeof(DIS)); dis[1]=0; intX,head=0, tail=1; sq[1]=1;  while(head<tail) {Head++; X=Sq[head];  for(intI=1; i<=t;i++)                 if(a[x][i]>0&&dis[i]<0) {Dis[i]=dis[x]+1; Tail++; Sq[tail]=i; }       }       if(dis[t]>0)return true;Else return false; }  intFindintXintLow ) {      intDelta=0; if(x==t)returnLow ;  for(intI=1; i<=t;i++)          if(dis[i]==dis[x]+1&&a[x][i]>0&& (delta=Find (I,min (low,a[x][i)))) {A[x][i]-=Delta; A[I][X]+=Delta; returnDelta; }      return 0; }   intMain () {memset (0,sizeof(guest)); Memset (DISH,0,sizeof(dish)); Memset (A,0,sizeof(a)); scanf ("%d%d%d",&n,&p,&q);  for(intI=1; i<=n;i++)           for(intj=1; j<=p;j++) scanf ("%d",&Room[i][j]);  for(intI=1; i<=n;i++)           for(intj=1; j<=q;j++) scanf ("%d",&Dish[i][j]); S=1; T=n+n+p+q+2;  for(intI=1; i<=p;i++) a[1][i+1]=1;  for(intI=1; i<=n;i++) a[p+1+i][p+n+1+i]=1;  for(intI=1; i<=n;i++)           for(intj=1; j<=p;j++)              if(room[i][j]==1) a[j+1][p+1+i]=1;  for(intI=1; i<=n;i++)           for(intj=1; j<=q;j++)              if(dish[i][j]==1) a[p+n+1+i][p+n+n+1+j]=1;  for(inti=n+n+p+2; i<t;i++) a[i][t]=1; Ans=0;  while(BFS ()) while(M=find (1,0x7fffffff)) ans+=m; printf ("%d", ans); return 0; }  

Time:91ms
Memory:6381kb

SAP from the same school's great God:

#include <cstdio>#include<cstdlib>#include<iostream>#include<cstring>using namespacestd; intedge[420][420],dis[420],start[420],gap[420],pre[420]; intMain () {intA,b,p,q,c,i,j,k,n,m,h,ll,flow; Memset (Edge,0,sizeof(Edge)); Memset (Gap,0,sizeof(GAP)); memset (Start,0,sizeof(start)); CIN>> n >> p>> q; C=p+n; b=c+N;  for(i=1; i<=n; i++) for(j=1; j<=p; J + +) {cin>>A; if(a) edge[j][i+p]++; }       for(i=1; i<=n; i++) edge[i+p][i+c]++;  for(i=1; i<=n; i++) for(j=1; j<=q; J + +) {cin>>A; if(a) edge[i+c][b+j]++; } N=b+q+1; gap[0]=n+1;  for(i=1; i<=p; i++) edge[0][i]++;  for(i=b+1; i<n; i++) edge[i][n]++;  for(i=0; i<=n; i++) dis[i]=0; I=0; flow=0;  while(dis[0]<=N) {ll=0;  for(J=start[i]; j<=n; J + +)           if(Edge[i][j] && dis[j]+1==Dis[i]) {ll=1; PRE[J]=i; I=J; if(i==N) {flow++;  while(i!=0) {h=i; I=Pre[i]; EDGE[I][H]--; Edge[h][i]++; }               }                Break; }           if(LL)Continue; H=n+1;  for(j=1; j<=n; J + +)           if(Edge[i][j] && dis[j]<h) {h=Dis[j]; A=J; } Start[i]=A; Gap[dis[i]]--; if(!gap[dis[i]]) Break; Dis[i]=h+1; Gap[dis[i]]++; if(i!=0) i=Pre[i]; } cout<<flow; return 0; }  

Time:137ms
Memory:2805kb

On the point of view, the composition is similar to the case, Dinic, but more memory, SPFA aging slightly inferior, but less memory.

Reasonable composition is the key!

The King of the Hotel (P1402 Valley)

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.