标签:
3 1 0 1 1 4 4 0 1 10 0 2 10 1 3 20 2 3 30
impossible 40 0
/************************************************************************* > File Name: hdu2121.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2015年01月26日 星期一 14时46分50秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const __int64 inf = ((1LL) << 60); const int N = 1110; struct node { int u, v; __int64 w; }edge[N * N]; int g; __int64 in[N]; int pre[N], vis[N], id[N]; __int64 zhuliu (int root, int n, int m) { __int64 res = 0; int u, v; while (1) { for (int i = 0; i < n; ++i) { in[i] = inf; } for (int i = 0; i < m; ++i) { if (edge[i].u != edge[i].v && edge[i].w < in[edge[i].v]) { in[edge[i].v] = edge[i].w; pre[edge[i].v] = edge[i].u; if (edge[i].u == root) { g = i; } } } for (int i = 0; i < n; ++i) { if (i != root && in[i] == inf) { return -1; } } int tn = 0; memset (id, -1, sizeof(id)); memset (vis, -1, sizeof(vis)); in[root] = 0; for (int i = 0; i < n; ++i) { res += in[i]; v = i; while (vis[v] != i && id[v] == -1 && v != root) { vis[v] = i; v = pre[v]; } if (v != root && id[v] == -1) { for (int u = pre[v]; u != v; u = pre[u]) { id[u] = tn; } id[v] = tn++; } } if (tn == 0) { break; } for (int i = 0; i < n; ++i) { if (id[i] == -1) { id[i] = tn++; } } for (int i = 0; i < m; ++i) { int v = edge[i].v; edge[i].u = id[edge[i].u]; edge[i].v = id[edge[i].v]; if (edge[i].u != edge[i].v) { edge[i].w -= in[v]; } } n = tn; root = id[root]; } return res; } int main () { int n, m, u, v; __int64 w; while (~scanf("%d%d", &n, &m)) { __int64 sum = 0; for (int i = 0; i < m; ++i) { scanf("%d%d%I64d", &u, &v, &w); if (u == v) { continue; } ++u; ++v; edge[i].u = u; edge[i].v = v; edge[i].w = w; sum += w; } ++sum; for (int i = m; i < m + n; ++i) { edge[i].u = 0; edge[i].v = i - m + 1; edge[i].w = sum; } __int64 ans = zhuliu (0, n + 1, m + n); if (ans == - 1 || ans - sum >= sum) { printf("impossible\n"); } else { printf("%I64d %d\n", ans - sum, g - m); } printf("\n"); } return 0; }
hdu2121---Ice_cream’s world II
标签:
原文地址:http://blog.csdn.net/guard_mine/article/details/43153027