Constructing Roads--hdu1102

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17187    Accepted Submission(s): 6526

Problem Description

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input

The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output

You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input

3

0 990 692

990 0 179

692 179 0

1

1 2

Sample Output

179

看了好久才看懂,前三行的意思是,第1,2,3个村子分别距离第1,2,3个村子的距离,

最小生成树来写!

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int per[110],map[101][101];
 6 struct node
 7 {
 8     int b,e,w;
 9 }s[10000];
10 bool cmp(node x,node y)
11 {
12     return x.w<y.w;
13 }
14 void init()
15 {
16     for(int i=1;i<110;i++)
17     per[i]=i;
18  }
19
20  int find(int x)
21  {
22      while(x!=per[x])
23      x=per[x];
24      return x;
25  }
26
27  bool join (int x,int y)
28  {
29      int fx=find(x);
30      int fy=find(y);
31      if(fx!=fy)
32      {
33          per[fx]=fy;
34          return true;
35      }
36      return false;
37  }
38  int main()
39  {
40      int n,i,n1,a,b,j;
41      while(scanf("%d",&n)!=EOF)
42      {
43          init();
44          for(i=1;i<=n;i++)
45             for(j=1;j<=n;j++)
46                  scanf("%d",&map[i][j]);
47          scanf("%d",&n1);
48         for(i=0;i<n1;i++)
49         {
50             scanf("%d%d",&a,&b);
51             map[a][b]=0;
52         }
53              int k=0;
54              for(i=1;i<=n;i++)
55              {
56                  for(j=i;j<=n;j++)
57                  {
58                      s[k].b=i;
59                      s[k].e=j;
60                      s[k].w=map[i][j];
61                      k++;
62                  }
63              }
64         sort(s,s+k,cmp);
65         int sum=0;
66         for(i=0;i<k;i++)
67         {
68             if(join(s[i].b,s[i].e))
69             sum+=s[i].w;
70         }
71         printf("%d\n",sum);
72       }
73      return 0;
74  }
75  
时间: 2024-07-30 21:44:55

Constructing Roads--hdu1102的相关文章

【HDU1102】Constructing Roads(MST基础题)

最小生成树水题.prim一次AC 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define typec int

POJ2421 &amp; HDU1102 Constructing Roads(最小生成树)

嘎唔!~又一次POJ过了HDU错了...不禁让我想起前两天的的Is it a tree?   orz..这次竟然错在HDU一定要是多组数据输入输出!(无力吐槽TT)..题目很简单,炒鸡水! 题意: 告诉你每个村庄之间的距离,和几组已经联通的村庄,求使所有村庄联通所要建的道路的最短距离. 很简单,用最小生成树的Prim算法,相当于邻接矩阵已知,把已联通的村庄之间的距离改为零即可. 附上AC代码: 1 #include <stdio.h> 2 #include <string.h> 3

HDU1102 Constructing Roads 【最小生成树Prim】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13756    Accepted Submission(s): 5223 Problem Description There are N villages, which are numbered from 1 to N, and you should

HDU1102 Constructing Roads【Prim】

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14897    Accepted Submission(s): 5677 Problem Description There are N villages, which are numbered from 1 to N, and you should

POJ 2421 Constructing Roads 修建道路 最小生成树 Kruskal算法

题目链接:POJ 2421 Constructing Roads 修建道路 Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19698   Accepted: 8221 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that e

Constructing Roads In JGShining&#39;s Kingdom HDU - 1025

JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities)

Constructing Roads(最小生成树)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 274 Accepted Submission(s): 172   Problem Description There are N villages, which are numbered from 1 to N, and you should build

hdu-1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21045    Accepted Submission(s): 5950 Problem Description JGShining's kingdom consists of 2n(n is

Constructing Roads (MST)

Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1102 Description There are N villages, which are numbered from 1 to N, and you should build some roads such that every two vill

hdu 1102 Constructing Roads Kruscal

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更适合用Prim来做. 用Kruscal的话要做一些变化. /*Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s)