Monday, November 18, 2019

[UVA] 10989 - Bomb, Divide and Conquer

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 10989 - Bomb, Divide and Conquer
Source            : UVA Online Judge
Category          : Minimum Cut
Algorithm         : Stoer Wagnar Algorithm
Complexity        : O(n^3)
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 200 + 5;  
  6.   
  7. struct StoerWagner  
  8. {  
  9.       int n;  
  10.       vector < vector <int> > cost;  
  11.       vector <int> w;  
  12.       vector <bool> marged;  
  13.       vector <int> bestCut;  
  14.   
  15.       StoerWagner() {}  
  16.   
  17.       void init(int nn)  
  18.       {  
  19.             n = nn;  
  20.             ++nn;  
  21.             cost.resize(nn, vector <int> (nn));  
  22.             w.resize(nn);  
  23.             marged.resize(nn);  
  24.       }  
  25.       void clean()  
  26.       {  
  27.             cost.clear();  
  28.             w.clear();  
  29.             marged.clear();  
  30.             bestCut.clear();  
  31.       }  
  32.       void addEdge(int u, int v, int c)  
  33.       {  
  34.             cost[u][v] += c;  
  35.             cost[v][u] += c;  
  36.       }  
  37.       int minCut()  
  38.       {  
  39.             int best = INT_MAX;  
  40.             marged[1] = true;  
  41.             for (int i = 2; i <= n; i++) marged[i] = false;  
  42.             vector <int> cut;  
  43.             vector <bool> vis(n);  
  44.             for (int phase = 1; phase < n; phase++)  
  45.             {  
  46.                   vis[1] = true;  
  47.                   for (int i = 2; i <= n; i++)  
  48.                   {  
  49.                         if (marged[i] == false)  
  50.                         {  
  51.                               vis[i] = false;  
  52.                               w[i] = cost[1][i];  
  53.                         }  
  54.                   }  
  55.                   int prv = 1;  
  56.                   int nxt;  
  57.                   for (int i = n - 1 - phase; i >= 0; i--)  
  58.                   {  
  59.                         nxt = -1;  
  60.                         for (int j = 2; j <= n; j++)  
  61.                         {  
  62.                               if (vis[j] == false and (nxt == -1 or w[j] > w[nxt])) nxt = j;  
  63.                         }  
  64.                         vis[nxt] = true;  
  65.                         if (i > 0)  
  66.                         {  
  67.                               prv = nxt;  
  68.                               for (int j = 2; j <= n; j++)  
  69.                               {  
  70.                                     if (vis[j] == false) w[j] += cost[nxt][j];  
  71.                               }  
  72.                         }  
  73.                   }  
  74.                   for (int i = 1; i <= n; i++)  
  75.                   {  
  76.                         cost[i][prv] += cost[nxt][i];  
  77.                         cost[prv][i] += cost[nxt][i];  
  78.                   }  
  79.                   marged[nxt] = true;  
  80.                   cut.emplace_back(nxt);  
  81.                   if (best > w[nxt])  
  82.                   {  
  83.                         best = w[nxt];  
  84.                         bestCut = cut;  
  85.                   }  
  86.             }  
  87.             return best;  
  88.       }  
  89. } storewagner;  
  90.   
  91. signed main()  
  92. {  
  93.       ios_base::sync_with_stdio(false);  
  94.       cin.tie(nullptr);  
  95.       cout.tie(nullptr);  
  96.   
  97.       #ifndef ONLINE_JUDGE  
  98.             freopen("in.txt""r", stdin);  
  99.       #endif // ONLINE_JUDGE  
  100.   
  101.       int tc;  
  102.       cin >> tc;  
  103.       for (int tcase = 1; tcase <= tc; tcase++)  
  104.       {  
  105.             int n;  
  106.             cin >> n;  
  107.             storewagner.init(n);  
  108.             int m;  
  109.             cin >> m;  
  110.             for (int e = 1; e <= m; e++)  
  111.             {  
  112.                   int u, v, w;  
  113.                   cin >> u >> v >> w;  
  114.                   storewagner.addEdge(u, v, w);  
  115.             }  
  116.             cout << "Case #" << tcase << ": " << storewagner.minCut() << '\n';  
  117.             storewagner.clean();  
  118.       }  
  119. }  

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.