How Many to Be Happy?

时间限制: 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

来源/分类

ICPC 2017 Daejeon


最小生成树的MST性质的应用。我们想让某一条边一定是最小生成树中的边,只要找到任意一种点集的分配,使得这条边的两个顶点在不同的分配中且边权是连接这两个分配的所有边中最小的那一个。显然只有边权比它小的边才会影响它是不是在最小生成树中。于是我们可以只在图中保留边权小于当前边权的边,看看是否能找到一种点集的分配。显然当这个边的两个顶点在新图中仍然连通时,我们找不到这种分配,于是就需要砍掉若干边使两顶点不连通,于是题目就转化为了最小割问题。
#include<bits/stdc++.h>
#define INF LLONG_MAX/2
#define N 505
using namespace std; struct ss
{
int v,next;
long long flow;
};
int head[N],now_edge=,S,T;
ss edg[N*]; void init()
{
now_edge=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,long long flow)
{
edg[now_edge]=(ss){v,head[u],flow};
head[u]=now_edge++;
edg[now_edge]=(ss){u,head[v],flow};
head[v]=now_edge++;
} int dis[N]; int bfs()
{
memset(dis,,sizeof(dis));
queue<int>q;
q.push(S);
dis[S]=; while(!q.empty())
{
int now=q.front();
q.pop(); for(int i=head[now];i!=-;i=edg[i].next)
{
ss &e=edg[i];
if(e.flow>&&dis[e.v]==)
{
dis[e.v]=dis[now]+;
q.push(e.v);
}
}
} if(dis[T]==)return ;
return ;
} int current[N];
long long dfs(int x,long long maxflow)
{
if(x==T)return maxflow;
for(int i=current[x];i!=-;i=edg[i].next)
{
current[x]=i; ss &e=edg[i];
if(e.flow>&&dis[e.v]==dis[x]+)
{
long long flow=dfs(e.v,min(maxflow,e.flow)); if(flow!=)
{
e.flow-=flow;
edg[i^].flow+=flow;
return flow;
}
}
}
return ;
} long long dinic()
{
long long ans=,flow; while(bfs())
{
for(int i=;i<N;i++)current[i]=head[i];
while(flow=dfs(S,INF))ans+=flow;
}
return ans;
} int from[N],to[N],w[N]; int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&from[i],&to[i],&w[i]);
} int ans=;
for(int i=;i<=m;i++)
{
init();
for(int j=;j<=m;j++)
if(w[j]<w[i])addedge(from[j],to[j],); S=from[i];
T=to[i];
ans+=dinic();
}
printf("%d\n",ans);
return ;
}

随机推荐

  1. java从键盘输入学生成绩,找出最高分,并输出学生成绩等级。

    /*从键盘输入学生成绩,找出最高分,并输出学生成绩等级:成绩 >=最高分-10 等级为A成绩 >=最高分-20 等级为B成绩 >=最高分-30 等级为C其余为 等级为D 提示:先输入 ...

  2. 高性能可扩展MySQL数据库设计及架构优化 电商项目(慕课)第3章 MySQL执行计划(explain)分析

    ID:相同就从上而下,不同数字越大越优先

  3. es6中的promise解读

    目录 什么是promise? promise的优点 回调地狱问题  Promise的三种状态 一个简单的promise promise中的then 利用promise解决回调地狱 promise的链式 ...

  4. 无屏幕和键盘配置树莓派WiFi和SSH

    原文转载:http://shumeipai.nxez.com/2017/09/13/raspberry-pi-network-configuration-before-boot.html 不算是什么新 ...

  5. LeetCode946-验证栈序列

    问题:验证栈序列 给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 ...

  6. ZendFramework-2.4 源代码 - ViewManager类图

  7. win7在某个盘或文件夹中出现右键只能新建文件夹的情况 (2012-12-28-bd 写的日志迁移

    至于只能新建文件夹的情况如图: 解决方法是在运行中输入msconfig进入如图: 在系统设置选工具项在选中更改UAC设置点击启动如图: 如图: 直接把通知栏拉到最低确定即可(如果已经是最低了那就随便改 ...

  8. 18.VUE学习之-v-for操作对象与数值

    一组数组时的循环 二组数组时的循环 另外可以v for 20 可以直接操作数字 <!DOCTYPE html> <html lang="en"> <h ...

  9. 分享 php array_column 函数 无法在低版本支持的 修改

    function i_array_column($input, $columnKey, $indexKey=null){ if(!function_exists('array_column')){ $ ...

  10. spring boot 设置tomcat post参数限制

    今天传图片,用的base64字符串,POST方法,前端传送的时候总是莫名其妙的崩溃,去网上搜了半天,以为是文件大小被限制了,但是我这个是字符串接收,不是文件接收,于是又继续搜,原来post本身没有参数 ...