- 【例4-11】最短网络(agrinet)
ac
- 2025-7-7 14:50:12 @
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<vector<int> > dist(n, vector<int>(n));
for (auto& row : dist){
for (auto& d : row){
cin >> d;
}
}
vector<bool> connected(n, false);
connected[0] = true;
int total = 0;
for (int k = 1; k < n; k++) {
int min_d = INT_MAX, next = -1;
for (int i = 0; i < n; i++) {
if (connected[i]) {
for (int j = 0; j < n; j++) {
if (!connected[j] && dist[i][j] < min_d) {
min_d = dist[i][j];
next = j;
}
}
}
}
connected[next] = true;
total += min_d;
}
cout << total << endl;
return 0;
}
0 条评论
目前还没有评论...
信息
- ID
- 351
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- (无)
- 递交数
- 38
- 已通过
- 22
- 上传者