EscapeTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 6239 Accepted Submission (s): 1474
Problem Description2012 If This is the end of the world? I don't know how. But now scientists has found that some stars, who can live, but some people does not fit to live some of the planet. Now scientists want your help, was to determine what's all of the people can live in these planets.
Inputmore set of test data, the beginning of each data is n (1 <= n <= 100000), M (1 <= m <=) n indicate th ere n people on the Earth, M representatives M planet, planet and people labels is from 0. Here's n lines, each line represents a suitable living conditions of people with each row have m digits, the ith digits is 1, said that's a person was fit to live in the Ith-planet, or was 0 for this person is not suitable for living in the ith planet .
The last line has m. digits, the ith digit AI indicates the ith planet can contain AI people most.
0 <= ai <= 100000
Outputdetermine whether all people can live up to these stars
If You can output YES, otherwise output NO.
Sample Input
1 1112 21 01 01 1
Sample Output
YESNO
Source2010 acm-icpc multi-university Training Contest (+)--host by Zstu
Test instructions: Now the earth has n (<=100w) Number of people to go to M (<=10) Other planets to survive, next n rows per row m column, if I is 1 corresponding to this person can go to planet I, the first I planet can accommodate AI individuals (the last line gives the number of M). Now ask if all people can survive on other planets.
Problem solving: Max stream SAP. Map: Because of the number of people, and the state of the planet to a minimum of 1024, so each state can be regarded as a point, the source point and each state to connect an edge, the capacity of the current state of the number of people, and then each state with the corresponding planets, contents the current state of the number of people. Each planet is connected to a meeting point, and contents is the number of people the planet can hold.
Another solution: Multi-match, the X-part and the planet as the Y part, forming a two-part diagram.
Here is the maximum flow SAP solution: with g++.
#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std ; #define Captype intconst int maxn = 1030; Total number of points const int MAXM = 14000; Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;} Edg[maxm];int Eid,head[maxn];int GAP[MAXN]; The number of points for each distance (or can be considered a height) int DIS[MAXN]; The shortest distance from each point to the end Enode int CUR[MAXN]; Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];inline void init () {eid=0; memset (head,-1,sizeof (Head));} There are three parameters to the edge, 4 parameters inline void addedg (int u,int v,captype c,captype rc=0), non-edge, {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++; Edg[eid].to=u; EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;} Inline Captype maxflow_sap (int& snode,int& enode, int n) {//n is the total number of points including source and sink points, this must be noted memset (Gap,0,sizeof (GAP)); memset (dis,0,sizeof (dis)); memcpy (cur,head,sizeof (head)); Pre[snode] =-1; Gap[0]=n; Captype ans=0; Max Flow INT U=snode, I; while (dis[snode]<n) {//determines from SNode point there is no flow to the next adjacent point if (u==enode) {//Find a way to add flow captype min=inf; int inser; I=pre[u]; while (i!=-1) {//The maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {Min=edg[i] is found from the path of this stream. Cap-edg[i].flow; Inser=i; } I=pre[edg[i^1].to]; } I=pre[u]; while (i!=-1) {edg[i].flow+=min; Edg[i^1].flow-=min; Flow i=pre[edg[i^1].to of the side that can be returned); } ans+=min; u=edg[inser^1].to; Continue } bool flag = FALSE; It is possible to determine whether an int v can flow toward the neighboring point from the U point; I=cur[u]; while (i!=-1) {v=edg[i].to; if (edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1) {flag=true; Cur[u]=pre[v]=i; Break } I=edg[i].next; } if (flag) {u=v; Continue }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point; I=head[u]; while (I!=-1) {if (edg[i].cap-edg[i].flow>0 && mind>dis[edg[i].to]) {mind=dis[e DG[I].TO]; Cur[u]=i; } I=edg[i].next; } gap[dis[u]]--; if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++; if (U!=snode) u=edg[pre[u]^1].to; Return an Edge} return ans; inline void scanf (int& valu) {char ch; while (Ch=getchar ()) {if (ch>= ' 0 ' &&ch<= ' 9 ') break; } valu=ch-' 0 '; while (Ch=getchar ()) {if (ch< ' 0 ' | | \ ch> ' 9 ') break; valu=valu*10+ch-' 0 '; }}int Main () {int N,m,a, S, t, ans, k[1030]; while (scanf ("%d%d", &n,&m) >0) {memset (k,0,sizeof (k)); int Id=0, i=0, J; while (i<n) {int b=0; j=0; while (j<m) {scanf (a); if (a) b|=1<<j; j + +; } if (k[b]==0&&b)//appearance of different number of States E id++; k[b]++; i++; } ans=n; Init (); N=id; s=0; T= n+m+1; I= (1<<m)-1; while (i>0) {if (k[i]>0) {//appears state ADDEDG (s, ID, k[i]); j=0; while ((1<<J) <=i) {if (i& (1<<j)) Addedg (ID, j+n+1, k[i]); j + +; } id--; } i--; } j=0; while (j<m) {scanf (a); if (a) addedg (j+n+1, T, a); j + +; } ANS-=MAXFLOW_SAP (S, T, t+1); if (ans) puts ("NO"); else putS ("YES"); printf ("%s\n", ans>0:); }}
Hdu3605escape (number of optimized points for maximum flow sap+ state compression)