过山车 ( 二分图最大匹配
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 100000;
const int INF = 100000000;
struct Node{
int ne;
int to;
int w;
}e[N<<1];
int head[N],deth[N];
int n,m,k,x,y,st,en,val,cnt;
void init()
{
memset(head,-1,sizeof(head));
memset(deth,0,sizeof(deth));
cnt = st = 0;
en = n + m + 1;
}
void add(int u,int v,int val)
{
e[cnt].to = v;
e[cnt].w = val;
e[cnt].ne = head[u];
head[u] = cnt ++;
}
bool bfs()
{
memset(deth,0,sizeof(deth));
queue<int>q;
deth[st] = 1;
q.push(st);
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i=head[now];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == 0 && e[i].w)
{
deth[to] = deth[now] + 1;
q.push(to);
}
}
}
if(deth[en] == 0)
return 0;
return 1;
}
int dfs(int u,int dist)
{
if(u == en)
return dist;
int temp = dist;
for(int i=head[u];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == deth[u] + 1 && e[i].w)
{
int di = dfs(to,min(e[i].w,temp));
if(di>0)
{
temp -= di;
e[i].w -= di;
e[i^1].w += di;
if(temp <= 0)
return dist;
}
}
}
return dist - temp;
}
int dinic()
{
int ans = 0;
while(bfs())
{
while(int di = dfs(st,INF))
ans += di;
}
return ans;
}
int main()
{
while(scanf("%d",&k) && k)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
{
add(st,i,1);
add(i,st,0);
}
for(int i=n+1;i<=n+m;i++)
{
add(i,en,1);
add(en,i,0);
}
for(int i=1;i<=k;i++)
{
scanf("%d%d",&x,&y);
add(x,n+y,1);
add(n+y,x,0);
}
printf("%d\n",dinic());
}
return 0;
}
匈牙利做法
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 555;
const int maxm = 5555;
struct Node{
int to;
int ne;
}e[maxn];
int head[maxn],match[maxn];
int cnt,ans;
bool vis[maxn];
int k,m,n;
void init()
{
memset(head,-1,sizeof(head));
memset(match,-1,sizeof(match));
cnt = ans = 0;
}
void add(int u,int v)
{
e[cnt].to = v;
e[cnt].ne = head[u];
head[u] = cnt ++;
}
bool findpath(int u)
{
for(int i=head[u];~i;i=e[i].ne)
{
int to = e[i].to;
if(!vis[to])
{
vis[to] = 1;
if(match[to] == -1 || findpath(match[to]))
{
match[to] = u;
return true;
}
}
}
return false;
}
void Hungary()
{
for(int i=1;i<=m;i++)
{
memset(vis,0,sizeof(vis));
if(findpath(i))
ans ++;
}
}
int main()
{
int u,v;
while(scanf("%d",&k) && k)
{
init();
scanf("%d%d",&m,&n);
for(int i=1;i<=k;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
}
Hungary();
printf("%d\n",ans);
}
return 0;
}
二分图多重匹配
全文阅读请移步
二分图多重匹配
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 10000;
const int INF = 100000000;
struct Node{
int to;
int ne;
int w;
}e[N<<1];
int head[N],deth[N];
int T,x,y,st,k,en,n,m,val,cnt;
void init()
{
memset(head,-1,sizeof(head));
cnt = st = 0;
en = n + m + 1;
}
void add(int u,int v,int val)
{
e[cnt].to = v;
e[cnt].w = val;
e[cnt].ne = head[u];
head[u] = cnt ++;
}
int bfs()
{
memset(deth,0,sizeof(deth));
queue<int>q;
deth[st] = 1;
q.push(st);
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i=head[now];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == 0 && e[i].w>0)
{
deth[to] = deth[now] + 1;
q.push(to);
}
}
}
if(deth[en] == 0)
return 0;
return 1;
}
int dfs(int u,int dist)
{
if(u == en)
return dist;
int temp = dist;
for(int i =head[u];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == deth[u] + 1 && e[i].w > 0)
{
int di = dfs(to,min(e[i].w,temp));
if(di>0)
{
temp -= di;
e[i].w -= di;
e[i^1].w += di;
if(temp <= 0)
return dist;
}
}
}
return dist - temp;
}
int dinic()
{
int ans = 0;
while(bfs())
{
while(int di = dfs(st,INF))
ans += di;
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
init();
int sum = 0;
for(int i=1;i<=m;i++)
{
scanf("%d",&x);
sum += x;
add(n+i,en,x);
add(en,n+i,0);
}
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(st,i,x);
add(i,st,0);
while(y--)
{
scanf("%d",&k);
add(i,n+k,1);
add(n+k,i,0);
}
}
if(sum == dinic())
puts("Yes");
else puts("No");
}
return 0;
}
假期的宿舍 ( 二分图完美匹配???
1. 有床的向 汇点 连边2. 源点 向需要床的连边,边权都为1
3. 跑最大流判断maxflow 是否和需要住宿的人 sum 相等即可
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1000;
const int INF = 100000000;
struct Node{
int ne;
int to;
int w;
}e[N<<1];
int head[N],deth[N];
int bed[N],home[N];
int st,en,n,m,x,y,val,cnt,k,T;
void init()
{
memset(head,-1,sizeof(head));
memset(deth,0,sizeof(deth));
st = cnt = 0;
en = n * 2 + 1;
}
void add(int u,int v,int val)
{
e[cnt].to = v;
e[cnt].w = val;
e[cnt].ne = head[u];
head[u] = cnt ++;
}
bool bfs()
{
memset(deth,0,sizeof(deth));
queue<int>q;
deth[st] = 1;
q.push(st);
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i=head[now];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == 0 && e[i].w>0)
{
deth[to] = deth[now] + 1;
q.push(to);
}
}
}
if(deth[en] == 0)
return 0;
return 1;
}
int dfs(int u,int dist)
{
if(u == en)
return dist;
int temp = dist;
for(int i=head[u];~i;i=e[i].ne)
{
int to = e[i].to;
if(deth[to] == deth[u] + 1 && e[i].w)
{
int di = dfs(to,min(e[i].w,temp));
if(di>0)
{
temp -= di;
e[i].w -= di;
e[i^1].w += di;
if(temp <= 0)
return dist;
}
}
}
return dist - temp;
}
int dinic()
{
int ans = 0;
while(bfs())
{
while(int di = dfs(st,INF))
ans += di;
}
return ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
init();
int sum = 0;
for(int i=1;i<=n;i++)
{
scanf("%d",&bed[i]);//有床的向汇点连边
if(bed[i])
{
add(i+n,en,1);
add(en,i+n,0);
}
}
for(int i=1;i<=n;i++)
{
scanf("%d",&home[i]);
if(!bed[i] || (bed[i] && !home[i])) //源点向需要床的连边
{
sum ++;
add(st,i,1);
add(i,st,0);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&x);
if(x ||i == j)
{
add(i,j+n,1);
add(j+n,i,0);
}
}
}
if(sum == dinic())
puts("^_^");
else puts("T_T");
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
#define ABSs(x) ((x)<0?-(x):(x))
const int N = 105;
struct Node{
int ne;
int to;
int w;
int co;
}e[N*(N+2)<<1+5];
int head[N<<1+6],hh[N*N],mm[N*N];
int path[N<<1+5],v[N<<1+5],dis[N<<1+5];
int n,m,len,st,en,cnt;
char s[N];
void init()
{
memset(head,-1,sizeof(head));
cnt = 0;
}
void add(int u,int v,int co,int val)
{
e[cnt].to = v;
e[cnt].w = val;
e[cnt].co = co;
e[cnt].ne = head[u];
head[u] = cnt ++;
}
bool spfa(int st,int en)
{
memset(path,-1,sizeof(path));
memset(v,0,sizeof(v));
memset(dis,127,sizeof(dis));
queue<int>q;
q.push(st);
v[st] = 1;
dis[st] = 0;
while(!q.empty())
{
int now = q.front();
q.pop();
for(int i=head[now];~i;i=e[i].ne)
{
int to = e[i].to;
if(e[i].w > 0 && dis[to] > dis[now] + e[i].co)
{
dis[to] = dis[now] + e[i].co;
path[to] = i;
if(!v[to])
{
v[to] = 1;
q.push(to);
}
}
}
v[now] = 0;
}
return dis[en] != dis[N<<1+4];
}
int MCMF(int st,int en)
{
int ans = 0;
while(spfa(st,en))
{
int minn = 1e9;
for(int i=path[en];~i;i=path[e[i^1].to])
{
if(minn > e[i].w)
minn = e[i].w;
}
ans += minn*dis[en];
for(int i=path[en];~i;i=path[e[i^1].to])
{
e[i].w -= minn;
e[i^1].w += minn;
}
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m) && n+m)
{
init();
int hlen = 0;
int mlen = 0;
for(int i=0;i<n;i++)
{
scanf("%s",s);
for(int j=0;j<m;j++)
{
if(s[j] == 'H')
{
hh[hlen++] = i*m+j;
}
else if(s[j] == 'm')
{
mm[mlen ++] = i*m+j;
}
}
}
int x0,x1,y0,y1;
for(int i=0;i<mlen;i++)
{
for(int j=0;j<hlen;j++)
{
x0 = mm[j]/m;
y0 = mm[j]%m;
x1 = hh[i]/m;
y1 = hh[i]%m;
add(i+1,j+mlen+1,ABSs(x0-x1) + ABSs(y0-y1),1);
add(j+mlen+1,i+1,- ABSs(x0-x1) - ABSs(y0-y1),0);
}
}
n = mlen;
m = hlen;
st = 0;
en = n + m + 1;
for(int i=0;i<n;i++)
{
add(st,i+1,0,1);
add(i+1,st,0,0);
}
for(int j=0;j<m;j++)
{
add(j+n+1,en,0,1);
add(en,j+n+1,0,0);
}
printf("%d\n",MCMF(st,en));
}
return 0;
}