Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  5. #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
  6. #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
  7. #define FILL(a,value) memset(a, value, sizeof(a))
  8.  
  9. #define SZ(a) (int)a.size()
  10. #define ALL(a) a.begin(), a.end()
  11. #define PB push_back
  12. #define MP make_pair
  13.  
  14. typedef long long LL;
  15. typedef vector<int> VI;
  16. typedef pair<int, int> PII;
  17.  
  18. const double PI = acos(-1.0);
  19. const int INF = 1000 * 1000 * 1000 + 7;
  20. const LL LINF = INF * (LL) INF;
  21.  
  22. const int MAX = 1010;
  23.  
  24. vector<PII> ggg[2][MAX];
  25.  
  26. vector<PII>* g;
  27. vector<PII>* g2;
  28.  
  29. VI gr[MAX];
  30.  
  31. int VAL[MAX];
  32. bool U[MAX];
  33. int C[MAX];
  34.  
  35. void dfs(int x)
  36. {
  37.     U[x] = true;
  38.     FOR (i, 0, SZ(g[x]))
  39.     {
  40.         int to = g[x][i].first;
  41.         int c = g[x][i].second;
  42.         if (c != 0) continue;
  43.         if (U[to]) continue;
  44.         dfs(to);
  45.     }
  46. }
  47.  
  48. VI ord;
  49.  
  50. void dfs1(int x)
  51. {
  52.     U[x] = true;
  53.     FOR (i, 0, SZ(g[x]))
  54.     {
  55.         int to = g[x][i].first;
  56.         int c = g[x][i].second;
  57.         if (c != 0) continue;
  58.         if (U[to]) continue;
  59.  
  60.         dfs1(to);
  61.     }
  62.  
  63.     ord.PB(x);
  64. }
  65.  
  66. void dfs2(int x, int c)
  67. {
  68.     U[x] = 1;
  69.     C[x] = c;
  70.  
  71.     FOR (i, 0, SZ(gr[x]))
  72.     {
  73.         int to = gr[x][i];
  74.         if (U[to]) continue;
  75.         dfs2(to, c);
  76.     }
  77. }
  78.  
  79. int compress(vector<PII>* g, vector<PII>* g2, int n)
  80. {
  81.     FOR (i, 0, n)
  82.     {
  83.         gr[i].clear();
  84.         U[i] = 0;
  85.     }
  86.     FOR (i, 0, n)
  87.     {
  88.         FOR (j, 0, SZ(g[i]))
  89.         {
  90.             int to = g[i][j].first;
  91.             int c = g[i][j].second;
  92.             if (c == 0) gr[to].PB(i);
  93.         }
  94.     }
  95.  
  96.     ord.clear();
  97.     FOR (i, 1, n)
  98.     {
  99.         if (!U[i]) dfs1(i);
  100.     }
  101.  
  102.     if (!U[0]) dfs1(0);
  103.  
  104.     FOR (i, 0, n)
  105.     {
  106.         U[i] = 0;
  107.     }
  108.  
  109.     reverse(ALL(ord));
  110.  
  111.     int cnt = 0;
  112.  
  113.     FOR (i, 0, SZ(ord))
  114.     {
  115.         int x = ord[i];
  116.         if (U[x]) continue;
  117.         dfs2(x, cnt);
  118.         cnt++;
  119.     }
  120.  
  121.     FOR (i, 0, cnt)
  122.     {
  123.         g2[i].clear();
  124.     }
  125.  
  126.     FOR (i, 0, n)
  127.     {
  128.         FOR (j, 0, SZ(g[i]))
  129.         {
  130.             int to = g[i][j].first;
  131.             int c = g[i][j].second;
  132.             if (C[i] == C[to]) continue;
  133.  
  134.             g2[C[i]].PB(MP(C[to], c));
  135.         }
  136.     }
  137.  
  138.     return cnt;
  139.  
  140.  
  141. }
  142.  
  143. int main()
  144. {
  145.     //freopen("in.txt", "r", stdin);
  146.     //ios::sync_with_stdio(false); cin.tie(0);
  147.  
  148.     int tt;
  149.     scanf("%d", &tt);
  150.     FOR (ttt, 0, tt)
  151.     {
  152.         g = ggg[0];
  153.         g2 = ggg[1];
  154.  
  155.         int n, m;
  156.         scanf("%d%d", &n, &m);
  157.         FOR (i, 0, n)
  158.         {
  159.             g[i].clear();
  160.         }
  161.  
  162.         FOR (i, 0, m)
  163.         {
  164.             int x, y, c;
  165.             scanf("%d%d%d", &x, &y, &c);
  166.             if (y == 0) continue;
  167.             if (x == y) continue;
  168.             g[x].PB(MP(y, c));
  169.         }
  170.  
  171.         int res = 0;
  172.         while(true)
  173.         {
  174.             FOR (i, 0, n)
  175.             {
  176.                 VAL[i] = INF;
  177.                 U[i] = 0;
  178.             }
  179.  
  180.             FOR (i, 0, n)
  181.             {
  182.                 FOR (j, 0, SZ(g[i]))
  183.                 {
  184.                     int to = g[i][j].first;
  185.                     int c = g[i][j].second;
  186.                     VAL[to] = min(VAL[to], c);
  187.                 }
  188.             }
  189.  
  190.             FOR (i, 1, n)
  191.             {
  192.                 if (VAL[i] == INF)
  193.                 {
  194.                     res = INF;
  195.                     break;
  196.                 }
  197.  
  198.                 res += VAL[i];
  199.             }
  200.  
  201.             if (res == INF) break;
  202.  
  203.             FOR (i, 0, n)
  204.             {
  205.                 FOR (j, 0, SZ(g[i]))
  206.                 {
  207.                     int to = g[i][j].first;
  208.                     g[i][j].second -= VAL[to];
  209.                 }
  210.             }
  211.  
  212.             dfs(0);
  213.  
  214.             bool ok = true;
  215.  
  216.             FOR (i, 0, n)
  217.             {
  218.                 if (!U[i]) ok = false;
  219.             }
  220.  
  221.             if (ok) break;
  222.  
  223.  
  224.             n = compress(g, g2, n);
  225.             swap(g, g2);
  226.         }
  227.  
  228.         cout<<"Case #"<<ttt+1<<": ";
  229.         if (res == INF) cout<<"Possums!"<<endl;
  230.         else cout<<res<<endl;
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement