标签:printf else desc from ber bsp tom contain redist
Description
Input
Output
Sample Input
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
Sample Output
7
#include<cstdio> #include<vector> #include<queue> #include<cstring> using namespace std; const int INF=0x3f3f3f3f,MAXN=1e3+5; int mp[MAXN][MAXN],num[MAXN],p[MAXN],pre[MAXN],a[MAXN],m,n; struct Edge { int from,to,cap,flow; Edge(){} Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}; }; vector<int>G[1005]; vector<Edge>edges; void init() { for(int i=0;i<n;i++) G[i].clear(); edges.clear(); } void addedge(int from,int to,int cap) { edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); int m=edges.size(); G[to].push_back(m-1); G[from].push_back(m-2); } int maxflow(int s,int t) { int flow=0; while(1) { memset(a,0,sizeof(a)); queue<int>q; q.push(s); a[s]=INF; while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0;i<G[x].size();i++) { Edge& e=edges[G[x][i]]; if(!a[e.to]&&e.cap>e.flow) { p[e.to]=G[x][i]; a[e.to]=min(a[x],e.cap-e.flow); q.push(e.to); } } if(a[t])break; } if(!a[t])break; for(int u=t;u!=s;u=edges[p[u]].from) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; } flow+=a[t]; } return flow; } int main() { int m,n; scanf("%d%d",&m,&n); for(int i=1;i<=m;i++)scanf("%d",&num[i]); for(int i=1;i<=n;i++) { int cnt; scanf("%d",&cnt); while(cnt--) { int t; scanf("%d",&t); if(!pre[t]) addedge(0,i,num[t]); else addedge(pre[t],i,INF); pre[t]=i; } scanf("%d",&cnt); addedge(i,n+1,cnt); } printf("%d\n",maxflow(0,n+1)); return 0; }
标签:printf else desc from ber bsp tom contain redist
原文地址:http://www.cnblogs.com/homura/p/6074083.html