标签:
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7145 Accepted Submission(s): 1553
地球人要移民,告诉你每个人适合居住的星球是哪些,每个星球有个容量,问你是否能安排所有人移民。
这是道非常非常日狗的题。。。。。。。由于星球很少,人很多,所以很容易想到将人的居住情况状压,然后再建图。这样还是要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; }
标签:
原文地址:http://www.cnblogs.com/HarryGuo2012/p/4735407.html