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. 动态规划:区间DP与环形DP

    区间型动态规划的典型例题是石子归并,同时使用记忆化搜索实现区间动归是一种比较容易实现的方式,避免了循环数组实现的时候一些边界的判断 n堆石子排列成一条线,我们可以将相邻的两堆石子进行合并,合并之后需要 ...

  2. II8部署WCF服务出错

    环境:Windows 2012 R2 + IIS 8.0 + .NET 4.5 错误404.3 - Not Found: 控制面板->程序->启用或关闭Windows功能,如下图所示,将需 ...

  3. C11简洁之道:初始化改进

    1.  C++98/03初始化 我们先来总结一下C++98/03的各种不同的初始化情况: //普通数组 ] = {, , }; //POD(plain old data) struct A { int ...

  4. 汕头市队赛 SRM19 字符题

    从天上掉下来了个这样的问题: 有一个字符串 从中选出两个子串 A,B,求 A+B可以构成的不同串的个数. 还想知道,这么多个串中字典序最大的那一个. 某人捡到了这个问题,并把它扔给了你. [输入] 一 ...

  5. 【51NOD-0】1137 矩阵乘法

    [算法]简单数学 [题解] 对于A*B=C C中第i行第j列的数字由A中第i行和B中的j列的数字各自相乘后相加得到. 所以两个矩阵能相乘要求A的列数等于B的行数,复杂度为O(n3). #include ...

  6. Spring boot 集成Dubbox(山东数漫江湖)

    前言 因为工作原因,需要在项目中集成dubbo,所以去查询dubbo相关文档,发现dubbo目前已经不更新了,所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,dub ...

  7. HDU 1205 吃糖果 (数学)

    题目链接 Problem Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃 ...

  8. bzoj 1968 数学

    在1-n所有数中,i一共可以成为n/i个数的约数,也即所有的约数中有n/i个i,所以扫一遍累加答案就好了. /********************************************* ...

  9. js_跑马灯

    跑马灯?刚听到这个词的时候,脑袋第一个想到的是跑马?嗯?就是香港的那种跑马场.懂?其次就是霓虹灯了,一闪一闪的多好看. 霓虹灯?哦,那是城市的杰作,记忆中是.开往城市边缘开,把车窗都摇下来,用速度换一 ...

  10. 【tomcat】手动部署动态JavaWeb项目到tomcat

    1.通过修改server.xml进行配置 1.查看项目的目录结构: tomcat运行时加载WebConmtent目录