'commit'
This commit is contained in:
40
TangDou/AcWing_TiGao/T3/MinialSpanningTree/1142_Prim.cpp
Normal file
40
TangDou/AcWing_TiGao/T3/MinialSpanningTree/1142_Prim.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
const int N = 310;
|
||||
int dis[N];
|
||||
int g[N][N];
|
||||
int n, m;
|
||||
bool st[N];
|
||||
|
||||
int prim() {
|
||||
memset(dis, 0x3f, sizeof dis);
|
||||
dis[1] = 0;
|
||||
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int t = -1;
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (!st[j] && (t == -1 || dis[t] > dis[j]))
|
||||
t = j;
|
||||
res = max(res, dis[t]); // 找出最长,不要累加和
|
||||
for (int j = 1; j <= n; j++)
|
||||
if (!st[j] && dis[j] > g[t][j])
|
||||
dis[j] = g[t][j];
|
||||
st[t] = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
memset(g, 0x3f, sizeof g);
|
||||
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
g[a][b] = g[b][a] = min(g[a][b], c);
|
||||
}
|
||||
printf("%d %d\n", n - 1, prim());
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user