题目链接
Solution
一眼看过去就是最小割,但是要求割边最少的最小的割.
所以要用骚操作...
建边的时候每条边权 \(w = w * E + 1;\)
这样得到最大流 \(maxflow \div E\) ,最少割边数 \(maxflow~mod~E\)
道理很简单,如果原先两类割边都是最小割,那么求出的最大流相等
但边权变换后只有边数小的才是最小割了
乘 \(E\) 是为了保证边数叠加后依然是余数,不至于影响求最小割的结果
因为假设最小割\(=k\),那么现在新图的最小割为\(k*E+p\) , \(p\) 为割的边数,本质上是,原来你割一条边,需要代价,
由于你要求边数最小 所以你多割一条边,就多一的代价,但是这个代价不足以影响到原来的代价.
原来割一条边,代价\(x_i\),现在割一条边,代价 \(x_i*A+1\) ,只要让 \(A>m+1\) , \(m\) 为边数,即使割了所有的边,自己加上去的代价也就为 \(m\)
Code
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define in(x) x=read()
#define ll long long
using namespace std;
const int mod=6008;
ll cost[mod+10],ans=0;
int to[mod+10],nxt[mod+10],head[mod+10];
int depth[mod+10],cur[mod+10],v[mod+10];
int n,m,tot=1;
int read()
{
int f=1,ret=0;char ch=getchar();
while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return f*ret;
}
void add(int x,int y,ll z)
{
to[++tot]=y;
cost[tot]=z;
nxt[tot]=head[x];
head[x]=tot;
}
bool bfs()
{
queue<int> q;
memset(depth,0,sizeof(depth));
q.push(1);depth[1]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for (int i=head[x];i;i=nxt[i])
{
int y=to[i];
if ((!depth[y])&&cost[i]) depth[y]=depth[x]+1,q.push(y);
}
}
return depth[n]==0?false:true;
}
ll dfs(int x,ll f)
{
ll w=0,used=0;
if (x==n) return f;
for (int& i=cur[x];i;i=nxt[i])
{
int y=to[i];
if (depth[y]==depth[x]+1)
{
w=f-used;w=dfs(y,min(cost[i],w));
cost[i]-=w;cost[i^1]+=w;used+=w;
if (used==f) return f;
}
}
if (!used) depth[x]=-1;
return used;
}
int main()
{
in(n); in(m);
for (int i=1;i<=m;i++)
{
int x,y;ll z;
in(x),in(y),in(z);
add(x,y,z*mod+1);add(y,x,0);
}
while(bfs())
{
for(int i=1;i<=n;i++)
cur[i]=head[i];
ans+=dfs(1,inf);
}
printf("%lld %lld\n",ans/mod,ans%mod);
return 0;
}