天天看點

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

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

時間限制: 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′.

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

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 複雜度

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

   加上 每條邊  ,  複雜度為: 

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

[代碼]

#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;
}
           

繼續閱讀