POJ 2914 Minimum Cut 全局最小割
2016-04-08 23:27
351 查看
求无向图的最小割
刷UOJ用户群的时候看到有人问一道全局最小割的裸题。。忽然想起之前还坑着Stoer Wagner算法。。
Total Submissions: 8588 Accepted: 3617
Case Time Limit: 5000MS
刷UOJ用户群的时候看到有人问一道全局最小割的裸题。。忽然想起之前还坑着Stoer Wagner算法。。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define FOR(i,j,k) for(i=j;i<=k;++i) const int N = 501; int vis , dis , v , a ; int stoer_wagner(int n) { int i, j, ans = 0x7fffffff, p, last; FOR(i,1,n) v[i] = i; while (n > 1) { p = last = 0; FOR(i,2,n) { dis[v[i]] = a[v[1]][v[i]]; if (dis[v[i]] > dis[v[p]]) p = i; } FOR(i,2,n) vis[v[i]] = 0; vis[v[1]] = 1; FOR(i,2,n) { if (i == n) { ans = min(ans, dis[v[p]]); FOR(j,1,n) a[v[j]][v[last]] = a[v[last]][v[j]] += a[v[j]][v[p]]; v[p] = v[n--]; } vis[v[last = p]] = 1; p = -1; FOR(j,2,n) if (!vis[v[j]]) { dis[v[j]] += a[v[last]][v[j]]; if (p == -1 || dis[v[p]] < dis[v[j]]) p = j; } } } return ans; } int main() { int n, m, u, v, w; while (scanf("%d%d", &n, &m) != EOF) { memset(a, 0, sizeof a); while (m--) { scanf("%d%d%d", &u, &v, &w); ++u; ++v; a[u][v] += w; a[v][u] += w; } printf("%d\n", stoer_wagner(n)); } return 0; }
Minimum Cut
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 8588 Accepted: 3617
Case Time Limit: 5000MS
Description
Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must be removed at least to disconnect the graph into two subgraphs?Input
Input contains multiple test cases. Each test case starts with two integers N and M (2 ≤ N ≤ 500, 0 ≤ M ≤ N × (N − 1) ⁄ 2) in one line, where N is the number of vertices. Following are M lines, each line contains M integers A, B and C (0 ≤ A, B < N, A ≠ B, C > 0), meaning that there C edges connecting vertices A and B.Output
There is only one line for each test case, which contains the size of the minimum cut of the graph. If the graph is disconnected, print 0.Sample Input
3 3 0 1 1 1 2 1 2 0 1 4 3 0 1 1 1 2 1 2 3 1 8 14 0 1 1 0 2 1 0 3 1 1 2 1 1 3 1 2 3 1 4 5 1 4 6 1 4 7 1 5 6 1 5 7 1 6 7 1 4 0 1 7 3 1
Sample Output
2 1 2
相关文章推荐
- android的一道面试题
- 解决问题的细微方法
- javascript中replace与正则表达式
- 搭建公司内部的NuGet Server
- 在阿里云中编译Linux4.5.0内核 - Ubuntu内核编译教程
- 刚刚注册的博客
- forward和redirect 的区别
- hdu 1078 FatMouse and Cheese(dfs)
- arpack编译64位版本的记录
- JAVA 对象引用,以及对象赋值(转载)
- 互联网产品各阶段的标准流程文档
- Java笔记
- 2010上交:计算表达式
- Python3爬虫之爬取某一路径的所有html文件
- web框架--flask
- 用 Express4 写一个简单的留言板
- 剑指Offer--004-替换空格
- C++11中的bind与lambda比较
- PHP的单个文件上传、多个单文件上传、多文件上传
- QT5 Opengl (八,绘制雾的效果)