soj2093: Farmer John

来源:互联网 发布:樱井知香迅雷种子磁力 编辑:程序博客网 时间:2024/05/19 17:22

soj上的一题,自己没想清楚就做结果坑了好久,先上题目


Problem D: Farmer John
If you once took part in USACO, which is short for USA Computing Olympiad,
you will know who is Farmer John. Well, I am also sure that some of you know
nothing about him. However, it does not matter. You could just help him solve
programming tasks.
John has built a huge theme park for crops. Wow, he really looks like \A
Tall-Rich-Handsome", and the park looks like \A Gao-Da-Shang". However,
he has to face a thorny problem recently. He needs to build many irrigation
equipments for the crops in the theme park. He should guarantee that each
crop-site (denoted by a number) has an access to water source. More clearly,
he could build a water equipment in a crop-site so that the crop-site can obtain
water directly. And, any other crop-site that connects a crop-site containing a
water equipment can also obtain the water indirectly via a water pipeline.
Figure 2: An example of crop-site map.
For example, Fig. 2 shows an example of crop-site map. As shown in Fig.
2, it contains 4 crop-sites, denoted by decimal number 1, 2, 3, and 4. An edge
between two crop-sites indicates that a water pipeline can be built between
them, otherwise, it is not able to build a water pipeline. For any two crop-sites
that can be connected by a pipeline, it needs a certain cost to build the pipeline.
For instance, if we want to build a pipeline between 1 and 2 (crop-site), we need
a cost of 10, which is marked in Fig. 2. For any crop-site, if we want to build
a water equipment (water source), we also need a certain cost. For instance, if
we want to build a water equipment in 1 (crop-site), we need a cost of 100; if
we want to build a water equipment in 4, we need a cost of 5, etc.
Farmer John hopes you help him to nd the minimum cost to build all pos-
sible water equipments and pipelines so that each of the crop-sites can directly
or indirectly obtain the water. E.g. for Fig. 2, we can build all the pipelines
with a cost of 30 and build a water equipment in 2 with a cost of 50, this way,
we need a total cost of 80. However, it is not a minimum cost! We can still
build all the pipelines with a cost of 30, but build a water equipment in 4 with
a cost of 5, this way, we only need a total cost of 35 and all the crop-sites can
obtain water directly or indirectly.
6
Input
Input contains multiple cases. Each case starts with two integer n and m (0 
n;m  105, m  n(n
0 0