题目链接

Problem Description

Consider a network G=(V,E)G=(V,E)G=(V,E) with source sss and sink t" role="presentation">ttt. An s-t cut is a partition of nodes set VVV into two parts such that s" role="presentation">sss and ttt belong to different parts. The cut set is the subset of E" role="presentation">EEE with all edges connecting nodes in different parts. A minimum cut is the one whose cut set has the minimum summation of capacities. The size of a cut is the number of edges in the cut set. Please calculate the smallest size of all minimum cuts.

Input

The input contains several test cases and the first line is the total number of cases T (1≤T≤300)T(1≤T≤300)T~(1\le T\le 300).Each case describes a network GGG, and the first line contains two integers n (2≤n≤200)" role="presentation">n (2≤n≤200)n (2≤n≤200)n~(2\le n\le 200) and m (0≤m≤1000)m(0≤m≤1000)m~(0\le m\le 1000) indicating the sizes of nodes and edges. All nodes in the network are labelled from 111 to n" role="presentation">nnn.The second line contains two different integers sss and t (1≤s,t≤n)" role="presentation">t (1≤s,t≤n)t (1≤s,t≤n)t~(1\le s,t\le n) corresponding to the source and sink.Each of the next mmm lines contains three integers u,v" role="presentation">u,vu,vu,v and w (1≤w≤255)w(1≤w≤255)w~(1\le w\le 255) describing a directed edge from node uuu to v" role="presentation">vvv with capacity ww<script type="math/tex" id="MathJax-Element-21">w</script>.

Output

For each test case, output the smallest size of all minimum cuts in a line.

Sample Input

2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 2
4 5
1 4
1 2 3
1 3 1
2 3 1
2 4 1
3 4 3

Sample Output


2
3

AC

  • 方法一:先跑一次最大流,将满流边的流量加1反向流量不变,不满流的边流量变为inf,反向流量不变,然后再跑一次最大流
  • 方法二:建图的时候把流量C, 改为C * (M + 1) + 1;
    然后跑一个最大流,最大流 = maxflow / (M + 1) 最小割边 = maxflow % (M + 1)
// 方法一
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 205
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
struct ac{int v, c, pre;
}edge[8000001];
int head[N], dis[N], curedge[N], cnt;
int inf = 0x3f3f3f3f;
void addedge(int u, int v, int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool bfs(int s, int e) {queue<int> que;que.push(s);memset(dis, 0, sizeof(dis));dis[s] = 1;while (!que.empty()) {int t = que.front();que.pop();for (int i = head[t]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] || edge[i].c == 0)   continue;dis[edge[i].v] = dis[t] + 1;que.push(edge[i].v);} }return dis[e] != 0;
}
int dfs(int s, int e, int flow) {if (s == e) return flow;for (int &i = curedge[s]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {int d = dfs(edge[i].v, e, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i ^ 1].c += d;return d; }}}return 0;
}
int n, d, m;
int solve(int s, int e) {int sum = 0;while (bfs(s, e)) {for (int i = 1; i < N ; ++i) {curedge[i] = head[i];}int d;while (d = dfs(s, e, inf)) {sum += d;}}return sum;
}
int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);while (t--) {memset(head, -1, sizeof(head));cnt = 0;int n, m, s, e;scanf("%d%d%d%d", &n, &m, &s, &e);for (int i = 0; i < m; ++i) {int x, y, c;scanf("%d%d%d", &x, &y, &c);addedge(x, y, c);}solve(s, e);for (int i = 0; i < cnt; i += 2) {if (edge[i].c == 0) edge[i].c = 1;else    edge[i].c = inf;}int ans = solve(s, e);printf("%d\n", ans);} return 0;
}
// 方法二
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 205
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;
struct ac{int v, c, pre;
}edge[8000001];
int head[N], dis[N], curedge[N], cnt;
int inf = 0x3f3f3f3f;
void addedge(int u, int v, int c) {edge[cnt].v = v;edge[cnt].c = c;edge[cnt].pre = head[u];head[u] = cnt++;swap(u, v);edge[cnt].v = v;edge[cnt].c = 0;edge[cnt].pre = head[u];head[u] = cnt++;
}
bool bfs(int s, int e) {queue<int> que;que.push(s);memset(dis, 0, sizeof(dis));dis[s] = 1;while (!que.empty()) {int t = que.front();que.pop();for (int i = head[t]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] || edge[i].c == 0)   continue;dis[edge[i].v] = dis[t] + 1;que.push(edge[i].v);} }return dis[e] != 0;
}
int dfs(int s, int e, int flow) {if (s == e) return flow;for (int &i = curedge[s]; i != -1; i = edge[i].pre) {if (dis[edge[i].v] == dis[s] + 1 && edge[i].c) {int d = dfs(edge[i].v, e, min(flow, edge[i].c));if (d > 0) {edge[i].c -= d;edge[i ^ 1].c += d;return d; }}}return 0;
}
int n, d, m;
int solve(int s, int e) {int sum = 0;while (bfs(s, e)) {for (int i = 1; i < N ; ++i) {curedge[i] = head[i];}int d;while (d = dfs(s, e, inf)) {sum += d;}}return sum;
}
int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);while (t--) {memset(head, -1, sizeof(head));cnt = 0;int n, m, s, e;scanf("%d%d%d%d", &n, &m, &s, &e);for (int i = 0; i < m; ++i) {int x, y, c;scanf("%d%d%d", &x, &y, &c);addedge(x, y, c * (m + 1) + 1);}int ans = solve(s, e) % (m + 1);printf("%d\n", ans);} return 0;
}

HDU Problem - 6214 Smallest Minimum Cut(最小割边,两种方法)相关推荐

  1. HDU - 6214 Smallest Minimum Cut(最小割最少边数)

    题目链接:点击查看 题目大意:给出一张由n个点以及m条边组成的有向图,现在要求出最少割掉几条边使得整张图不连通并且割掉边的权值最小 题目分析:题目的意思也就是要求最小割的最少边数,这里有两个方法: 先 ...

  2. 牛客 Tree(最小深度总和)(两种方法求重心)难度⭐⭐⭐

    题目链接 牛妹有一张连通图,由n个点和n-1条边构成,也就是说这是一棵树,牛妹可以任意选择一个点为根,根的深度deprootdep_{root}deproot​​为0,对于任意一个非根的点,我们将他到 ...

  3. HDU 3555 Bomb(数位DP模板啊两种形式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...

  4. python创建树结构、求深度_Python实现二叉树的最小深度的两种方法

    找到给定二叉树的最小深度 最小深度是从根节点到最近叶子节点的最短路径上的节点数量 注意:叶子节点没有子树 Example: Given binary tree [3,9,20,null,null,15 ...

  5. 【LA3487】最小割-经典模型 两种方法

    题目链接 题意:A.B两个公司要买一些资源(他们自己买的资源不会重复),一个资源只能卖给一个公司.问最大收益. simple input 部分: 54 1 //买到1就给54元 15 2 33 3 2 ...

  6. 判断一个数是否为素数的两种方法:质数又称素数,有无限个。一个大于1的自然数,除了1和它本身外,不能被其他自然数(质数)整除,换句话说就是该数除了1和它本身以外不再有其他的因。最小的质数是2。

    一,由键盘输入一个数判断是否为素数(设一个数存放变量,将为0的代表非素数,为1的代表为素数) #include <stdio.h> int main() { int i,flag,numb ...

  7. HDU Problem 1272 小希的迷宫 【并查集】

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. HDU Problem - 5938 Four Operations

    题目链接 Problem Description Little Ruins is a studious boy, recently he learned the four operations!Now ...

  9. hdu 1233 还是畅通工程(最小生成树的Prim和Kruskal两种算法的c++实现)(prim算法详解)...

    赤裸裸滴最小生成树(MST),刚学的玩意,用两种方法熟练一下.(都是greedy) Kruskal方法:先对边按照代价非递减排序,再不断添加边且不产生环路,当边数=点数-1结束.判断加入(v,w)是否 ...

最新文章

  1. while循环里嵌套一个if_if-else嵌套太深?教你一个新手都能掌握的设计模式搞定!...
  2. 1-微信小程序开发(安装软件和运行第一个微信小程序)
  3. alsa和oss声音系统比较
  4. 奇安信代码安全实验室帮助微软修复高危漏洞,获官方致谢
  5. python的while分支
  6. Anaconda 安装教程(Win10环境) Tensorflow安装
  7. 第四周课程总结&实验报告。
  8. 为什么vi用HJKL和ESC
  9. H5调用app原生接口
  10. 计算机英语期末论文格式,计算机英文论文大纲格式 计算机英文论文大纲如何写...
  11. PyTorch之—卷积层、激活层、BN
  12. 在python中逻辑量有_(五)我的魔法竟有了一丝逻辑
  13. 计算机如何区分程序和数据,计算机如何区分数据和指令?
  14. 13岁男孩偷开公交车 连撞12车撞断电线杆
  15. 网络威胁分析师必须具备的十种能力
  16. 2014 Multi-University Training Contest 5——by Xiaoxu Guo (ftiasch)
  17. 2022年最新山西机动车签字授权人模拟考试及答案
  18. Win7开启ACHI模式蓝屏的解决办法
  19. VB编码, Gamma编码, Delta编码
  20. modem 指令:AT+COPS

热门文章

  1. PHP学习笔记--面向对象
  2. bzoj 3277 串 后缀树+子树不同数个数
  3. hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  4. .Net 应用框架设计系列(二)
  5. 关于MultipleOutputFormat若干小记
  6. 从一个C程序学“逐步求精”的分析方法
  7. [渗透攻防] 二.SQL MAP工具从零解读数据库及基础用法
  8. Django RestFramework BaseSerializer
  9. 数据库开发——MySQL——简单介绍和安装
  10. Netty实战 IM即时通讯系统(六)实战: 客户端和服务端双向通信