题意:https://blog.csdn.net/Ratina/article/details/95200594

思路:

首先我们知道最小生成树就是按长度枚举边,能连就连。

那么,如果这条边在最小生成树里,那我们只需要看比它短的边是不是已经使当前的u---v连通,如果连通最少需要切掉几条(边权为1跑最小割)。

所以我们对边排序,枚举边+重构图跑Dinic就行了。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
int abss(int a);
int lowbit(int n);
int Del_bit_1(int n);
int maxx(int a,int b);
int minn(int a,int b);
double fabss(double a);
void swapp(int &a,int &b);
clock_t __STRAT,__END;
double __TOTALTIME;
void _MS(){__STRAT=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);
//const ll INF=(1LL<<60);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e3+;
const int M=(int)5e3+; class DINIC
{
public:
// const int MAXN=10004,MAXWAY=100005;
int n,way,max_flow,deep[N];
int tot,head[N],cur[N];
struct EDGE{
int to,next;
int dis;
}edge[M];
void Init(int n_)
{
tot=-;//因为加反向边要^1,所以要从0开始;
n=n_;
max_flow=;
for(int i=;i<=n_;++i)
head[i]=-;
}
void add(int from,int to,int V)
{
//正向
++tot;
edge[tot].to=to;
edge[tot].dis=V;
edge[tot].next=head[from];
head[from]=tot;
//反向
swap(from,to);
++tot;
edge[tot].to=to;
edge[tot].dis=V;
edge[tot].next=head[from];
head[from]=tot;
}
queue<int>q;
bool bfs(int s,int t)
{
for(int i=;i<=n;++i)
deep[i]=inf;
while(!q.empty())q.pop();
for(int i=;i<=n;++i)cur[i]=head[i];
deep[s]=;
q.push(s); while(!q.empty())
{
int now=q.front();q.pop();
for(int i=head[now];i!=-;i=edge[i].next)
{
if(deep[edge[i].to]==inf&&edge[i].dis)
{
deep[edge[i].to]=deep[now]+;
q.push(edge[i].to);
}
}
}
return deep[t]<inf;
}
int dfs(int now,int t,int limit)
{
if(!limit||now==t)return limit;
int flow=,f;
for(int i=cur[now];i!=-;i=edge[i].next)
{
cur[now]=i;
if(deep[edge[i].to]==deep[now]+&&(f=dfs(edge[i].to,t,min(limit,edge[i].dis))))
{
flow+=f;
limit-=f;
edge[i].dis-=f;
edge[i^].dis+=f;
if(!limit)break;
}
}
return flow;
}
void Dinic(int s,int t)
{
while(bfs(s,t))
max_flow+=dfs(s,t,inf);
}
}G;
struct EDGE
{
int u,v;
int val;
friend bool operator<(EDGE a,EDGE b)
{
return a.val<b.val;
}
}edge[M]; int main()
{
int n,m;
sc("%d%d",&n,&m);
for(int i=;i<=m;++i)
sc("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].val);
sort(edge+,edge++m);
int ans=;
for(int i=;i<=m;++i)
{
G.Init(n);
for(int j=;j<i;++j)
{
if(edge[j].val<edge[i].val)
G.add(edge[j].u,edge[j].v,);
}
G.Dinic(edge[i].u,edge[i].v);
ans+=G.max_flow;
}
pr("%d\n",ans);
return ;
} /**************************************************************************************/ int maxx(int a,int b)
{
return a>b?a:b;
} void swapp(int &a,int &b)
{
a^=b^=a^=b;
} int lowbit(int n)
{
return n&(-n);
} int Del_bit_1(int n)
{
return n&(n-);
} int abss(int a)
{
return a>?a:-a;
} double fabss(double a)
{
return a>?a:-a;
} int minn(int a,int b)
{
return a<b?a:b;
}

网络流+最小生成树的最少割边数--How Many to Be Happy?的更多相关文章

  1. hdu3987,最小割时求最少割边数

    题意:求最小割时候割边最少的数量.算法:先求dinic一遍,跑出残网络,再把该网络中满流量(残量为0)的边 残量改为1,其他边残量改为无穷,则再跑一次最大流,所得即为答案.(思,最小割有喝多组,但是要 ...

  2. HDU 6214.Smallest Minimum Cut 最少边数最小割

    Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Oth ...

  3. hdu3987 最小割边数

    题意:      是让你求最小割之后问最小割的最少边数是多少,因为最小割不是唯一的,所以存在最小边数的问法.思路:      两个方法,一个是先一遍最大流,然后把割边全都改成流量1,其他的全都改成流量 ...

  4. 算法之Python实现 - 002 : 换钱的最少货币数补充(每种货币只能使用一次)

    [题目]:给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币仅可以使用一张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数. [代码1]:时间与额 ...

  5. 算法之Python实现 - 001 : 换钱的最少货币数

    [题目]给定数组arr,arr中所有的值都为正数且不重复.每个值代表一种面值的货币,每种面值的货币可以使用任意张,再给定一个整数aim代表要找的钱数,求组成aim的最少货币数. [代码1]:时间与额外 ...

  6. Leetcode. 回文字符串的分割和最少分割数

    Q1: 回文字符串的分割 Given a string s, partition s such that every substring of the partition is a palindrom ...

  7. AC日记——[网络流24题]方格取数问题 cogs 734

    734. [网络流24题] 方格取数问题 ★★☆   输入文件:grid.in   输出文件:grid.out   简单对比时间限制:1 s   内存限制:128 MB «问题描述: 在一个有m*n ...

  8. OptimalSolution(1)--递归和动态规划(2)矩阵的最小路径和与换钱的最少货币数问题

    一.矩阵的最小路径和 1 3 5 9 1 4 9 18 1 4 9 18 8 1 3 4 9 9 5 8 12 5 0 6 1 14 14 5 11 12 8 8 4 0 22 22 13 15 12 ...

  9. 最少硬币数——Java

    问题:有n种硬币,面值分别为v1,v2,v3,…,vn,存于数组T[1:n]中,可以使用的各种面值的硬币个数存于数组Coins[1:n]中.对任意钱数0≤m≤20001,设计一个用最少硬币找钱m的方法 ...

随机推荐

  1. 一步一步配置AWS ELB Https证书

    第一步:生成CSR 要配置证书,我们首先需要创建一个CSR来向证书提供商申请证书.这个过程我们可以通过IIS中的工具来生成. 然后需要填写如下信息: 下一步后选择文件名后我们就可以创建出CSR 文件了 ...

  2. C# 窗体 类似framest 左侧点击右侧显示 左侧菜单右侧显示

    首先托一个splitContainer调节大小位置 然后进行再新创建一个窗体名为add 在左侧拖入button按钮 进入代码阶段 更改属性 public Main() { InitializeComp ...

  3. LinkedBlockingQueue和ArrayBlockingQueue的异同

    相同: 1.LinkedBlockingQueue和ArrayBlockingQueue都实现了BlockingQueue接口: 2.LinkedBlockingQueue和ArrayBlocking ...

  4. 修改ssh连上默认目录

    vi /etc/passwd ssh连接linux服务器只显示-bash-4.1#不显示路径时,我们只需要修改  ~/.bash_profile文件,如果不存在这个文件,那么新建一个,增加内容  ex ...

  5. UNIX 历史问题 分布式系统的Thundering Herd效应 惊群效应

    https://uwsgi-docs.readthedocs.io/en/latest/articles/SerializingAccept.html One of the historical pr ...

  6. Winform中利用委托实现窗体之间的传值

    点击打开按扭,打开传输值窗体 public partial class Form1 : Form { public Form1() { InitializeComponent(); } public ...

  7. 前端 - 轮询, 长轮训, websocket

    轮询 概念 ajax 轮询的原理很简单,让浏览器间隔几秒就发送一次请求,询问服务器是否有新信息 代码 function showUnreadNews() { $(document).ready(fun ...

  8. Qt编写安防视频监控系统6-面板开关

    一.前言 面板开关功能是整个系统最人性化的功能之一,可以对主界面中左侧右侧的各个小面板进行显示和隐藏,当隐藏的时候,另外的同级面板自动拉伸填充,这样就不会显得空洞,直接在每个面板的右上角提供了关闭按钮 ...

  9. [Scikit-learn] 1.1 Generalized Linear Models - Neural network models

    本章涉及到的若干知识点(红字):本章节是作为通往Tensorflow的前奏! 链接:https://www.zhihu.com/question/27823925/answer/38460833 首先 ...

  10. Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.5:test

    解决方法: 打包跳过测试有两种方法 一是命令行 mvn clean package -Dmaven.test.skip=true 二是写入pom文件 <plugin> <groupI ...