NSOJ Constructing Roads(图论)

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 #include <vector>
 2 #include <map>
 3 #include <set>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstdlib>
 9 #include <string>
10 #include <cstring>
11 #include <queue>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 #define MAX 1000000
15
16 int n,ans;
17 int dis[111],vis[111],mp[111][111];
18
19 void prim()
20 {
21     memset(vis,0,sizeof(vis));
22     memset(dis,INF,sizeof(dis));
23     dis[1]=0;ans=0;dis[0]=INF;
24     while(true){
25         int m=0;
26         for(int i=1; i<=n; i++){
27             if(!vis[i] && (dis[i]<dis[m]))
28                 m=i;
29         }
30         if(m==0)
31             break;
32         vis[m]=1;
33         ans+=dis[m];
34         for(int i=1; i<=n; i++)
35             dis[i]=min(dis[i],mp[m][i]);
36     }
37 }
38
39 int main()
40 {
41     int x,a,b;
42     while(scanf("%d",&n)){
43         if(n==1)
44             break;
45         for(int i=1; i<=n; i++){
46             for(int j=1; j<=n; j++){
47                 scanf("%d",&mp[i][j]);
48             }
49         }
50         scanf("%d",&x);
51         while(x--){
52             scanf("%d%d",&a,&b);
53             mp[a][b]=mp[b][a]=0;
54         }
55         prim();
56         printf("%d\n",ans);
57     }
58
59 }

AC代码:

 1 #include <vector>
 2 #include <map>
 3 #include <set>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cstdio>
 7 #include <cmath>
 8 #include <cstdlib>
 9 #include <string>
10 #include <cstring>
11 #include <queue>
12 using namespace std;
13 #define INF 0x3f3f3f3f
14 #define MAX 1000000
15
16 int n,ans;
17 int dis[111],vis[111],mp[111][111];
18
19 void prim()
20 {
21     memset(vis,0,sizeof(vis));
22     memset(dis,INF,sizeof(dis));
23     dis[1]=0;
24     ans=0;
25     dis[0]=INF;
26     while(true){
27         int m=0;
28         for(int i=1; i<=n; i++){
29             if(!vis[i] && dis[i]<dis[m])
30                 m=i;
31         }
32         if(m==0)
33             break;
34         vis[m]=1;
35         ans+=dis[m];
36         for(int i=1; i<=n; i++)
37             dis[i]=min(dis[i],mp[m][i]);
38     }
39 }
40
41 int main()
42 {
43     int x,a,b;
44     while(scanf("%d",&n)==1){
45         for(int i=1; i<=n; i++){
46             for(int j=1; j<=n; j++){
47                 scanf("%d",&mp[i][j]);
48             }
49         }
50         scanf("%d",&x);
51         while(x--){
52             scanf("%d%d",&a,&b);
53             mp[a][b]=mp[b][a]=0;
54         }
55         prim();
56         printf("%d\n",ans);
57     }
58
59 }
时间: 2024-10-13 22:45:21

NSOJ Constructing Roads(图论)的相关文章

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)

hdu Constructing Roads(最小生成树,kuskal算法)

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

hdu oj1102 Constructing Roads(最小生成树)

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

Constructing Roads In JGShining&#39;s Kingdom(HDU 1025 LIS nlogn方法)

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21002    Accepted Submission(s): 5935 Problem Description JGShining's kingdom consists of 2n(n is no mor