1 条题解
-
0
最短网络(Agri-Net) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <algorithm> using namespace std; const int INF = 1000000000; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n)) return 0; vector<vector<int>> g(n, vector<int>(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) cin >> g[i][j]; } vector<int> dist(n, INF); vector<char> vis(n, 0); dist[0] = 0; long long ans = 0; for (int step = 0; step < n; ++step) { int u = -1; for (int i = 0; i < n; ++i) { if (!vis[i] && (u == -1 || dist[i] < dist[u])) u = i; } vis[u] = 1; ans += dist[u]; for (int v = 0; v < n; ++v) { if (!vis[v] && g[u][v] < dist[v]) dist[v] = g[u][v]; } } cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 8735
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者