标签:iss bool eps source using end ble style ios
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 7624 | Accepted: 2562 |
Description
Input
Output
Sample Input
3 2 John 0 1 Rose 1 Mary 1 5 4 ACM 1 2 3 ICPC 0 1 Asian 0 2 3 Regional 1 2 ShangHai 0 2 0 0
Sample Output
2 2
Source
//二分最大值mid,源点连向n个联系人权值为1,联系人连向它可以属于的分类,m个分类与汇点建边权值为mid(该分类最多mid个人) //看最大流是否等于n。 #include<iostream> #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; const int maxn=2009; const int inf=0x7fffffff; vector<int>v[maxn]; struct Edge{ int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){} }; struct Dinic{ int n,m,s,t; vector<Edge>edges; vector<int>g[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void Init(int n){ this->n=n; 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));//反向弧 m=edges.size(); g[from].push_back(m-2); g[to].push_back(m-1); } bool Bfs(){ memset(vis,0,sizeof(vis)); queue<int>q; q.push(s); d[s]=0; vis[s]=1; while(!q.empty()){ int x=q.front();q.pop(); for(int i=0;i<(int)g[x].size();i++){ Edge &e=edges[g[x][i]]; if(!vis[e.to]&&e.cap>e.flow){ vis[e.to]=1; d[e.to]=d[x]+1; q.push(e.to); } } } return vis[t]; } int Dfs(int x,int a){ if(x==t||a==0) return a; int flow=0,f; for(int&i=cur[x];i<(int)g[x].size();i++){ Edge &e=edges[g[x][i]]; if(d[x]+1==d[e.to]&&(f=Dfs(e.to,min(a,e.cap-e.flow)))>0){ e.flow+=f; edges[g[x][i]^1].flow-=f; flow+=f; a-=f; if(a==0) break; } } return flow; } int Maxflow(int s,int t){ this->s=s;this->t=t; int flow=0; while(Bfs()){ memset(cur,0,sizeof(cur)); flow+=Dfs(s,inf); } return flow; } }dc; int n,m; char ch[20]; bool solve(int mid){ dc.Init(n+m+2); for(int i=1;i<=n;i++){ dc.Addedge(0,i,1); for(int j=0;j<(int)v[i].size();j++) dc.Addedge(i,v[i][j]+n,1); } for(int i=n+1;i<=n+m;i++){ dc.Addedge(i,n+m+1,mid); } return dc.Maxflow(0,n+m+1)==n; } int main() { while(scanf("%d%d",&n,&m)&&(n+m)){ for(int i=1;i<=n;i++){ scanf("%s",ch); while(getchar()!=‘\n‘){ int x; scanf("%d",&x); x++; v[i].push_back(x); } } int l=0,r=n,mid,ans=0; while(l<=r){ mid=(l+r)/2; if(solve(mid)){ ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); for(int i=1;i<=n;i++) v[i].clear(); } return 0; }
标签:iss bool eps source using end ble style ios
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6906245.html