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如何将十六进制数转换为十进制数的自编程序

    package com.swift;//所属包 import java.util.Scanner;//导入扫描器 public class Hex2Decimal { public static vo ...

  2. vue-cli npm run build 打包问题 webpack@3.6

    1, vue-router 路由 有两个模式 (mode) hash (默认模式) 使用URL来模拟一个完整的URL 但是没个URL都会带上 "#/'' 支持所有浏览器 这个模式使用 red ...

  3. Arguments Optional-freecodecamp算法题目

    Arguments Optional 1.要求 创建一个计算两个参数之和的 function.如果只有一个参数,则返回一个 function,该 function 请求一个参数然后返回求和的结果. 如 ...

  4. 认识mysql(3)

    认识mysql第三篇,发出的内容适合初学者,如果能持续关注我的博客,可以全面的掌握mysql的常用知识,后续我也会陆续发出python相关的知识,关注我,和我一共进步吧! 1.SQL查询 1.执行顺序 ...

  5. HTML5/CSS3 第三章页面布局

    页面布局 1 页面组成 2 布局相关的标签 <div></div> 定义文档中的分区或节 <span></span> 这是一个行内元素,没有任何意义 & ...

  6. .net core 获取浏览器UserAgent

    这两天由于自己公司的机器磁盘不够用了,果断把VS2015卸载了,只留下VS2017 当我打开一个以前一个很简单的MVC4.0的项目时候 温馨提示要安装MVC4,我犹豫了一下,还是点了安装,接下来提示要 ...

  7. U盘装系统之winpe中常用安装win7的方法和备份(2013-01-15-bd 写的日志迁移

    首先到网上去下一个制作U盘启动的的软件比如老毛桃.大白菜.电脑城制作u盘启动软件[其实他们的装机界面和工具那些都差不多], 我是用的老毛桃至于制作流程你可以看它的视频你往下拉就可以看见,或者看说明,自 ...

  8. Java集合框架汇总

    HashMap是一个最常用的Map,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的.HashMap最多只允许一条记录的键为NUL ...

  9. 常见/dev/mapper/centos-root扩容

    系统Centos 7 df -h 查看当前分区使用情况: dfisk /dev/xvda 对/dev/xvda磁盘进行操作(新建分区及格式化) n p 回车 默认分区号: 回车 默认磁盘创建开始位置: ...

  10. excel VBA 将文本数值转换为数字格式(单元格中数据左上角是绿三角,鼠标点上有叹号标示)

    Range("A6").SelectSelection.CopyRange("A10:A60").SelectRange(Selection, Selectio ...