zoukankan      html  css  js  c++  java
  • P5934[清华集训2012]最小生成树【最小割】

    正题

    题目链接:https://www.luogu.com.cn/problem/P5934


    题目大意

    给出\(n\)个点\(m\)条边的一张图,再加入一条边\((u,v,L)\)求至少删掉多少条边可以使得这条边即在最小生成树上又在最大生成树上。

    \(1\leq n\leq 2\times 10^4,1\leq m\leq 2\times 10^5\)


    解题思路

    稍微思考一下就不难发现这两个问其实是没有影响的,因为第一个问显然只需要删去边权小于\(L\)的,第二个问显然只需要删去边权大于\(L\)的。所以考虑分开求然后相加

    那么考虑怎么让它在最小生成树上。考虑我们之前\(\text{LCT}\)维护最小生成树的做法,我们加入一条边\((u,v,w)\)的时候,是找到\(u\sim v\)路径上的最大边然后和\(w\)比较。

    那么如果原图中存在一条不经过这条边的路径且最大值比\(u,v\)要小。那么显然这条路径可以完全取代这条边,所以这条边一定不是最小生成树上的边。

    那么同理我们只需要把所有边权小于\(L\)的边加入,然后再删去最少的边使得\(u,v\)不连通即可。这个用最小割解决就好了。

    最大生成树同理


    code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue> 
    using namespace std;
    const int N=2e4+10,M=2e5+10,inf=1e9;
    struct node{
    	int to,next,w;
    }a[M<<1];
    struct edge{
    	int x,y,w;
    }e[M];
    int n,m,s,t,L,tot=1,ls[N],dep[N],ans;
    queue<int> q;
    void addl(int x,int y,int w){
    	a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
    	a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=w;
    	return;
    }
    bool bfs(){
    	while(!q.empty())q.pop();q.push(s);
    	memset(dep,0,sizeof(dep));dep[s]=1;
    	while(!q.empty()){
    		int x=q.front();q.pop();
    		for(int i=ls[x];i;i=a[i].next){
    			int y=a[i].to;
    			if(dep[y]||!a[i].w)continue;
    			dep[y]=dep[x]+1;
    			if(y==t)return 1;
    			q.push(y);
    		}
    	}
    	return 0;
    }
    int dinic(int x,int flow){
    	if(x==t)return flow;
    	int rest=0,k;
    	for(int i=ls[x];i;i=a[i].next){
    		int y=a[i].to;
    		if(dep[x]+1!=dep[y]||!a[i].w)continue;
    		rest+=(k=dinic(y,min(flow-rest,a[i].w)));
    		a[i].w-=k;a[i^1].w+=k;
    		if(rest==flow)return flow;
    	}
    	if(!rest)dep[x]=0;
    	return rest;
    }
    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].w);
    	scanf("%d%d%d",&s,&t,&L);
    	for(int i=1;i<=m;i++)
    		if(e[i].w<L)addl(e[i].x,e[i].y,1);
    	while(bfs())
    		ans+=dinic(s,inf);
    	memset(ls,0,sizeof(ls));tot=0;
    	for(int i=1;i<=m;i++)
    		if(e[i].w>L)addl(e[i].x,e[i].y,1);
    	while(bfs())
    		ans+=dinic(s,inf);
    	printf("%d\n",ans);
    	return 0;
    }
    
  • 相关阅读:
    ZooKeeper 相关知识
    zookeeper 启动和停止脚本
    es 6.4.3 版本的es的处理方式
    SpringBoot启动使用elasticsearch启动异常:Received message from unsupported version:[2.0.0] minimal compatible
    windows下安装elasticsearch-6.4.3和elasticsearch-head插件
    二项式公式
    计算公式
    大规模数据如何检索?
    设计数据服务:为报表服务提供服务接口
    win10 桌面快捷键技术
  • 原文地址:https://www.cnblogs.com/QuantAsk/p/14613855.html
Copyright © 2011-2022 走看看