HDU 2121 Ice_cream’s world II

本文介绍了一个关于最小树形图的问题,通过构建虚拟根节点来寻找最优解的方法。问题背景为在一个由城市和道路组成的网络中选择首都位置并进行道路优化。文中详细解释了算法流程,包括初始化、迭代计算直至找到最小成本方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ice_cream’s world II

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on  HDU. Original ID: 2121
64-bit integer IO format: %I64d      Java class name: Main
 
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

Source

 
解题:最小树形图。由于我们加的虚拟根与边有关,所以可以方便的找出实际根
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 1010;
 5 struct arc {
 6     int u,v,w;
 7 } e[50000];
 8 int in[maxn],pre[maxn],hs[maxn],vis[maxn],Rt;
 9 int DMST(int root,int n,int m,int ret = 0) {
10     while(true) {
11         for(int i = 0; i < n; ++i) {
12             hs[i] = vis[i] = -1;
13             in[i] = INF;
14         }
15         for(int i = 0; i < m; ++i) {
16             if(e[i].u != e[i].v && e[i].w < in[e[i].v]) {
17                 in[e[i].v] = e[i].w;
18                 pre[e[i].v] = e[i].u;
19                 if(e[i].u == root) Rt = i;
20             }
21         }
22         for(int i = 0; i < n; ++i)
23             if(root != i && in[i] == INF) return -1;
24         int cnt = in[root] = 0;
25         for(int i = 0; i < n; ++i) {
26             ret += in[i];
27             int v = i;
28             while(vis[v] != i && hs[v] == -1 && v != root) {
29                 vis[v] = i;
30                 v = pre[v];
31             }
32             if(v != root && hs[v] == -1) {
33                 for(int u = pre[v]; u != v; u = pre[u]) hs[u] = cnt;
34                 hs[v] = cnt++;
35             }
36         }
37         if(!cnt) break;
38         for(int i = 0; i < n; ++i)
39             if(hs[i] == -1) hs[i] = cnt++;
40         for(int i = 0; i < m; ++i) {
41             int u = e[i].u;
42             int v = e[i].v;
43             e[i].u = hs[u];
44             e[i].v = hs[v];
45             if(e[i].u != e[i].v) e[i].w -= in[v];
46         }
47         n = cnt;
48         root = hs[root];
49     }
50     return ret;
51 }
52 int main() {
53     int n,m,sum;
54     while(~scanf("%d%d",&n,&m)) {
55         for(int i = sum = 0; i < m; ++i) {
56             scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
57             sum += e[i].w;
58         }
59         sum++;
60         for(int i = 0; i < n; ++i) {
61             e[i + m].u = n;
62             e[i + m].v = i;
63             e[i + m].w = sum;
64         }
65         int ret = DMST(n,n + 1,n + m);
66         if(ret == -1 || ret > (sum<<1)) puts("impossible");
67         else printf("%d %d\n",ret-sum,Rt - m);
68         puts("");
69     }
70     return 0;
71 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4761665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值