标签:
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
Source
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<queue> 7 #define inf 0x3f3f3f3f 8 using namespace std; 9 const int maxn=100+10,maxm=1000+10,maxe=200000+10; 10 int n,m,S,T; 11 int cus[maxm][maxn],pig[maxm],buy[maxn],num[maxm]; 12 struct Tedge{int x,y,flow,cap,next;}; 13 struct Dinic{ 14 Tedge adj[maxe];int fch[maxn],ms; 15 int n,m,S,T,d[maxn],cur[maxn];bool vis[maxn]; 16 void init(int n){ 17 this->n=n; 18 ms=0; 19 memset(fch,-1,sizeof(fch)); 20 return; 21 } 22 void AddEdge(int a,int b,int c){ 23 adj[ms]=(Tedge){a,b,0,c,fch[a]};fch[a]=ms++; 24 adj[ms]=(Tedge){b,a,0,0,fch[b]};fch[b]=ms++; 25 return; 26 } 27 bool BFS(){ 28 memset(vis,false,sizeof(vis)); 29 queue<int>Q;Q.push(S);vis[S]=true;d[S]=0; 30 while(!Q.empty()){ 31 int u=Q.front();Q.pop(); 32 for(int i=fch[u];i!=-1;i=adj[i].next){ 33 int v=adj[i].y; 34 if(!vis[v]&&adj[i].cap>adj[i].flow){ 35 vis[v]=true; 36 d[v]=1+d[u]; 37 Q.push(v); 38 } 39 } 40 } return vis[T]; 41 } 42 int DFS(int u,int a){ 43 if(u==T||!a) return a; 44 int flow=0,f; 45 for(int& i=cur[u];i!=-1;i=adj[i].next){ 46 int v=adj[i].y; 47 if(d[v]==d[u]+1&&(f=DFS(v,min(a,adj[i].cap-adj[i].flow)))>0){ 48 flow+=f; 49 a-=f; 50 adj[i].flow+=f; 51 adj[i^1].flow-=f; 52 if(!a) break; 53 } 54 } return flow; 55 } 56 int MaxFlow(int S,int T){ 57 this->T=T;this->S=S; 58 int flow=0; 59 while(BFS()){ 60 for(int i=0;i<n;i++) cur[i]=fch[i]; 61 flow+=DFS(S,inf); 62 } return flow; 63 } 64 }sol; 65 inline int read(){ 66 int x=0,sig=1;char ch=getchar(); 67 while(!isdigit(ch)){if(ch==‘-‘) sig=-1;ch=getchar();} 68 while(isdigit(ch)) x=10*x+ch-‘0‘,ch=getchar(); 69 return x*=sig; 70 } 71 inline void write(int x){ 72 if(x==0){putchar(‘0‘);return;} if(x<0) putchar(‘-‘),x=-x; 73 int len=0,buf[15]; while(x) buf[len++]=x%10,x/=10; 74 for(int i=len-1;i>=0;i--) putchar(buf[i]+‘0‘);return; 75 } 76 void init(){ 77 m=read();n=read(); 78 for(int i=0;i<m;i++) pig[i]=read(); 79 for(int i=0;i<n;i++){ 80 int a=read(),b; 81 for(int j=0;j<a;j++){ 82 b=read(); 83 b--; 84 cus[b][num[b]++]=i; 85 } 86 buy[i]=read(); 87 } 88 return; 89 } 90 void work(){ 91 92 return; 93 } 94 void print(){ 95 S=n;T=n+1; 96 sol.init(n+2); 97 for(int i=0;i<n;i++) sol.AddEdge(i,T,buy[i]); 98 for(int i=0;i<m;i++){ 99 if(num[i]>0) sol.AddEdge(S,cus[i][0],pig[i]); 100 for(int j=1;j<num[i];j++) sol.AddEdge(cus[i][j-1],cus[i][j],inf); 101 } 102 write(sol.MaxFlow(S,T)); 103 return; 104 } 105 int main(){ 106 init();work();print();return 0; 107 }
标签:
原文地址:http://www.cnblogs.com/chxer/p/4439772.html