HDU 3065 Escape(简单网络流 二分图多重匹配)
2014-07-11 11:26
302 查看
由于n太大,无法直接建图,所以考虑压缩图,因为m很小,而且起作用的只为1或0,所以可以将一行压缩为一个数,然后统计这个数的出现个数,而这个数最多2047,,大大简化了图的复杂度。
#include <iostream> #include <cstdio> #include <climits> #include <cstring> #include <algorithm> using namespace std; typedef struct {int v,next,val;} edge; const int MAXN=2100; const int MAXM=23000; edge e[MAXM]; int p[MAXN],eid; int map[MAXN]; inline void init(){memset(p,-1,sizeof(p));eid=0;} //有向 inline void insert1(int from,int to,int val) { e[eid].v=to;e[eid].val=val; e[eid].next=p[from]; p[from]=eid++; swap(from,to); e[eid].v=to;e[eid].val=0; e[eid].next=p[from]; p[from]=eid++; } //无向 inline void insert2(int from,int to,int val) { e[eid].v=to;e[eid].val=val; e[eid].next=p[from]; p[from]=eid++; swap(from,to); e[eid].v=to;e[eid].val=val; e[eid].next=p[from]; p[from]=eid++; } int n,m;//n为点数 m为边数 int h[MAXN]; int gap[MAXN]; int source,sink; inline int dfs(int pos,int cost) { if (pos==sink) return cost; int j,minh=n-1,lv=cost,d; for (j=p[pos];j!=-1;j=e[j].next) { int v=e[j].v,val=e[j].val; if(val>0) { if (h[v]+1==h[pos]) { if (lv<e[j].val) d=lv; else d=e[j].val; d=dfs(v,d); e[j].val-=d; e[j^1].val+=d; lv-=d; if (h[source]>=n) return cost-lv; if (lv==0) break; } if (h[v]<minh) minh=h[v]; } } if (lv==cost) { --gap[h[pos]]; if (gap[h[pos]]==0) h[source]=n; h[pos]=minh+1; ++gap[h[pos]]; } return cost-lv; } int isap(int st,int ed) { source=st;sink=ed; int ret=0; memset(gap,0,sizeof(gap)); memset(h,0,sizeof(h)); gap[st]=n; while (h[st]<n) { ret+=dfs(st,INT_MAX); } return ret; } int main() { int p,k,t,i,j,q,s; while(scanf("%d%d",&p,&k)!=EOF) { init(); m=0; memset(map,0,sizeof(map)); for(i=1;i<=p;i++){ t=1; s=0; for(j=0;j<k;j++){ scanf("%d",&q); s+=q*t; t<<=1; } map[s]++; } t=(1<<(k)); for(i=2;i<=t+1;i++){ insert1(1,i,map[i-2]); m++; } n=0; n+=t+1; for(i=0;i<t;i++){ for(j=0;j<k;j++){ insert1(i+2,t+2+j,map[i]*(i&(1<<(j)))); m++; } } for(j=0;j<k;j++){ scanf("%d",&q); insert1(t+2+j,t+k+2,q); m++; } insert1(t+k+2,t+k+3,100000000); m++; n+=k+2; t=isap(1,n); if(t>=p) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- HDU 3605 Escape(网络流 + 状压简化 | 二分图多重匹配)
- hdu_3605 Escape 二分图的多重匹配 匈牙利算法
- HDU 3605 Escape【二分图多重匹配】
- HDU 3605 Escape (二分图多重匹配模板)
- HDU 3605 Escape(多重匹配||状压缩点的网络流)
- HDU 3036 Escape 网格图多人逃生 网络流||二分匹配 建图技巧
- hdu 3605 Escape(二分图多重匹配问题)
- hdu 3605 Escape 二分图的多重匹配(匈牙利算法)
- HDU 2063 过山车(二分图最大匹配:简单题)
- hdu 3605 Escape (二分图的多重匹配)
- hdu 3605 Escape(最大流+状态压缩 or 二分图多重匹配)
- HDU 3605 Escape 最大流or二分图多重匹配 2010 ACM-ICPC Multi-University Training Contest(17)——Host by ZSTU
- HDU 3605 Escape (二分图多重匹配模板)
- hdu 3605(二分图的多重匹配 | 网络流)
- HDU 2063 过山车(二分图最大匹配:简单题)
- HDU_3605_Escape(多重匹配 / 网络流)
- hdu 3605 Escape【二分图多重匹配】
- HDU 3605 Escape【二分图多重匹配】
- hdu 3605 Escape (二分图多重匹配)
- HDU 3605 Escape【二分图多重匹配】