Roads

时间限制: 2 Sec  内存限制: 256 MB
提交: 6  解决: 2
[提交][状态][讨论版]

题目描述

In ICPCCamp, there are n towns conveniently labeled with 1,2,…,n and m bidirectional roads planned to be built. The i-th road will be built between cities ai and bi with cost ci. The builders in ICPCCamp will build the (n−1) roads with the least total cost to connect any of two cities directly or indirectly.

Bobo, the mayor of ICPCCamp is going to remove some of the roads from the construction plan. He would like to know the minimum number roads to be removed to strictly increases the total cost.

Note that the total cost is considered as +∞ if no valid (n−1) roads exist after removing. It is also counted as "total cost strictly increases".

输入

The input contains zero or more test cases and is terminated by end-of-file. For each test case:
The first line contains two integers n and m. The i-th of the following m lines contains ai,bi,ci.
2≤n≤50,n−1≤m≤n2,1≤ai,bi≤n,1≤ci≤109
Any two cities will be connected if all m roads are built.
The sum of n does not exceed 103.

输出

For each case, output an integer which denotes the result.

样例输入

3 3
1 2 1
1 3 2
2 3 3
3 4
1 2 1
1 2 1
1 3 2
1 3 3
3 4
1 2 1
1 2 1
1 3 2
1 3 2
4 6
1 2 1
1 3 1
1 4 1
2 3 1
2 4 1
3 4 1

样例输出

1
1
2
3
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e2+;;
const int M = ;
const int mod = ;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
ll qpow(int x,int qq){ll f=,p=x;while(qq){if(qq&)f=f*p%mod;p=1LL*p*p%mod;qq>>=;}}
int n,m;
int fa[N],wage[N],edg[N][N];
bool vis[N],in_set[N];
struct man{
int u,v,w;
bool operator <(const man &s)const {
return w<s.w;
}
}a[N*N];
int findFa(int x){
return fa[x]==x?x:fa[x]=findFa(fa[x]);
}
void unionFa(int x,int y){
if(findFa(x)==findFa(y))return ;
fa[findFa(x)]=findFa(y);
}
int search(int &s,int &t){
met(vis,false);
met(wage,);
int u,mincut,mx;
for(int i=;i<=n;i++){
u=mx=-;
for(int j=;j<=n;j++){
if(!in_set[j]&&!vis[j]&&wage[j]>mx)mx=wage[u=j];
}
if(u==-)return mincut;
vis[u]=true;
s=t,t=u;
mincut=mx;
for(int j=;j<=n;j++){
if(!in_set[j]&&!vis[j])wage[j]+=edg[u][j];
}
}
return mincut;
}
int stoer_wagner(){
int mincut=inf,s,t,ret;
for(int i=;i<n;i++){
ret=search(s,t);
in_set[t]=true;
if(ret!=&&ret<mincut)mincut=ret;//加ret!=0是为了求所有联通块的最小割
for(int j=;j<=n;j++)if(!in_set[j])edg[s][j]=(edg[j][s]+=edg[j][t]);
}
return mincut;
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<m;i++){
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
sort(a,a+m);
int ans=inf,cnt=;
for(int i=;i<m;){
int now=i,w=a[i].w;
met(edg,);met(in_set,false);
for(;a[i].w==w&&i<m;i++){
if(findFa(a[i].u)!=findFa(a[i].v)){
edg[findFa(a[i].u)][findFa(a[i].v)]++;
edg[findFa(a[i].v)][findFa(a[i].u)]++;
}
}
ans=min(ans,stoer_wagner());
for(int i=now;a[i].w==w&&i<m;i++){
if(findFa(a[i].u)!=findFa(a[i].v)){
unionFa(a[i].u,a[i].v);
cnt++;
}
}
if(cnt>=n-)break;
}
printf("%d\n",ans);
}
return ;
}
 

2017江苏省省赛 Roads(全局最小割)的更多相关文章

  1. HDU 3691 Nubulsa Expo(全局最小割Stoer-Wagner算法)

    Problem Description You may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa ...

  2. HDU 3691 Nubulsa Expo(全局最小割)

    Problem DescriptionYou may not hear about Nubulsa, an island country on the Pacific Ocean. Nubulsa i ...

  3. UVALive 5099 Nubulsa Expo 全局最小割问题

    B - Nubulsa Expo Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  4. ZOJ 2753 Min Cut (Destroy Trade Net)(无向图全局最小割)

    题目大意 给一个无向图,包含 N 个点和 M 条边,问最少删掉多少条边使得图分为不连通的两个部分,图中有重边 数据范围:2<=N<=500, 0<=M<=N*(N-1)/2 做 ...

  5. 全局最小割Stoer-Wagner算法

    借鉴:http://blog.kongfy.com/2015/02/kargermincut/ 提到无向图的最小割问题,首先想到的就是Ford-Fulkerson算法解s-t最小割,通过Edmonds ...

  6. HDU 6081 度度熊的王国战略(全局最小割堆优化)

    Problem Description度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族.哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士.所以这一场战争,将会十分艰难.为了更好的进攻 ...

  7. HDU 6081 度度熊的王国战略(全局最小割Stoer-Wagner算法)

    Problem Description 度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族. 哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士. 所以这一场战争,将会十分艰难. 为了更 ...

  8. 全局最小割StoerWagner算法详解

    前言 StoerWagner算法是一个找出无向图全局最小割的算法,本文需要读者有一定的图论基础. 本文大部分内容与词汇来自参考文献(英文,需***),用兴趣的可以去读一下文献. 概念 无向图的割:有无 ...

  9. 求全局最小割(SW算法)

    hdu3002 King of Destruction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. jquery checkbox选中状态以及实现全选反选

    jquery1.6以下版本获取checkbox的选中状态: $('.ck').attr('checked'); $('.ck').attr('checked',true);//全选 $('.ck'). ...

  2. centos6.8配置FTP普通用户除了家目录外还能访问其他目录

    今天有个需求,使用ftp服务搭建一个文件共享服务器,每个普通用户除了能访问自己家目录的东西,还能访问一个公共的目录.配置步骤如下: 环境如下: 创建用户并配置密码(使用默认家目录/home) user ...

  3. Moq 和 RhinoMocks

    我们在做单元测试的时候,利用mock可轻松构建出测试需要的类或接口,而不需要编写繁琐的测试代码. .net 下我知道Moq与Rhino Mocks这两个框架. Moq 网上介绍的比较多. Rhino ...

  4. .NET RabbitMQ

    在企业应用系统领域,会面对不同系统之间的通信.集成与整合,尤其当面临异构系统时,这 种分布式的调用与通信变得越发重要.其次,系统中一般会有很多对实时性要求不高的但是执行起来比较较耗时的地方,比如发送短 ...

  5. javascript「篱式」条件判断

    我们已经知道,null 没有任何的属性值,并且无法获取其实体(existence)值.所以 null.property 返回的是错误(error)而不是 undefined . 考虑下面的代码 if ...

  6. Windows/Linux javac/java编译运行引入所需的jar包

    > Windows 假设要引用的jar放在D:/test目录下,名字为t1.jar, java源文件放在D:/test/src目录下,名字为t2.java. 编译: javac  -cp  d: ...

  7. 崩坏3mmd中的渲染技术研究

    http://youxiputao.com/articles/11839 主要是参考该篇文章做一个微小的复盘. 漫反射与高光 文章中的漫反射与高光并不是类似于普通的 resultCol = Diffu ...

  8. HDU - 5327 Olympiad(一维前缀和)

    Olympiad Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  9. javaWeb面试题(重要)

    1.Javaweb 技术的结构  1.1 技术结构图

  10. linux下终端游戏

    sl 一列火车 oneko 一只淘气的小猫