【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 <numeric>
 9
10 #define typec int
11 using namespace std;
12
13 const int V = 1005;
14 const typec inf = 0xffff;
15 int vis[V];
16 typec lowc[V];
17 int Map[V][V];
18
19 typec prim (typec cost[][V], int n) {
20     int i, j, p;
21     typec minc, res = 0;
22     memset(vis, 0, sizeof(vis));
23     vis[0] = 1;
24     for (i = 1; i < n; ++ i) lowc[i] = cost[0][i];
25     for (i = 1; i < n; ++ i) {
26         minc = inf; p = -1;
27         for (j = 0 ; j < n; ++ j) {
28             if (0 == vis[j] && minc > lowc[j]) {
29                 minc = lowc[j]; p = j;
30             }
31         }
32         if (inf == minc) return -1;
33         res += minc; vis[p] = 1;
34         for (j = 0 ; j < n; ++ j) {
35             if (0 == vis[j] && lowc[j] > cost[p][j]) {
36                 lowc[j] = cost[p][j];
37             }
38         }
39     }
40     return res;
41 }
42
43 int main () {
44     int n, road_n;
45     int cur = 0;
46     while (cin >> n >> road_n) {
47         /*if (cur ++ != 0) {
48             cout << endl;
49         }*/
50         for (int i = 0 ; i < n; ++ i) {
51             for (int j = 0 ; j < n; ++ j) {
52                 if (i == j) Map[i][j] = 0;
53                 else {
54                     Map[i][j] = inf;
55                 }
56             }
57         }
58
59         for (int i = 0 ; i < road_n; ++ i) {
60             int x, y, c;
61             scanf("%d%d%d", &x, &y, &c);
62             if (x == y) {
63                 continue;
64             }
65             Map[x][y] = Map[y][x] = min(Map[x][y], c);
66         }
67
68         int ans = prim (Map, n);
69
70         if (ans == -1) {
71             cout << "impossible" << endl << endl;
72         } else {
73             cout << ans << endl << endl;
74         }
75     }
76     return 0;
77 }

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

时间: 2024-10-07 14:21:21

【HDU2122】Ice_cream’s world III(MST基础题)的相关文章

【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

【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 &

HDU2122 Ice_cream’s world III 【最小生成树】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 802    Accepted Submission(s): 274 Problem Description ice_cream's world becomes stronger and stronger; every road is built

【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

【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

【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【Kruskal】

Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 997    Accepted Submission(s): 321 Problem Description ice_cream's world becomes stronger and stronger; every road is built