1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| #include <cstdio> #include <cstring> #define maxN 1000010 struct Edge{ int x, y, g; bool u; } b[maxN << 1]; int len = 1, X = 0, Y = 0; int f[maxN][2]; int val[maxN], vis[maxN], h[maxN]; int min (int x, int y) { return x < y ? x : y; } void ins (int x, int y) { len++; b[len].x = x; b[len].y = y; b[len].g = h[x]; h[x] = len; } void dfs (int x, int from) { if(X) { return ; } vis[x] = true; for(int i = h[x];i;i = b[i].g) { int y = b[i].y; if(y != from) { if(vis[y]) { b[i].u = b[i ^ 1].u = true; X = x, Y = y; return ; } dfs(y, x); } } } void work (int x, int from) { bool flag = false; f[x][0] = f[x][1] = 0; for(int i = h[x];i;i = b[i].g) { int y = b[i].y; if(y != from && !b[i].u) { flag = true; work(y, x); f[x][0] += f[y][1]; f[x][1] += min(f[y][0], f[y][1]); } } if(!flag) { f[x][0] = 0, f[x][1] = val[x]; } else { f[x][1] += val[x]; } } int main () { int n = 0, A = 0, B = 0; scanf("%d %d %d", &n, &A, &B); for(int i = 1;i <= n; i++) { int x = 0, y = 0; scanf("%d %d", &x, &y); val[x] += A, val[y] += B; ins(x, y), ins(y, x); } dfs(1, 0); int Ans = 2147483647; memset(f, 127 / 3, sizeof(f)); work(X, 0); Ans = min(Ans, f[X][1]); memset(f, 127 / 3, sizeof(f)); work(Y, 0); Ans = min(Ans, f[Y][1]); printf("%d", Ans); return 0; }
|