您的位置:首页 > 其它

POJ 1149 PIGS

2015-07-19 18:59 246 查看
最大流,

给定M个猪圈 N个商人 每个商人可以选择特定的猪圈买猪 当一个商人同时可以选择多个猪圈时,此时猪圈可以互通,就是此时两猪圈的流量上限为INF

#include "cstring"
#include "iostream"
#include "cstdio"
#include "queue"
using namespace std;
typedef long long LL;
const int M=105;
const int maxn=2147483648;
const int INF = 0x3f3f3f3f;

int map[M][M],flow[M][M];
int p[M],a[M];
int f,n;

void EK(int s, int t)
{
queue<int>q;
memset(flow,0,sizeof(flow));
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
f=0;
while(1)
{
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
for(int v = 0; v <= n+1; ++v)
if(!a[v] && map[u][v] > flow[u][v])
{
p[v] = u;
q.push(v);
a[v] = a[u] < map[u][v] - flow[u][v]? a[u]:map[u][v] - flow[u][v];
}
}
if(a[t] == 0) break;
for(int u = t; u != s; u = p[u])
{
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
f += a[t];
}

}

int main()
{
int i,j,k;
int s,t,m;
int num[1000+10];
int vis[1000+10];
int nn,mm;
cin>>mm>>nn;
s=0,t=nn+1;
n=nn;
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for(i = 1; i <= mm; ++i)
cin>>num[i];
for(i = 1; i <= nn; ++i)
{
cin>>m;
for(j = 0; j < m; ++j)
{
int temp;
cin>>temp;
if(vis[temp]==0)
map[s][i] += num[temp]; //
else
map[vis[temp]][i]=INF; //猪圈互通
vis[temp]=i;
}
cin>>map[i][t];  //到超级汇点的流量
}

EK(s,t);
cout<<f<<endl;

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: