King of Destruction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1171 Accepted Submission(s): 479
Problem Description
Zhou xingxing is the successor of one style of kung fu called "Karate Kid".he is falling love with a beautiful judo student,after being humiliated by her boyfriend,a Taekwando master from Japan,Zhou is going to fight with his rival
in love.The way they fight is to destroy the wooden plank between some wooden pegs,in order to cut these wooden pegs into two disconnected parts,and destroy each piece of plank need consume different energy.However Zhou xingxing is beginner after all,so he
is turn to you for help,please calculate the minimum energy he need to destroy the wooden plank.
Input
The input consists of multiple test cases.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
Each test case starts with two integers n (0 < n <= 100) and m in one line, where n、m are the number of wooden pegs and wooden plank.
Following are m lines, each line contains three integers s, e and q (0 <= s, e < n,q > 0), meaning that there need q energy to destroy the wooden plank between s and e.
Output
There is only one line for each test case, which contains the minimum energy they need to complete this fight.
Sample Input
2 1 0 1 50 3 2 0 1 50 1 2 10
Sample Output
50 10
Source
题解:
无向图的全局最小割算法,模板,但是重边要累加起来。
最小割算法的主要思想:
每次相当于构建一个最大生成树;
然后将最后两个进入最大生成树集合的点,合并;
合并后继续构建;
直到合并为一个点时。
代码:
题解:
无向图的全局最小割算法,模板,但是重边要累加起来。
最小割算法的主要思想:
每次相当于构建一个最大生成树;
然后将最后两个进入最大生成树集合的点,合并;
合并后继续构建;
直到合并为一个点时。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 100+10;
int map[maxn][maxn];
int vis[maxn];
int node[maxn];
int dis[maxn];
int Stoer_wagner(int N) {
int pre = 0;
int ans = inf;
memset(dis, 0, sizeof(dis));
for (int i=0; i<N; i++)
node[i] = i;
int maxj, maxx=-1;
while (N > 1) {
maxx = -1; pre = 0;
for (int i=0; i<N; i++) {
dis[node[i]] = map[node[0]][node[i]];
if (dis[node[i]] > maxx) {
maxx = dis[node[i]];
maxj = i;
}
}
memset(vis, 0, sizeof(vis));
pre = 0;
vis[node[0]] = 1;
for (int j=1; j<N; j++) {
vis[node[maxj]] = 1;
if (j == N-1) {
ans = min(ans, maxx);
for (int i=0; i<N; i++) {
map[node[pre]][node[i]] += map[node[maxj]][node[i]];
map[node[i]][node[pre]] += map[node[maxj]][node[i]];
}
node[maxj] = node[--N];
}
else
{
pre = maxj;
maxx = -1;
for (int i=1; i<N; i++) {
if (!vis[node[i]]) {
dis[node[i]] += map[node[pre]][node[i]];
if (dis[node[i]] > maxx) {
maxx = dis[node[i]];
maxj = i;
}
}
}
}
}
}
return ans;
}
int main()
{
int n, m;
while (scanf("%d%d", &n, &m)!=EOF) {
memset(map, 0, sizeof(map));
for (int i=0; i<m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
map[u][v] += w;
map[v][u] += w;
}
printf("%d\n", Stoer_wagner(n));
}
return 0;
}
本文介绍了一个基于无向图的全局最小割算法的应用实例,通过解决一个具体的木板破坏问题来展示算法的具体实现过程。该算法主要用于计算从源点到汇点的最小割,通过不断寻找并合并生成树中的节点来逐步求解。
936

被折叠的 条评论
为什么被折叠?



