HDU - 1301 - Jungle Roads (最小生成樹!!prim算法!!)
Jungle Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4572 Accepted Submission(s): 3354
Problem Description
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The
Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads,
even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through
I in the maps above. The map on the right shows the roads that could be maintained mZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3QgY2hlYXBseSwgZm9yIDIxNiBhYWNtcyBwZXIgbW9udGguIFlvdXIgdGFzayBpcyB0byB3cml0ZSBhIHByb2dyYW0gdGhhdCB3aWxsIHNvbHZlIHN1Y2ggcHJvYmxlbXMuIDxicj4KPGJyPgpUaGUgaW5wdXQgY29uc2lzdHMgb2Ygb25lIHRvIDEwMCBkYXRhIHNldHMsIGZvbGxvd2VkIGJ5IGEgZmluYWwgbGluZSBjb250YWluaW5nIG9ubHkgMC4gRWFjaCBkYXRhIHNldCBzdGFydHMgd2l0aCBhIGxpbmUgY29udGFpbmluZyBvbmx5IGEgbnVtYmVyIG4sIHdoaWNoIGlzIHRoZSBudW1iZXIgb2YgdmlsbGFnZXMsIDEgPCBuIDwgMjcsIGFuZCB0aGUgdmlsbGFnZXMgYXJlIGxhYmVsZWQgd2l0aCB0aGUgZmlyc3QgbiBsZXR0ZXJzIG9mIHRoZSBhbHBoYWJldCwKIGNhcGl0YWxpemVkLiBFYWNoIGRhdGEgc2V0IGlzIGNvbXBsZXRlZCB3aXRoIG4tMSBsaW5lcyB0aGF0IHN0YXJ0IHdpdGggdmlsbGFnZSBsYWJlbHMgaW4gYWxwaGF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Zmlyc3QgZGF0YSBzZXQgZ29lcyB3aXRoIHRoZSBtYXAgYWJvdmUuIDxicj4KPGJyPgpUaGUgb3V0cHV0IGlzIG9uZSBpbnRlZ2VyIHBlciBsaW5lIGZvciBlYWNoIGRhdGEgc2V0OiB0aGUgbWluaW11bSBjb3N0IGluIGFhY21zIHBlciBtb250aCB0byBtYWludGFpbiBhIHJvYWQgc3lzdGVtIHRoYXQgY29ubmVjdCBhbGwgdGhlIHZpbGxhZ2VzLiBDYXV0aW9uOiBBIGJydXRlIGZvcmNlIHNvbHV0aW9uIHRoYXQgZXhhbWluZXMgZXZlcnkgcG9zc2libGUgc2V0IG9mIHJvYWRzIHdpbGwgbm90IGZpbmlzaCB3aXRoaW4gdGhlIG9uZSBtaW51dGUKIHRpbWUgbGltaXQuIDxicj4KCgogCjxicj4KClNhbXBsZSBJbnB1dAoKPHByZSBjbGFzcz0="brush:java;">9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0
Sample Output
216
30
Source
Mid-Central USA 2002
AC代碼:
#include
#include
#include
#define INF 0x7fffffff
using namespace std;
int map[30][30], vis[30], dis[30];
int n;
void prim()
{
for(int i=1; i<=n; i++)
{
dis[i] = map[1][i];
}
dis[1] = 0; vis[1] = 1;
int sum = 0, pos;
for(int i=1; i