E.How Many to Be Happy?+dinic

E: How Many to Be Happy?

时间限制: 1 Sec  内存限制: 128 MB
提交: 75  解决: 29
[提交] [状态] [讨论版] [命题人: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
#include<bits/stdc++.h> 

using namespace std;
const int N=105,M=1005;

int n,m,head[N],cnt;
int Next[M],To[M],flow[M];

struct node{
	int u,v,w;
}a[M];

void addedge(int u,int v,int f){
	Next[cnt]=head[u],To[cnt]=v,flow[cnt]=f,head[u]=cnt++;
}

queue<int>Q;
int level[N];
int bfs(int S,int T){
	while(!Q.empty())Q.pop();
	memset(level,0,sizeof(level));
	level[S]=1;
	Q.push(S);
	while(!Q.empty()){
		int u=Q.front();
		for(int i=head[u];~i;i=Next[i]){
			int v=To[i];
			if(!flow[i]||level[v])continue;
			level[v]=level[u]+1;
			Q.push(v);
			if(v==T)return 1;
		}
		Q.pop();
	}
	return 0;
}
int dfs(int u,int minf,int T){
	if(u==T)return minf;
	int res=0;
	for(int i=head[u];~i;i=Next[i]){
		int v=To[i];
		if(level[v]!=level[u]+1||!flow[i])continue;
		int tmp=dfs(v,min(minf,flow[i]),T);
		minf-=tmp,res+=tmp;
		flow[i]-=tmp,flow[i^1]+=tmp;
		if(minf==0)return res;
	}
	level[u]=0;
	return res;
}
int dinic(int S,int T){
	for(int i=0;i<cnt;i++)flow[i]=1;
	int res=0;
	while(bfs(S,T))
		res+=dfs(S,0x3f3f3f3f,T);
	return res;
}

int main(){
	scanf("%d %d",&n,&m);
	for(int i=1;i<=m;i++)
	scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
	sort(a+1,a+1+m,[](node a,node b){return a.w<b.w;});
	cnt=0;
	memset(head,-1,sizeof(head));
	int ans=0,l=1,r=1;
	for(;l<=m;l++){
		while(a[r].w<a[l].w){
			addedge(a[r].u,a[r].v,1);
			addedge(a[r].v,a[r].u,1);
			r++;			
		}
		ans+=dinic(a[l].u,a[l].v);
	}
	printf("%d\n",ans);
	return 0;
}




 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值