HDU 2121 Ice

    xiaoxiao2021-12-14  19

    Description

    After awarded lands to ACMers, the queen want to choose a city be her capital. This is an important event in ice_cream world, and it also a very difficult problem, because the world have N cities and M roads, every road was directed. Wiskey is a chief engineer in ice_cream world. The queen asked Wiskey must find a suitable location to establish the capital, beautify the roads which let capital can visit each city and the project’s cost as less as better. If Wiskey can’t fulfill the queen’s require, he will be punishing.

    Input

    Every case have two integers N and M (N<=1000, M<=10000), the cities numbered 0…N-1, following M lines, each line contain three integers S, T and C, meaning from S to T have a road will cost C.

    Output

    If no location satisfy the queen’s require, you must be output “impossible”, otherwise, print the minimum cost in this project and suitable city’s number. May be exist many suitable cities, choose the minimum number city. After every case print one blank. 

    Sample Input

    3 1 0 1 1 4 4 0 1 10 0 2 10 1 3 20 2 3 30

    Sample Output

    impossible

    40 0

    无根的最小树形图,在原来的板子上稍加修改即可。

    #include<cmath> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define rep(i,j,k) for(int i=j;i<=k;i++) #define loop(i,j,k) for (int i=j;i!=-1;i=k[i]) #define inone(x) scanf("%d",&x) #define intwo(x,y) scanf("%d%d",&x,&y) #define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z) const int N = 1e5+ 10; const int INF = 0x7FFFFFFF; int n, m, ans, who; int x[N], y[N], z[N]; int pre[N], in[N], v[N], f[N]; int zhuliu(int rt) { long long res = who = 0; while (true) { rep(i, 0, n) in[i] = INF; rep(i, 1, m) { if (z[i] < in[y[i]] && x[i] != y[i]) { pre[y[i]] = x[i], in[y[i]] = z[i]; if (x[i] == rt) who = i; } } int cnt = in[rt] = 0; rep(i, 0, n) f[i] = v[i] = -1; for (int i = 0, j; i <= n; i++) { res += in[i]; for (j = i; j != rt&&v[j] != i&&f[j] == -1; v[j] = i, j = pre[j]); if (j != rt && f[j] == -1) { for (int k = pre[j]; k != j; k = pre[k]) f[k] = cnt; f[j] = cnt++; } } if (res - INF > INF - 2) return -1; if (!cnt) break; rep(i, 0, n) if (f[i] == -1) f[i] = cnt++; for (int i = 1, j; i <= m; i++) { x[i] = f[x[i]]; j = y[i]; y[i] = f[y[i]]; if (x[i] != y[i]) z[i] -= in[j]; } n = cnt - 1; rt = f[rt]; } return res - INF + 1; } int main() { while (intwo(n, m) != EOF) { rep(i, 1, m) { inthr(x[i], y[i], z[i]); x[i]++; y[i]++; } rep(i, 1, n) x[i + m] = 0, y[i + m] = i, z[i + m] = INF - 1; m += n; int nn = n; if ((ans = zhuliu(0)) == -1) printf("impossible\n"); else printf("%d %d\n", ans, who - m + nn - 1); putchar(10); } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-970872.html

    最新回复(0)