1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <cstdio> #include <cstring> using namespace std; const int maxn=100010,maxm=200010; const int INF=2147483647; struct { int v,c; node *next,*reverse; }pool[maxm],*h[maxn]; int tot=0,level[maxn],q[maxn]; int n,m,src,sink; int front,rear; int people[maxn],capacity[maxn]; void addEdge(int u,int v,int c){ node *p=&pool[++tot],*q=&pool[++tot]; p->v=v;p->c=c;p->next=h[u];h[u]=p;p->reverse=q; q->v=u;q->c=0;q->next=h[v];h[v]=q;q->reverse=p; } inline bool bfs(){ front=0,rear=1; memset(level,-1,sizeof(level)); q[front]=src;level[src]=0; while(front<rear){ int tmp=q[front++]; for(node *p=h[tmp];p;p=p->next){ if(level[p->v]<0 && p->c){ level[p->v]=level[tmp]+1;q[rear++]=p->v; } } if(level[sink]>0) return true; } return false; } inline int find(int u,int key){ if(u==sink) return key; int ret=0; for(node *p=h[u];p;p=p->next){ if(p->c && level[p->v] == level[u]+1){ int tmp=find(p->v,min(key,p->c)); if(tmp){ ret+=tmp; p->c-=tmp; p->reverse->c+=tmp; key-=tmp; if(key<=0) break; } } } if(ret==0) level[u]=-1; return ret; } inline int dinic(){ int totflow=0; while(bfs()) totflow+=find(src,INF); return totflow; } int main(){ int tot=0; scanf("%d%d",&m,&n); src=0; sink=n+m+1; for(int i=1;i<=m;i++) { scanf("%d",&people[i]); tot+=people[i]; addEdge(src,i,people[i]); } for(int i=1;i<=n;i++) { scanf("%d",&capacity[i]); addEdge(i+m,sink,capacity[i]); } for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) addEdge(i,j+m,1); int res=dinic(); if(tot!=res) { printf("0n"); goto end; } else printf("1n"); for(int i=1;i<=m;i++) { for(node *p=h[i];p;p=p->next) { if(p->c==0 && p->v!=sink) { printf("%d ",p->v-m); } } printf("n"); } end: return 0; }
|
近期评论