Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define FOR(i,a,b) for (int i = (a); i < (b); i++)
- #define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
- #define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
- #define FILL(a,value) memset(a, value, sizeof(a))
- #define SZ(a) (int)a.size()
- #define ALL(a) a.begin(), a.end()
- #define PB push_back
- #define MP make_pair
- typedef long long LL;
- typedef vector<int> VI;
- typedef pair<int, int> PII;
- const double PI = acos(-1.0);
- const int INF = 1000 * 1000 * 1000 + 7;
- const LL LINF = INF * (LL) INF;
- const int MAX = 1010;
- vector<PII> ggg[2][MAX];
- vector<PII>* g;
- vector<PII>* g2;
- VI gr[MAX];
- int VAL[MAX];
- bool U[MAX];
- int C[MAX];
- void dfs(int x)
- {
- U[x] = true;
- FOR (i, 0, SZ(g[x]))
- {
- int to = g[x][i].first;
- int c = g[x][i].second;
- if (c != 0) continue;
- if (U[to]) continue;
- dfs(to);
- }
- }
- VI ord;
- void dfs1(int x)
- {
- U[x] = true;
- FOR (i, 0, SZ(g[x]))
- {
- int to = g[x][i].first;
- int c = g[x][i].second;
- if (c != 0) continue;
- if (U[to]) continue;
- dfs1(to);
- }
- ord.PB(x);
- }
- void dfs2(int x, int c)
- {
- U[x] = 1;
- C[x] = c;
- FOR (i, 0, SZ(gr[x]))
- {
- int to = gr[x][i];
- if (U[to]) continue;
- dfs2(to, c);
- }
- }
- int compress(vector<PII>* g, vector<PII>* g2, int n)
- {
- FOR (i, 0, n)
- {
- gr[i].clear();
- U[i] = 0;
- }
- FOR (i, 0, n)
- {
- FOR (j, 0, SZ(g[i]))
- {
- int to = g[i][j].first;
- int c = g[i][j].second;
- if (c == 0) gr[to].PB(i);
- }
- }
- ord.clear();
- FOR (i, 1, n)
- {
- if (!U[i]) dfs1(i);
- }
- if (!U[0]) dfs1(0);
- FOR (i, 0, n)
- {
- U[i] = 0;
- }
- reverse(ALL(ord));
- int cnt = 0;
- FOR (i, 0, SZ(ord))
- {
- int x = ord[i];
- if (U[x]) continue;
- dfs2(x, cnt);
- cnt++;
- }
- FOR (i, 0, cnt)
- {
- g2[i].clear();
- }
- FOR (i, 0, n)
- {
- FOR (j, 0, SZ(g[i]))
- {
- int to = g[i][j].first;
- int c = g[i][j].second;
- if (C[i] == C[to]) continue;
- g2[C[i]].PB(MP(C[to], c));
- }
- }
- return cnt;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- //ios::sync_with_stdio(false); cin.tie(0);
- int tt;
- scanf("%d", &tt);
- FOR (ttt, 0, tt)
- {
- g = ggg[0];
- g2 = ggg[1];
- int n, m;
- scanf("%d%d", &n, &m);
- FOR (i, 0, n)
- {
- g[i].clear();
- }
- FOR (i, 0, m)
- {
- int x, y, c;
- scanf("%d%d%d", &x, &y, &c);
- if (y == 0) continue;
- if (x == y) continue;
- g[x].PB(MP(y, c));
- }
- int res = 0;
- while(true)
- {
- FOR (i, 0, n)
- {
- VAL[i] = INF;
- U[i] = 0;
- }
- FOR (i, 0, n)
- {
- FOR (j, 0, SZ(g[i]))
- {
- int to = g[i][j].first;
- int c = g[i][j].second;
- VAL[to] = min(VAL[to], c);
- }
- }
- FOR (i, 1, n)
- {
- if (VAL[i] == INF)
- {
- res = INF;
- break;
- }
- res += VAL[i];
- }
- if (res == INF) break;
- FOR (i, 0, n)
- {
- FOR (j, 0, SZ(g[i]))
- {
- int to = g[i][j].first;
- g[i][j].second -= VAL[to];
- }
- }
- dfs(0);
- bool ok = true;
- FOR (i, 0, n)
- {
- if (!U[i]) ok = false;
- }
- if (ok) break;
- n = compress(g, g2, n);
- swap(g, g2);
- }
- cout<<"Case #"<<ttt+1<<": ";
- if (res == INF) cout<<"Possums!"<<endl;
- else cout<<res<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement