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. Doc常用命令

    1. 获取目录: dir 2. 清屏: cls

  2. Maven -- 在进行war打包时用正式环境的配置覆盖开发环境的配置

    我们的配置文件一般都放在  src/main/resource 目录下. 假定我们的正式环境配置放在 src/main/online-resource 目录下. 那么打成war包时,我们希望用onli ...

  3. SVG 基本绘图方法总结

    基本内容:   * SVG并不属于HTML5专有内容    * HTML5提供有关SVG原生的内容   * 在HTML5出现之前,就有SVG内容   * SVG,简单来说就是矢量图   * SVG文件 ...

  4. Linux powercli 以及connect-viserver 连接问题

    1. 参考文档 http://fdo-workspace.blogspot.hk/2017/07/powershell-powercli-for-linux-server.html 2. powers ...

  5. 「6月雅礼集训 2017 Day11」delight

    [题目大意] 有$n$天,每天能吃饭.睡觉.什么事也不干 每天吃饭的愉悦值为$e_i$,睡觉的愉悦值为$s_i$,什么都不干愉悦值为0. 要求每连续$k$天都要有至少$E$天吃饭,$S$天睡觉. 求最 ...

  6. HDU5852 Intersection is not allowed!

    There are K pieces on the chessboard. The size of the chessboard is N*N. The pieces are initially pl ...

  7. 【LA】5135 Mining Your Own Business

    [算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...

  8. 简述--构建React项目的几种方式

    前言: 构建React项目的几种方式: 构建:create-react-app 快速脚手架 构建:generator-react-webpack 构建:webpack一步一步构建 1)构建:creat ...

  9. hdu 2795 Billboard(线段树+单点更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others ...

  10. linux编程之消息队列

    消息队列是内核地址空间中的内部链表,通过linux内核在各个进程之间传递内容,消息顺序地发送到消息队列中,并且以几种不同的方式 从队列中获取,每个消息队列可以用IPC标识符唯一的进行标识,内核中的消息 ...