[HDU 3605] Escape

Source: Internet
Author: User

This question is very intuitive, but not intuitive.

Let me talk about it first ~ Author: Why did you read about Mars?

 

In general, the question is that humans want to migrate, and then there are n people and M planets.

Each person has M 0 and 1 digital, indicating whether he can migrate to the planet. Each planet has a maximum population.

Ask if there is a solution for migrating the N people

 

At first glance, it was very watery, but it was very touching to look at the data range of N. Nima was not a kind of online stream --

However, the m value range is only 10! Intuition tells me what it seems like I can do it?

Pressure! Because no scheme needs to be output, two persons with the same immigration scheme can be regarded as the same person.

Then the point is pressed to less than 1024, And it passes through properly ~

I won't say that I have been connected by mistake for half an hour.

 

  1 #include <cstdio>  2 #include <cstring>  3 #define min(x, y) ((x)<(y) ? (x):(y))  4 const int inf=0x7FFFFFFF;  5 const int sizeOfPoint=10050;  6 const int sizeOfEdge=500050;  7   8 int n, m, t;  9 int S, T; 10 int f[sizeOfPoint], a[sizeOfPoint]; 11  12 struct edge {int point, flow; edge * next, * pair;}; 13 edge memory[sizeOfEdge], * port=memory; 14 edge * e[sizeOfPoint]; 15 inline void clear() {port=memory; memset(e, 0, sizeof e); t=0; memset(f, 0, sizeof f);} 16 inline edge * newedge(int point, int flow, edge * next) {edge * ret=port++; ret->point=point; ret->flow=flow; ret->next=next; ret->pair=NULL; return ret;} 17 inline void build(int u, int v, int f) {e[u]=newedge(v, f, e[u]); e[v]=newedge(u, 0, e[v]); e[u]->pair=e[v]; e[v]->pair=e[u];} 18 int h[sizeOfPoint]; 19 inline bool bfs(); 20 inline int aug(); 21 inline int dinic(); 22  23 int main() 24 { 25     int x, state; 26  27     while (scanf("%d %d", &n, &m)!=EOF) 28     { 29         clear(); 30         for (int i=1;i<=n;i++) 31         { 32             state=0; 33             for (int j=0;j<m;j++) 34             { 35                 scanf("%d", &x); 36                 state|=x<<j; 37             } 38             if (!f[state]++) a[++t]=state; 39         } 40         S=0; T=t+m+1; 41         for (int i=1;i<=t;i++) 42         { 43             build(S, i, f[a[i]]); 44             for (int j=0;j<m;j++) if ((a[i]>>j)&1) 45                 build(i, t+j+1, f[a[i]]); 46         }     47         for (int i=1;i<=m;i++) 48         { 49             scanf("%d", &x); 50             build(t+i, T, x); 51         } 52  53         if (dinic()==n) printf("YES\n"); 54         else printf("NO\n"); 55     } 56  57     return 0; 58 } 59 inline bool bfs() 60 { 61     static int q[sizeOfPoint]; 62     int l=0, r=0; 63     memset(h, 0xFF, sizeof h); h[T]=0; 64     for (q[r++]=T;l<r;l++) 65     { 66         int u=q[l]; 67         for (edge * i=e[u];i;i=i->next) if (i->pair->flow && h[i->point]==-1) 68             h[q[r++]=i->point]=h[u]+1; 69     } 70     return h[S]>=0; 71 } 72 inline int aug() 73 { 74     static edge * t[sizeOfPoint], * path[sizeOfPoint]; 75     static int aug[sizeOfPoint]; 76     int flow=0; 77  78     memcpy(t, e, sizeof e); 79     memset(path, 0, sizeof path); 80     memset(aug, 0, sizeof aug); 81     aug[S]=inf; 82     for (int u=S; ; ) 83     { 84         if (u==T) 85         { 86             flow+=aug[T]; 87             for (edge * i=path[T];i;i=path[i->point]) 88             { 89                 i->pair->flow-=aug[T], i->flow+=aug[T]; 90                 aug[i->point]-=aug[T]; 91                 if (!aug[i->point]) h[i->point]=-1; 92             } 93             u=S; 94         } 95  96         edge *& i=t[u]; 97         for ( ;i && (!i->flow || h[u]!=h[i->point]+1);i=i->next); 98         if (i) 99         {100             path[i->point]=i->pair; aug[i->point]=min(aug[u], i->flow);101             u=i->point; i=i->next;102         }103         else104         {105             if (u==S) break;106             u=path[u]->point;107         }108     }109 110     return flow;111 }112 inline int dinic()113 {114     int ret=0, flow;115     while (bfs())116         while (flow=aug())117             ret+=flow;118     return ret;119 }
This silly call out Xiang Series

 

[HDU 3605] Escape

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.