zoukankan      html  css  js  c++  java
  • Gym

    题目链接:Gym - 101667E How Many to Be Happy?

    题意:
    给出一含有n个结点和m条边的图G,定义该图中的最小生成树(MST)含有的边为happy,而不在MST中的边为unhappy,对于unhappy的边e,删除最少的边数H(e)使得其变为happy,求H(e)之和。

    Solution:

    很可惜没有想出来,开始一直在想的是先把这条边给选了,在搞剩下的东西,其实这样就错了

    应该是考虑如何搞才能让这条边不得不选,那就是这个集合不得不通过这一条边来联通----》应该就能想到最小割了吧。。。。

    由MST的性质可知,对于任意一条不在MST中的边e,会影响e构成MST的只有比它边权小的边,所以要删除的边只可能在这些比e边权小的边当中。

    将这些边权比e小的边选出后建图(不包含e自身),设边e的端点为s、t

    那么只需要令s、t不连通,边e就可以构成MST了,很明显,问题就转化为了求最小割,那么建图的时候只需让边的容量为1即可(求最小割时正向边、反向边容量相等)。

    Code:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int maxn=150;
    const int maxm=550;
    int n,m;
    struct EDGE
    {
        int u;
        int v;
        int w;
    }E[maxm];
    struct edge
    {
        int w;
        int to;
        int next;
    }e[maxm*2];
    int head[maxn],cnt;
    int s,t;
    int depth[maxn],cur[maxn];
    void addedge(int u,int v,int w)
    {
        e[cnt].w=w;
        e[cnt].to=v;
        e[cnt].next=head[u];
        head[u]=cnt;
        cnt++;
        e[cnt].w=w;       //求最小割时反向边容量不为0
        e[cnt].to=u;
        e[cnt].next=head[v];
        head[v]=cnt;
        cnt++;
    }
    bool bfs()
    {
        memset(depth,-1,sizeof(depth));
        queue<int> q;
        q.push(s);
        depth[s]=0;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            for(int i=head[u];i!=-1;i=e[i].next)
            {
                int v=e[i].to;
                if(e[i].w>0&&depth[v]==-1)
                {
                    depth[v]=depth[u]+1;
                    q.push(v);
                }
            }
        }
        return depth[t]!=-1;
    }
    int dfs(int u,int flow)
    {
        if(u==t)
            return flow;
        for(int &i=cur[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            if(depth[v]==depth[u]+1&&e[i].w>0)
            {
                int k=dfs(v,min(flow,e[i].w));
                if(k>0)
                {
                    e[i].w-=k;
                    e[i^1].w+=k;
                    return k;
                }
            }
        }
        return 0;
    }
    int dinic()
    {
        int ans=0;
        while(bfs())
        {
            for(int i=1;i<=n;i++)
                cur[i]=head[i];
            while(int k=dfs(s,INF))
                ans+=k;
        }
        return ans;
    }
    int main()
    {
        scanf("%d %d",&n,&m);
        for(int i=0;i<m;i++)
            scanf("%d %d %d",&E[i].u,&E[i].v,&E[i].w);
        int he=0;
        for(int i=0;i<m;i++)
        {
            memset(head,-1,sizeof(head));
            cnt=0;
            for(int j=0;j<m;j++)
            {
                if(E[j].w<E[i].w)     //仅考虑边权比i小的边
                    addedge(E[j].u,E[j].v,1);   //容量为1
            }
            s=E[i].u;      //设定s、t
            t=E[i].v;
            he+=dinic();
        }
        printf("%d
    ",he);
        return 0;
    }
    

      

  • 相关阅读:
    要学习的东西~!~
    Concrete5官网
    PHP免费源码下载
    PHP源码下载1
    PHP源码下载
    PHP框架之ThinkPHP项目CMS内容管理系统源码及开发手册视频
    SELINUX导致数据修改权限不成功
    mysql参数安全设置
    大话存储(1-5)
    存储知识(4)
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/11644437.html
Copyright © 2011-2022 走看看