HDU - 3002 King of Destruction(最小割)

http://acm.hdu.edu.cn/showproblem.php?pid=3002

 

最小割模板

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)

using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1000000007;
int T;
void testcase(){
    printf("Case #%d: ",++T);
}
const int MAXN = 2500;
const int MAXM = 30;
int maz[MAXN][MAXN];
int v[MAXN],dist[MAXN];
bool visit[MAXN];
int n,m;
int StoerWagner(int n){
    int cut = inf;
    for(int i=0;i<n;i++) v[i]=i;
    while(n>1){
        int k=1,pre=0;
        for(int i=1;i<n;++i){
            dist[v[i]]=maz[v[0]][v[i]];
            if(dist[v[i]]>dist[v[k]]) k=i;
        }
        mset(visit,false);
        visit[v[0]]=true;
        for(int i=1;i<n;i++){
            if(i==n-1){
                cut=min(cut,dist[v[k]]);
                for(int j=0;j<n;j++){
                    maz[v[pre]][v[j]] += maz[v[j]][v[k]];
                    maz[v[j]][v[pre]] += maz[v[j]][v[k]];
                }
                v[k]=v[--n];
            }
            visit[v[k]]=true;
            pre = k,k=-1;
            for(int j=1;j<n;j++){
                if(!visit[v[j]]){
                    dist[v[j]] += maz[v[pre]][v[j]];
                    if(k==-1 || dist[v[k]]<dist[v[j]]) k=j;
                }
            }
        }
    }
    return cut;
}

int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
    int n,m;
    while(~scdd(n,m)){
        mset(maz,0);
        int x,y,w;
        for(int i=0;i<m;i++){
            scddd(x,y,w);
            maz[x][y]+=w;
            maz[y][x]+=w;
        }
        cout<<StoerWagner(n)<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/fht-litost/p/9281892.html

时间: 2024-11-06 09:44:12

HDU - 3002 King of Destruction(最小割)的相关文章

HDU 4289 Control (网络流-最小割)

Control Problem Description You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their

HDU 6214 Smallest Minimum Cut 最小割,权值编码

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6214 题意:求边数最小的割. 解法: 建边的时候每条边权 w = w * (E + 1) + 1; 这样得到最大流 maxflow / (E + 1) ,最少割边数 maxflow % (E + 1) 道理很简单,如果原先两类割边都是最小割,那么求出的最大流相等 但边权变换后只有边数小的才是最小割了 乘(E+1)是为了保证边数叠加后依然是余数,不至于影响求最小割的结果 因为假设最小割=k,那么现在新

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

HDU 4289:Control(最小割)

http://acm.hdu.edu.cn/showproblem.php?pid=4289 题意:有n个城市,m条无向边,小偷要从s点开始逃到d点,在每个城市安放监控的花费是sa[i],问最小花费可以监控到所有小偷. 思路:求最小割可以转化为最大流.每个城市之间拆点,流量是sa[i],再增加一个超级源点S和s相连,增加一个超级汇点T,让d的第二个点和T相连.然后就可以做了. 1 #include <cstdio> 2 #include <algorithm> 3 #include

HDU 3452 Bonsai(网络流之最小割)

题目地址:HDU 3452 最小割水题. 源点为根节点.再另设一汇点,汇点与叶子连边. 对叶子结点的推断是看度数是否为1. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include &l

HDU 3251 Being a Hero 最小割

Problem Description You are the hero who saved your country. As promised, the king will give you some cities of the country, and you can choose which ones to own! But don't get too excited. The cities you take should NOT be reachable from the capital

HDU 3526 Computer Assembling(最小割)

http://acm.hdu.edu.cn/showproblem.php?pid=3526 题意:有个屌丝要配置电脑,现在有n个配件需要购买,有两家公司出售这n个配件,还有m个条件是如果配件x和配件y在不同公司买的话,需要花费额外的w元.现在需要计算购买这n个配件的最小花费. 思路: 一开始想的费用流,但好像不太行?? 其实一看到二选一的话就首先应该往最小割和二分图这个方向去想一想的. 这题用最小割来做,对于这m条件,在这两个顶点之间加两条有向边. 1 #include<iostream>

HDU 3657 Game(取数 最小割)经典

Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1065    Accepted Submission(s): 449 Problem Description onmylove has invented a game on n × m grids. There is one positive integer on each g

HDU 5294(Tricks Device-最短路最小割)[Template:SPFA]

Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2389    Accepted Submission(s): 635 Problem Description Innocent Wu follows Dumb Zhang into a ancient tomb. Innocent Wu's at the en