- 【例4-11】最短网络(agrinet)
666
- 2025-7-7 14:35:55 @
#include <bits/stdc++.h> using namespace std;
int main() { int n; cin >> n; vector<vector > dist(n, vector(n)); for (auto& row : dist){ for (auto& d : row){ cin >> d; } } vector 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; }
信息
- ID
- 351
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 3
- 标签
- (无)
- 递交数
- 38
- 已通过
- 22
- 上传者