HDU 3605 Escape 最大流+状压
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605
Escape
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7145 Accepted Submission(s): 1553
Problem Description
2012 If this is the end of the world how to do? I do not know how. But now scientists have found that some stars, who can live, but some people do not fit to live some of the planet. Now scientists want your help, is to determine what all of people can live in these planets.
Input
More set of test data, the beginning of each data is n (1 <= n <= 100000), m (1 <= m <= 10) n indicate there n people on the earth, m representatives m planet, planet and people labels are from 0. Here are n lines, each line represents a suitable living conditions of people, each row has m digits, the ith digits is 1, said that a person is fit to live in the ith-planet, or is 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
The last line has m digits, the ith digit ai indicates the ith planet can contain ai people most..
0 <= ai <= 100000
Output
Determine whether all people can live up to these stars
If you can output YES, otherwise output NO.
If you can output YES, otherwise output NO.
Sample Input
1 1
1
1
2 2
1 0
1 0
1 1
Sample Output
YES
NO
Source
题意
地球人要移民,告诉你每个人适合居住的星球是哪些,每个星球有个容量,问你是否能安排所有人移民。
题解
这是道非常非常日狗的题。。。。。。。由于星球很少,人很多,所以很容易想到将人的居住情况状压,然后再建图。这样还是要T的,这是为什么呢?因为脸黑。。。在尝试了各种输入挂之后,终于998ms过了,老天有眼。
代码
#include<iostream> #include<stack> #include<vector> #include<cstring> #include<string> #include<algorithm> #include<cstdio> #include<queue> #define MAX_S (1<<10)+10 #define MAX_V 1222 #define MAX_N MAX_V #define INF 2500005 using namespace std; struct edge { int to, cap, rev; bool isRev; edge(int t, int c, int r, bool i) : to(t), cap(c), rev(r), isRev(i) { } edge() { } }; template <class T> inline bool scan_d(T &ret) { char c; int sgn; if(c=getchar(),c==EOF) return 0; //EOF while(c!=' -' &&(c<'0' ||c>'9' )) c=getchar(); sgn=(c==' -' )?-1:1; ret=(c==' -' )?0:(c-'0' ); while(c=getchar(),c>='0' &&c<='9' ) ret=ret*10+(c-'0' ); ret*=sgn; return 1; } vector<edge> G[MAX_N]; int level[MAX_V]; int iter[MAX_V]; void init(int totNode) { for (int i = 0; i <= totNode; i++) G[i].clear(); memset(level, 0, sizeof(level)); memset(iter, 0, sizeof(iter)); } void add_edge(int from,int to,int cap) { G[from].push_back(edge (to, cap, G[to].size(),0)); G[to].push_back(edge (from, 0, G[from].size() - 1,1)); } void bfs(int s) { queue<int> que; memset(level, -1, sizeof(level)); level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v,int t,int f) { if (v == t)return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s,int t) { int flow = 0; for (; ;) { bfs(s); if (level[t] < 0)return flow; memset(iter, 0, sizeof(iter)); int f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } } int n,m; int S=1113; int T=1114; int cnt[MAX_S]; int main() { while (scanf("%d%d", &n, &m)!=EOF) { init(T + 5); memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < n; i++) { int s = 0; for (int j = 0; j < m; j++) { int t; scan_d(t); if (t)s |= (1 << j); } cnt[s]++; } for (int i = 0; i < (1 << m); i++) { if (cnt[i]) { add_edge(S, i, cnt[i]); for (int j = 0; j < m; j++) if ((1 << j) & i) add_edge(i, j + (1 << m), cnt[i]); } } for (int i = 0; i < m; i++) { int t; scan_d(t); add_edge(i + (1 << m), T, t); } int f = max_flow(S, T); if (f == n) printf("YES\n"); else printf("NO\n"); } return 0; }