【题目来源】:http://codeforces.com/gym/101667

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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

题意和题解思想均来自:javascript:void(0)

【题意】

给定一个无向连通图,令H(e)为使得边e被连通图的最小生成树包含所需要删掉的最少的边数目。

【题解】

思路:构造一个最小生成树,我们需要先选取最小的边,再选取较大的边,这就是最小生成树的Kruskal算法。那么当边e连接u,v两点时,我们要保证连接u,v的最短边就是e,才能保证最小生成树内包含e,所以我们只要使e的两个端点为源点和汇点,跑出一个最小割,即为H(e),最后求和即可。

代码是用自己整理的板子写的

【注意事项】

由于是求无向图的最小割,边权都是1才可以用Dinic,那么初始化残留图的时候:正向弧方向弧上的流量都是1(边权不为1不知道可不可以用Dinic)。

那么可以直接套用dinic。
【代码】

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e3+50;
const ll inf=0x3f3f3f3f3f3f3f3f;
struct node{
	int u,v,nxt;
	ll f;
}e[N*10];
struct edge
{
    int u,v;
    ll w;
    operator <(const edge &o) const
    {
        return w<o.w;
    }
}edg[N];
int n,m,cnt=0,st,ed,dep[N],head[N];
int q[N*2];///模拟栈
int tot,tail;
void add(int u,int v,ll f)
{
	e[cnt].u=u;
	e[cnt].v=v;
	e[cnt].f=f;
	e[cnt].nxt=head[u];
	head[u]=cnt++;
	e[cnt].u=v;
	e[cnt].v=u;
	e[cnt].f=f;//无向图的正向弧,反向弧都是1
	e[cnt].nxt=head[v];
	head[v]=cnt++;
}

bool bfs()
{
	memset(dep,-1,sizeof(dep));
	dep[st]=1;
	q[tot=0]=st,tail=1;
	while(tot<tail){
		int u=q[tot++];
		if(u==ed) break;
		for(int i=head[u];i!=-1;i=e[i].nxt){
			int v=e[i].v;
			if(dep[v]!=-1||e[i].f==0) continue;
			dep[v]=dep[u]+1;
			q[tail++]=v;
		}
	}
	return dep[ed]!=-1;
}
ll dfs(int u,ll flow)
{
	ll res=flow;
	if(u==ed) return flow;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v=e[i].v;
		if(dep[v]!=dep[u]+1||!e[i].f) continue;
		ll d=dfs(v,min(res,e[i].f));
		e[i].f-=d;
		e[i^1].f+=d;
		res-=d;
		if(res==0) break;
	}
	if(flow==res) dep[u]=-1;
	return flow-res;
}
ll dinic()
{
    for(int i=0;i<=cnt;++i) e[i].f=1;
	ll ans=0;
	ll d;
	while(bfs()){
        while(d=dfs(st,inf))
            ans+=d;
	}
	return ans;
}

int main()
{

    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d%lld",&edg[i].u,&edg[i].v,&edg[i].w);
    }
    sort(edg+1,edg+1+m);
    cnt=0;
    memset(head,-1,sizeof(head));
    int r=1;
    ll ans=0;
    for(int i=1;i<=m;++i)
    {
        while(edg[r].w<edg[i].w)
        {
            add(edg[r].u,edg[r].v,1);
            ++r;
        }
        st=edg[i].u;
        ed=edg[i].v;
        ans+=dinic();
    }
    printf("%lld\n",ans);
	return 0;
}