【HDU - 3002】King of Destruction(无向图全局最小割,SW算法,模板题)
題干:
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.
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 10Sample Output
50 10題目大意:
給定一個無向圖,刪除一些邊使得圖不再連通,求最小的代價。(代價定義為刪除的邊權和)。
(即在最小割的前提下,沒指定起點和終點)
解題報告:
算法過程如下(參考:http://m.blog.csdn.net/blog/u011483306/25697255):
1、設最小割ans=INF,任選一個點到集合A中,定義W(A,p)為A中的所有點到A外一點p的權值總和
2、對剛才選定的s,更新W(A,p)
3、選出A外一點p,且W(A,p)最大的作為新的s,若A!=G(V),則goto步驟2
4、把最后進入A的兩點記為s和t,用W(A,t)更新ans
5、新建頂點u,邊權w(u,v)=w(s,v)+w(t,v),刪除頂點s和t,以及與它們相連的邊(就是將s和t縮成點u)
6、若|V|!=1,則goto步驟1
最終的復雜度是O(N^2*N),若果加了優先隊列優化找最大值那一步可以達到O(NlogN*N)
但是博主說有時優先隊列STL會狂T……直接O(N^3)卻不會……我也沒有很多實戰過。。就不清楚真假了
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 100 + 5; const int INF = 0x3f3f3f3f; int n, cost[MAX][MAX]; bool Read() {int m;if(!(cin>>n>>m)) return 0;memset(cost,0, sizeof(cost));for(int i = 1; i<=m; i++) {int u,v,c;scanf("%d%d%d",&u,&v,&c);cost[u][v]+=c;cost[v][u]+=c;}return 1; } int Solve() { // 0(n^3)int wage[MAX], micut=INF;bool inset[MAX], del[MAX];memset(del, 0, sizeof(del));for(int cnt = 0; cnt<n-1; cnt++) {memset(wage, 0, sizeof (wage));memset(inset, 0, sizeof(inset));int pre=0, last=0;while(1) {int u=-1, mx=-1;for(int i = 0; i<n; i++) {if(!inset[i]&&!del[i]) {if (wage[i]>mx) {u=i;mx=wage[i];}}}if(u==-1) break;pre=last;last=u;inset[u]=1;for(int i = 0; i<n; i++) if(!inset[i]) wage[i]+=cost[u][i];}micut=min( micut, wage[last]);for(int i = 0; i<n; i++) cost[i][pre]+=cost[i][last];for(int i = 0; i<n; i++) cost[pre][i]+=cost[last][i];del[last]=1;}return micut; } int main() {while(Read()) cout << Solve() << endl;return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 3002】King of Destruction(无向图全局最小割,SW算法,模板题)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 办信用卡查学历吗 办信用卡学历会核实吗
- 下一篇: 【机器学习】 - 关于图像质量评价IQA