【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
11 using namespace std;
12
13 const int V = 105;
14 const int inf = 0xffff;
15 int vis[V]; typec lowc[V];
16 int Map[105][105];
17
18 typec prim (typec cost[][V], int n) {
19     int i, j, p;
20     typec minc, res = 0;
21     memset(vis, 0, sizeof(vis));
22     vis[0] = 1;
23     for (i = 1; i < n; ++ i) lowc[i] = cost[0][i];
24     for (i = 1; i < n; ++ i) {
25         minc = inf; p = -1;
26         for (j = 0; j < n; ++ j) {
27             if (0 == vis[j] && minc > lowc[j]) {
28                 minc = lowc[j]; p = j;
29             }
30         }
31         if (inf == minc) return -1;
32         res += minc; vis[p] = 1;
33         for (j = 0; j < n; ++ j) {
34             if (0 == vis[j] && lowc[j] > cost[p][j]) {
35                 lowc[j] = cost[p][j];
36             }
37         }
38     }
39     return res;
40 }
41
42 int main () {
43     //cout << inf <<endl;
44     int n;
45     while (cin >> n) {
46         for (int i = 0 ; i < n; ++ i) {
47             for (int j = 0 ; j < n; ++ j) {
48                 scanf("%d", &Map[i][j]);
49             }
50         }
51         int ok_line; cin >> ok_line;
52         for (int i = 0; i < ok_line; ++ i) {
53             int s, e;
54             scanf("%d%d", &s, &e);
55             Map[s - 1][e - 1] = Map[e - 1][s - 1] = 0;
56         }
57         cout << prim(Map, n) << endl;
58     }
59     return 0;
60 }

【HDU1102】Constructing Roads(MST基础题),布布扣,bubuko.com

时间: 2024-10-07 15:26:59

【HDU1102】Constructing Roads(MST基础题)的相关文章

【HDU1301】Jungle Roads(MST基础题)

爽爆.史上个人最快MST的记录7分40s..一次A. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 10 #define

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

【HDU1162】Eddy&#39;s picture(MST基础题)

很基础的点坐标MST,一不留神就AC了, - - !! 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #include &

【HDU3371】Connect the Cities(MST基础题)

注意输入的数据分别是做什么的就好.还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #incl

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

【HDU1875】畅通工程再续(MST基础题)

更改成实形数即可.第一次敲完直接交,CE了一次.晕. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdio> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 10 #define t

【HDU1879】继续畅通工程(MST基础题)

真心大水题...不多说. 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 11 u

【HDU1233】还是畅通工程(MST基础题)

无坑,裸题.直接敲就恩那个AC. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 #include <cctype> 6 #include <algorithm> 7 #include <numeric> 8 9 #define typec int 10 using namespace std; 11 1

【HDU2122】Ice_cream’s world III(MST基础题)

2坑,3次WA. 1.判断重边取小.2.自边舍去. (个人因为vis数组忘记初始化,WA了3次,晕死!!) 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include &l