卧薪尝胆,厚积薄发。
USACO4.4 追查坏牛奶Pollutant Control
Date: Sat Oct 27 16:57:01 CST 2018 In Category: NoCategory

Description:

给一个图,求边数最小的最小割。
$1\leqslant n\leqslant 32,1\leqslant m\leqslant 1000$

Solution:

把边权设成 $c\times 1010+1$ 做最小割即可。

Code:


#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cctype>
#include<cstring>
using namespace std;
int n,m;
#define MAXN 34
#define MAXM 1010
struct edge
{
int to,nxt;
long long f;
}e[MAXM << 1];
int edgenum = 0;
int lin[MAXN] = {0};
void add(int a,int b,long long f)
{
e[edgenum] = (edge){b,lin[a],f};lin[a] = edgenum++;
e[edgenum] = (edge){a,lin[b],0};lin[b] = edgenum++;
return;
}
int s,t;
int ch[MAXN];
bool BFS()
{
memset(ch,-1,sizeof(ch));ch[s] = 0;
queue<int> q;q.push(s);
while(!q.empty())
{
int k = q.front();q.pop();
for(int i = lin[k];i != -1;i = e[i].nxt)
{
if(ch[e[i].to] == -1 && e[i].f)
{
ch[e[i].to] = ch[k] + 1;
q.push(e[i].to);
}
}
}
return (ch[t] != -1);
}
long long flow(int k,long long f)
{
if(k == t)return f;
long long r = 0;
for(int i = lin[k];i != -1 && f > r;i = e[i].nxt)
{
if(ch[e[i].to] == ch[k] + 1 && e[i].f)
{
int l = flow(e[i].to,min(e[i].f,f - r));
r += l;e[i].f -= l;e[i ^ 1].f += l;
}
}
if(r == 0)ch[k] = -1;
return r;
}
#define INF 0x3f3f3f3f3f3f3f3f
long long dinic()
{
long long ans = 0,r;
while(BFS())while(r = flow(s,INF))ans += r;
return ans;
}
int main()
{
memset(lin,-1,sizeof(lin));
scanf("%d%d",&n,&m);
int a,b;
long long c;
for(int i = 1;i <= m;++i)
{
scanf("%d%d%lld",&a,&b,&c);
add(a,b,c * 1010 + 1);
}
s = 1;t = n;
long long res = dinic();
cout << res / 1010 << " " << res % 1010 << endl;
return 0;
}
In tag: 图论-dinic
Copyright © 2020 wjh15101051
ღゝ◡╹)ノ♡