http://poj.org/problem?id=1274
題意:n頭牛 m個穀倉,一頭牛只願意去 一個固定的穀倉產奶(牛喜歡好幾個穀倉),問有多少頭牛可以去產內
題解:最大流寫,想着儘量佔滿穀倉構建模型,建立超級源點 超級匯點,源點連接所有牛 , 牛連接喜歡的穀倉, 穀倉連接匯點 ,然後跑就行了
二分圖也可以寫,我不會
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<queue>
using namespace std;
typedef long long LL;
const int N = 20005;
const int INF = 0x3f3f3f3f;
bool vis[N];
struct Edge{
int to, cap, flow, next;
}edge[N*50];
int n, m, cnt;//n是點 m是邊 cnt是加邊操作後的邊
int head[N];//鄰接表
int dis[N];//分層 等級
int cur[N];//弧優化
void add(int u, int v, int w){
edge[cnt] = (struct Edge){v, w, 0, head[u]};
head[u] = cnt++;
edge[cnt] = (struct Edge){u, 0, 0, head[v]};
head[v] = cnt++;
}
bool bfs(int start, int endd){//分層
memset(dis, -1, sizeof(dis));
memset(vis, false, sizeof(vis));
queue<int>que;
dis[start] = 0;
vis[start] = true;
que.push(start);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -1; i = edge[i].next){
Edge E = edge[i];
if(!vis[E.to] && E.flow<E.cap){
dis[E.to] = dis[u]+1;
vis[E.to] = true;
if(E.to == endd) return true;
que.push(E.to);
}
}
}
return false;
}
int dfs(int x, int res, int endd){ //增廣
if(x == endd || res == 0) return res;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = edge[i].next){
Edge E = edge[i];
if(dis[E.to] == dis[x]+1){
f = dfs(E.to, min(res, E.cap-E.flow), endd);
if(f>0){
edge[i].flow += f;
edge[i^1].flow -= f;
flow += f;
res -= f;
if(res == 0) break;
}
}
}
return flow;
}
int max_flow(int start, int endd){
int flow = 0;
while(bfs(start, endd)){
memcpy(cur, head, sizeof(head));//初始化弧優化數組
flow += dfs(start, INF, endd);
}
return flow;
}
void init(){//初始化
cnt = 0;
memset(head, -1, sizeof(head));
}
int main(){
int na,nb;
while(cin>>na>>nb){
init();
int sp=0;//超級源點
int tp=na+nb+1;//超級匯點
for(int i=1;i<=na;i++){
add(sp,i,1);//源點與 牛連邊
int k;
cin>>k;
for(int j=1;j<=k;j++){
int x;
cin>>x;
add(i,na+x,1);//牛與穀倉連邊
}
}
for(int j=1;j<=nb;j++){
add(na+j,tp,1);//穀倉與匯點連邊
}
int ans=max_flow(sp,tp);
cout<<ans<<endl;
}
return 0;
}