0%

HDU 2121 Ice_cream’s world II 最小树形图 模板

开始学习最小树形图,模板题。

Ice_cream’s world II

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem 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

分析

一道裸的最小树形图模板题,算法思路比较简单,但是写起来感觉很不优美…仅作为模板记录吧。

本题不确定起点,故在读入完数据之后建立一个虚根,把所有的点都与这个虚根相连,边权为所有边边权和+1(保证虚边足够长),最后减掉即可。

确定起点的题可以从模板中删掉这部分。

然后就是这个模板是从0开始存储…强迫症好不爽啊…

详细参见:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU 2121
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1010;
const int M = N*N;

typedef struct nod
{
int a,b,c;
} node;

node edge[M];

int predge[N],id[N],visit[N];//id用来标记圈的
int in[N];//入弧最小的

int ansroot;

int dmst(int root,int n,int m)//n表示点数,m表示边数,root表示根
{
int u,v;
int ret=0;
while(true)
{
for(int i=0;i<n;i++) in[i]=INT_MAX;
for(int i=0;i<m;i++)
{
if(edge[i].c<in[edge[i].b]&&edge[i].a!=edge[i].b)
{
predge[edge[i].b]=edge[i].a;//找出每个点的最小入弧
if(edge[i].a==root) ansroot=i;
in[edge[i].b]=edge[i].c;
}
}
for(int i=0;i<n;i++)
{
if(i==root) continue;
if(in[i]==INT_MAX) return -1;
}
in[root]=0;
int cnt=0;
memset(id,-1,sizeof(id));
memset(visit,-1,sizeof(visit));
for(int i=0;i<n;i++)
{
ret+=in[i];//进行缩圈
v=i;
while(visit[v]!=i&&id[v]==-1&&v!=root)
{
visit[v]=i;
v=predge[v];
}
if(v!=root&&id[v]==-1)
{
for(u=predge[v];u!=v;u=predge[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++)
{
v=edge[i].b;//进行缩点,重新标记。
edge[i].a=id[edge[i].a];
edge[i].b=id[edge[i].b];
if (edge[i].a!=edge[i].b) edge[i].c-=in[v];
}
n=cnt;
root=id[root];
}
return ret;
}
int main()
{
freopen("2121.txt","r",stdin);
int n,m,m1;
while(scanf("%d%d",&n,&m)!=EOF)
{
int a,b;
int r=0;
m1=m;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].c);
r+=edge[i].c;
}
r++;
for(int i=0;i<n;i++)
{
edge[m].a=n;
edge[m].b=i;
edge[m].c=r;
m++;
}
int ans=dmst(n,n+1,m);
ansroot-=m1;//最小根对应的标号为i-m1
if(ans==-1||ans>=2*r) printf("impossible\n");
else printf("%d %d\n",ans-r,ansroot);
printf("\n");
}
return 0;
}