@2017-2018 ACM-ICPC, Asia Daejeon Regional Contest H; How Many to Be Happy? ( 最小割 dinic算法)

O(n^2*m) 时间限制: 1 Sec  内存限制: 128 MB
提交: 80  解决: 32
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Let G be a connected simple undirected graph where each edge has an associated weight. Let’s consider the popular MST (Minimum Spanning Tree) problem. Today, we will see, for each edge e, how much modification on G is needed to make e part of an MST for G. For an edge e in G, there may already exist an MST for G that includes e. In that case, we say that e is happy in G and we define H(e) to be 0. However, it may happen that there is no MST for G that includes e. In such a case, we say that e is unhappy in G. We may remove a few of the edges in G to make a connected graph G′ in which e is happy. We define H(e) to be the minimum number of edges to remove from G such that e is happy in the resulting graph G′.

Figure E.1. A complete graph with 3 nodes.

Consider the graph in Figure E.1. There are 3 nodes and 3 edges connecting the nodes. One can easily see that the MST for this graph includes the 2 edges with weights 1 and 2, so the 2 edges are happy in the graph. How to make the edge with weight 3 happy? It is obvious that one can remove any one of the two happy edges to achieve that.
Given a connected simple undirected graph G, your task is to compute H(e) for each edge e in G and print the total sum.

 

输入

Your program is to read from standard input. The first line contains two positive integers n and m, respectively, representing the numbers of vertices and edges of the input graph, where n ≤ 100 and m ≤ 500. It is assumed that the graph G has n vertices that are indexed from 1 to n. It is followed by m lines, each contains 3 positive integers u, v, and w that represent an edge of the input graph between vertex u and vertex v with weight w. The weights are given as integers between 1 and 500, inclusive.

 

输出

Your program is to write to standard output. The only line should contain an integer S, which is the sum of H(e) where e ranges over all edges in G.

 

样例输入

3 3
1 2 1
3 1 2
3 2 3

 

样例输出

1

 

来源/分类

ICPC 2017 Daejeon 

 

题意:

N个点 M个边的无向图.  边权 不相同.,

求  成为最小生成树的每条边,   最少需要删去边的数量之和.

也就是说,  对每条边跑最小割. 

Dinc 复杂度O(n^2*m)   加上 每条边  ,  复杂度为: O(n^2*m^2)

 

 

[代码]

#include <bits/stdc++.h>
#include <stdio.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e5+10;
const int mod =1e9+7;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m;

struct node{
    int v,w,next; //u  v 从 u-v 权值为w
}edge[maxn];
struct Nn
{
	int u,v,w;
}a[maxn];
int head[maxn],num[maxn],start,END,cnt,sum;
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
    memset(edge,0,sizeof(edge));
}

void add(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;

    edge[cnt].v=u;
    edge[cnt].w=w;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int bfs()
{
    queue<int>Q;
    mem(num,0);
    num[start]=1;
    Q.push(start);
    while(!Q.empty())
    {
        int t=Q.front();
        Q.pop();
        if(t==END)
            return 1;
        for(int i=head[t];i!=-1;i=edge[i].next)// 链式前向星访问找增广路
        {
            int t1= edge[i].v;//下一个节点
            int t2= edge[i].w;// 当前点 权值
            if(t2&&num[t1]==0)// 当前点存在 并且下一个点没有访问
            {
                num[t1]=num[t]+1;// 点=1
                if(t1==END)//  结束
                    return 1;
                Q.push(t1);
            }
        }
    }
    return 0;
}
int dfs(int u,int maxflow)
{
    if(u==END)
        return maxflow;
    int res=0;

    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int t1=edge[i].v;// 下一个节点
        int t2=edge[i].w;// 当前节点
        if(t2&&num[t1]==num[u]+1)
        {
            int temp=dfs(t1,min(maxflow-res,t2));// 选择流 小的一部分

            edge[i].w-=temp;// 正向减少
            edge[i^1].w+=temp;//反向增加
            res+=temp;
            if(res==maxflow)
                return res;
        }
    }
    if(!res)
        num[u]=-1;
    return res;
}
ll Dinic()
{
    ll ans=0;
    while(bfs())
    {
        ans+=dfs(start,INF);
    }
    return ans;
}

int cmp(Nn a,Nn b)
{
	return a.w < b.w;
}
ll solve(int x)
{
	init();
	rep(i,1,m)
	{
		if( a[i].w < a[x].w)
		{
			add(a[i].u,a[i].v,1);
		}
	}
	start = a[x].u,END = a[x].v;
	return Dinic();
}
int main(int argc, char const *argv[])
{
	scanf("%d %d",&n,&m);
	rep(i,1,m)
	{
		scanf("%d %d %d",&a[i].u,&a[i].v,&a[i].w);
	}
	sort(a+1,a+m+1,cmp);
	ll ans  = 0 ;
	rep(i,1,m)
		ans += solve(i);
	printf("%lld\n",ans );
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值