求全局最小割(SW算法)
来源:互联网 发布:中关村大数据产业联盟 编辑:程序博客网 时间:2024/05/08 08:51
hdu3002
King of Destruction
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1022 Accepted Submission(s): 400
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 10 1 503 20 1 501 2 10
Sample Output
5010
题意:求无向图的最小割。思路:无向图的最小割又叫全局最小割,枚举汇点求最大流效率很低,因而普遍使用StoerWagner算法,时间复杂度为O(n^3)。程序:#include"stdio.h"#include"string.h"#define inf 99999999int G[666][666],ans;int min(int a,int b){ return a<b?a:b;}int f[666];int finde(int x){ if(x!=f[x]) f[x]=finde(f[x]); return f[x];}void make(int a,int b){ int x=finde(a); int y=finde(b); if(x!=y) f[x]=y;}void Mincut(int n){ ans=inf; int node[666],dis[666]; bool use[666]; int i,k,pre; for(i=0;i<n;i++) node[i]=i; while(n>1) { int maxj=1; for(i=1;i<n;i++)//初始化到已圈集合的割大小 { dis[node[i]]=G[node[i]][node[0]]; if(dis[node[maxj]]<dis[node[i]]) { maxj=i; } } pre=0; memset(use,false,sizeof(use)); use[node[0]]=true; for(k=1;k<n;k++) { if(k==n-1)//只剩最后一个没加入集合的点,更新最小割 { ans=min(ans,dis[node[maxj]]); for(i=0;i<n;i++)//合并最后一个点以及推出它的集合中的点 { G[node[i]][node[pre]]=G[node[pre]][node[i]]+=G[node[i]][node[maxj]]; } node[maxj]=node[--n];//缩点后的图 } use[node[maxj]]=true; pre=maxj; maxj=-1; for(i=1;i<n;i++) { if(!use[node[i]]) { dis[node[i]]+=G[node[i]][node[pre]];//将上次求的maxj加入集合,合并与它相邻的边到割集 if(maxj==-1||dis[node[maxj]]<dis[node[i]]) maxj=i; } } } }}int main(){ int n,m,i; while(scanf("%d%d",&n,&m)!=-1) { memset(G,0,sizeof(G)); for(i=0;i<n;i++) { f[i]=i; } while(m--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); G[a][b]=G[b][a]+=c; make(a,b); } int flag=0; for(i=0;i<n;i++) { if(finde(i)!=finde(0)) flag++; } if(flag) printf("0\n"); else { Mincut(n); printf("%d\n",ans); } }}
0 0
- 求全局最小割(SW算法)
- SW算法求全局最小割
- sw算法求最小割学习(*)
- HDU 3691 Nubulsa Expo(SW算法求最小割)
- sw算法求最小割学习
- Stoer-Wagner算法求全局最小割
- poj 2914(stoer_wanger算法求全局最小割)
- HDU 3002 King of Destruction(全局(无向图)最小割(SW))
- SW板子 hdu3691(全局最小割 o(n^3)
- HDU 3002 King of Destruction(SW求最小割的值)
- poj 2914 全局最小割(stoer_wagner算法)
- poj2914(stoer-wagner算法求解全局最小割)
- POJ 2914 Minimum Cut 最小割集Stoer-Wagner算法(全局最小割)
- 全局最小割集Stoer-Wagner算法
- Stoer-Wagner算法(O(n^3))求全局最小割 hdu3691 2010福州站B题
- Dinic 算法求最大流(最小割) POJ 2536
- pku2914(求最小割)
- Stoer-Wagner求无向图全局最小割
- CODEFORCES 189A(完全背包完全装满)
- 25条提高iOS App性能的技巧和诀窍
- 未来两年必须掌握的移动互联网技术与能力
- Android SDKVersion 参数列表
- 进制转换
- 求全局最小割(SW算法)
- Cocos2dx 3.0 过渡篇(一) 初体验
- 查看win8.1系统 激活信息
- SQL语句GROUP BY 后面用问号?替代出现问题。
- Cocos2dx 3.0 过渡篇(二) 事件回调
- 基于python的25种设计模式
- Win7下Maven3.2.1配置
- ASIHTTPRequest:异步请求之后 用代理传值问题 代理 块
- Cocos2dx 3.0 过渡篇(三) 触摸机制