HDU - 3605 : acm.hdu.edu.cn/showproblem.php?pid=3605
题目:
有1e5的的人,小于10个的星球,每个星球都有容量,每个人也有适合的星球和不适合的星球。问所有人是否能住到星球上去。
思路:
这道题目如果直接用常用的建图方式是不行的,因为人太多了,相应的边就很多,会TLE。那要怎么做呢,因为只有十个星球,所以有很多人的特征是相同的,所以把这1e5个点可以缩成最多1024个点。这样建图跑dinic就行了。
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3) #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ struct edge{ int u,v,cap,flag; edge(){} edge(int u,int v,int cap,int flag):u(u),v(v),cap(cap),flag(flag){} }es[100009]; int tot,s,t; vector<int>tab[2009]; int dis[2009],cur[2009]; void addedge(int u,int v,int cap){ // debug(u); tab[u].pb(tot); es[tot++] = edge(u,v,cap,1); tab[v].pb(tot); es[tot++] = edge(v,u,0,0); } bool bfs(){ queue<int>q; q.push(s); memset(dis,inf,sizeof(dis)); dis[s] = 0; while(!q.empty()){ int h = q.front(); q.pop(); for(int i=0; i<tab[h].size(); i++){ edge & e = es[tab[h][i]]; if(e.cap > 0 && dis[e.v] >= inf){ dis[e.v] = dis[h] + 1; q.push(e.v); } } } return dis[t] < inf; } int dfs(int x,int maxflow){ if(x == t || maxflow == 0) return maxflow; for(int i=cur[x] ; i<tab[x].size(); i++){ cur[x] = i; edge & e = es[tab[x][i]]; if(dis[e.v] == dis[x] + 1 && e.cap > 0){ int flow = dfs(e.v, min(maxflow, e.cap)); if(flow){ e.cap -= flow; es[tab[x][i] ^ 1].cap += flow; return flow; } } } return 0; } int dinic(){ int ans = 0; while(bfs()){ int flow; memset(cur,0,sizeof(cur)); do{ flow = dfs(s,inf); if(flow) ans += flow; }while(flow); } return ans; } int cnt[2000]; int main(){ int n,m; while(~scanf("%d%d", &n, &m) && n+m){ tot = 0; s = 0, t = 1024+m+m+1; for(int i=s; i<=t; i++)tab[i].clear(); memset(cnt,0,sizeof(cnt)); for(int i=1; i<=n; i++){ int tmp = 0; for(int j=1; j<=m; j++){ int x; scanf("%d", &x); tmp= tmp*2 + x; } cnt[tmp]++; } for(int i=1; i<=1024; i++) { if(cnt[i] == 0)continue; addedge(s,i,cnt[i]); for(int j=0; j<m; j++){ if((i>>j) & 1 == 1) addedge(i,1025+j,cnt[i]); } } for(int i=0; i<m; i++){ int x;scanf("%d", &x); addedge(1025+i,1025+m+i,x); addedge(1025+m+i,t,inf); } if(n == dinic()) puts("YES"); else puts("NO"); } return 0; }