当前位置: 代码迷 >> 综合 >> UVA 11183 Teen Girl Squad(最小树形图裸题)
  详细解决方案

UVA 11183 Teen Girl Squad(最小树形图裸题)

热度:71   发布时间:2023-12-08 10:48:08.0

题目链接:
UVA 11183 Teen Girl Squad

最小树形图裸题。好好敲模板?。。。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 1010;
const int MAX_M = 40010;
const int INF = 0x3f3f3f3f;int T, n, m, NV, NE, cases = 0;
int pre[MAX_N], vis[MAX_N], In[MAX_N], ID[MAX_N];struct Edge{int u, v, w;Edge() {}Edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w) {}
}edge[MAX_M];int ZLEdmonds(int root)
{int res = 0, u, v, w;while(1){for(int i = 0; i < NV; i++) { In[i] = INF; }for(int i = 0; i < NE; i++){u = edge[i].u, v = edge[i].v, w = edge[i].w;if(u != v && w < In[v]){In[v] = w;pre[v] = u;}}for(int i = 0; i < NV; i++){if(i != root && In[i] == INF) return -1;}int cnt = 0;memset(vis, -1, sizeof(vis));memset(ID, -1, sizeof(ID));In[root] = 0;for(int i = 0; i < NV; i++){res += In[i];v = i;while(v != root && ID[v] == -1 && vis[v] != i){vis[v] = i;v = pre[v];}if(v != root && ID[v] == -1){for( u = pre[v]; u != v; u = pre[u]){ID[u] = cnt;}ID[v] = cnt++;}}if(cnt == 0) break;for(int i = 0; i < NV; i++) { if(ID[i] == -1) ID[i] = cnt++; }for(int i = 0; i < NE; i++){u = edge[i].u, v = edge[i].v, w = edge[i].w;edge[i].u = ID[u], edge[i].v = ID[v];if(edge[i].u != edge[i].v){edge[i].w -= In[v];}}NV = cnt, root = ID[root];}return res;
}int main()
{scanf("%d", &T);while(T--){scanf("%d%d", &n, &m);for(int i = 0; i < m; i++){scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);}NV = n, NE = m;int ans = ZLEdmonds(0);printf("Case #%d: ", ++cases);if(ans == -1) printf("Possums!\n");else printf("%d\n", ans);}return 0;
}