UVA 11183 Teen Girl Squad 最小树形图

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;

#define mxn 1020
#define mxe 40200
#define LL long long
#define inf 0x3f3f3f3f
#define vi vector<int>
#define PB push_back
#define MP make_pair
#define G(i, u) for(int i = fst[u]; ~i; i = nxt[i])
#define F(i, n) for(int i = 1; i <= n; ++i)

int n, m;

int A[mxe], B[mxe], C[mxe];
int in[mxn], pre[mxn];
int id[mxn], vis[mxn];

int DMST(int s, int n) {
	int ret = 0;
	while(1) {
		memset(in, 0x3f, sizeof in);
		F(i, m) {
			if(A[i] != B[i] && C[i] < in[B[i]])
				in[B[i]] = C[i], pre[B[i]] = A[i];
		}
		in[s] = 0;
		F(i, n) if(in[i] == inf) return -1;
		memset(vis, -1, sizeof vis);
		memset(id, -1, sizeof id);
		int cnt = 0;
		F(i, n) {
			ret += in[i];
			int v = i;
			while(vis[v] != i && id[v] == -1 && v != s)
				vis[v] = i, v = pre[v];
			if(id[v] == -1 && v != s) {
				id[v] = ++cnt;
				for(int u = pre[v]; u != v; u = pre[u])
					id[u] = cnt;
			}
		}
		if(cnt == 0) break;
		F(i, n) if(id[i] == -1) id[i] = ++cnt;
		F(i, m) {
			
			C[i] -= in[B[i]];
			A[i] = id[A[i]], B[i] = id[B[i]];
		}
		n = cnt, s = id[s];
	}
	return ret;
}
int main() {
	int cas, kk = 0;
	scanf("%d", &cas);
	while(cas--) {
		scanf("%d%d", &n, &m);
		F(i, m) {
			scanf("%d%d%d", &A[i], &B[i], &C[i]);
			++ A[i], ++ B[i];
		}
		printf("Case #%d: ", ++kk);
		int ans = DMST(1, n);
		if(ans == -1)
			puts("Possums!");
		else
			printf("%d\n", ans);
	}
	return 0;
}

你可能感兴趣的:(最小树形图)