题意:
给定一个边带正权的连通无向图,现在加入一条边权为L的边(u,v),那么需要删掉最少多少条边,才能够使得这条边既可能出现在最小生成树上,也可能出现在最大生成树上?
题解:
以前看着一脸懵逼,现在好像就是那样。
容易想到,当u->v存在一条路径,上面不存在>=L的边,那么新边一定不在最小生成树上,所以将所有小于L的边建出来,做最小割就可以了。
最大生成树上同样,最后答案加起来。
code:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
const int inf=(1<<28);
struct node{
int x,y,next,c,other;
}a[2110000],e[210000];int last[20010],len=0;
int h[20010],s[20010],st,ed,n,m;
void ins(int x,int y,int c)
{
int k1=++len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len;
int k2=++len;
a[len].x=y;a[len].y=x;a[len].c=c;
a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool bt_h()
{
memset(h,0,sizeof(h));
int l=1,r=2;s[l]=st;h[st]=1;
while(l!=r)
{
int x=s[l];
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[y]==0&&a[i].c>0) h[y]=h[x]+1,s[r++]=y;
}
l++;
}
return h[ed]!=0;
}
int findflow(int x,int f)
{
if(x==ed) return f;
int t,ans=0;
for(int i=last[x];i;i=a[i].next)
{
int y=a[i].y;
if(h[x]+1==h[y]&&a[i].c>0&&ans<f)
{
ans+=(t=findflow(y,min(a[i].c,f-ans)));
a[i].c-=t;a[a[i].other].c+=t;
}
}
if(ans==0) h[x]=0;
return ans;
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d %d %d",&e[i].x,&e[i].y,&e[i].c);
int l,ans=0;scanf("%d %d %d",&st,&ed,&l);
len=0;memset(last,0,sizeof(last));
for(int i=1;i<=m;i++)
if(e[i].c<l) ins(e[i].x,e[i].y,1);
while(bt_h()) ans+=findflow(st,inf);
len=0;memset(last,0,sizeof(last));
for(int i=1;i<=m;i++)
if(e[i].c>l) ins(e[i].x,e[i].y,1);
while(bt_h()) ans+=findflow(st,inf);
printf("%d",ans);
}