HDU2121-Ice_cream’s world II(不定根的最小树形图)

来源:互联网 发布:深入浅出node.js 完整 编辑:程序博客网 时间:2024/05/17 02:20

题目链接

http://acm.split.hdu.edu.cn/showproblem.php?pid=2121

思路

因为根不确定,因此需要添加一个虚根结点,转为固定根节点的最小树形图。
对于虚根节点,添加权值(X)相同的边指向所有结点,权值相同为了保证每个节点都有机会作为真正的树根,并且X应略大,因大于所有边权之和,这样是为了和原图中的边区分开

细节

在记录真正的根的时候,记录的应该是边而不是结点,因为结点被重新编号过

代码

#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)#define pr(x) cout << #x << " = " << x << endl#define lc o << 1#define rc o << 1 | 1#define pl() cout << endl#define CLR(a, x) memset(a, x, sizeof(a))#define FILL(a, n, x) for (int i = 0; i < n; i++) a[i] = x#define INF 0x7f7f7f7ftypedef __int64 type;int n, m, M;const int maxm = 100000 + 5;const int maxn = 1000 + 5;struct Edge {    int u, v;    type w;    Edge(int a, int b, type c) : u(a), v(b), w(c) {    }    Edge() {    }} E[maxm];int pre[maxn], id[maxn], vis[maxn], rr;type in[maxn];void init() {    M = 0;}type directed_mst(int root, int n) {    type ans = 0;    while (1) {        FILL(in, n, INF);        for (int i = 0; i < M; i++) {            int u = E[i].u, v = E[i].v;            if (u != v && E[i].w < in[v]) {                in[v] = E[i].w;                pre[v] = u;                //因为每个点被重新编号过了,因此需要记录边                if (u == root) rr = i;            }        }        for (int i = 0; i < n; i++) {            if (i == root) continue;            if (in[i] == INF) return -1;         }        int cnt = 0;        in[root] = 0;        CLR(vis, -1);        CLR(id, -1);        for (int i = 0; i < n; i++) {            ans += in[i];            int 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] = cnt;                id[v] = cnt++;            }        }        if (cnt == 0) break;        for (int i = 0; i < n; i++)             if (id[i] == -1) id[i] = cnt++;        for (int i = 0; i < M; i++) {            int u = E[i].u, v = E[i].v;            E[i].u = id[u];            E[i].v = id[v];            if (id[u] != id[v]) E[i].w -= in[v];        }        root = id[root];        n = cnt;    }    return ans;}int main() {    while (~scan2(n, m)) {        init();        type sum = 0;        for (int i = 0; i < m; i++) {            int x, y;            type z;            scanf("%d%d%I64d", &x, &y, &z);            E[M].u = x;            E[M].v = y;            E[M++].w = z;            sum += z;        }        int root = n;        sum++;        for (int i = 0; i < n; i++) {            E[M].u = root;            E[M].v = i;            E[M++].w = sum;        }        type ans = directed_mst(root, n + 1);        if (ans == -1 || ans - sum >= sum) puts("impossible");        else printf("%I64d %d\n", ans - sum, rr - m);        pl();    }    return 0;}
0 0