用c++解决1160. Network Time limit: 1.0 second Memory limit: 64 MB Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem - not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections. You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied. Input The first line contains two integer: N - the number of hubs in the network (2 ≤ N ≤ 1000) and M — the number of possible hub connections (1 ≤ M ≤ 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs. Output Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
时间: 2024-02-12 22:08:58 浏览: 146
以下是使用 C++ 解决 1160. Network 问题的代码:
```cpp
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 1005;
const int MAXM = 15005;
const int INF = 0x3f3f3f3f;
struct Edge {
int to, w, next;
} edges[MAXM * 2];
int head[MAXN], cnt = 0;
void addEdge(int u, int v, int w) {
edges[++cnt].to = v;
edges[cnt].w = w;
edges[cnt].next = head[u];
head[u] = cnt;
}
int n, m;
int d[MAXN]; // 存储每个点到最小生成树的距离
bool vis[MAXN];
int parent[MAXN]; // 存储最小生成树中每个节点的父节点
int max_len = -1;
void prim() {
memset(d, INF, sizeof(d));
memset(vis, false, sizeof(vis));
memset(parent, -1, sizeof(parent));
d[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push(make_pair(0, 1));
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
int w = edges[i].w;
if (!vis[v] && d[v] > w) {
d[v] = w;
q.push(make_pair(d[v], v));
parent[v] = u;
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
addEdge(u, v, w);
addEdge(v, u, w);
}
prim();
vector<pair<int, int>> ans;
for (int i = 2; i <= n; i++) {
ans.push_back(make_pair(i, parent[i]));
max_len = max(max_len, d[i]);
}
cout << max_len << endl;
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
return 0;
}
```
解释如下:
首先,我们定义一个边结构体 `Edge`,其中包括两个属性:`to` 表示边指向哪个节点,`w` 表示边的权值。然后,我们定义一个 `head` 数组,用于存储每个节点的第一条边。
接着,我们编写 `addEdge` 函数,用于加边。由于是无向图,所以需要加两次边。
在 `main` 函数中,我们首先读入节点数 `n` 和边数 `m`,然后读入每条边,并调用 `addEdge` 函数加边。
接下来,我们调用 `prim` 函数计算最小生成树。具体过程如下:
1. 初始化 `d` 数组为正无穷,`vis` 数组为 `false`,`parent` 数组为 `-1`,并将 `d[1]` 设为 0。
2. 将 `(0, 1)` 加入优先队列 `q`。
3. 当 `q` 不为空时,取出队首元素 `(dist, u)`,如果 `u` 已被访问过,则继续取队首元素。
4. 将 `u` 标记为已访问。
5. 遍历 `u` 的所有相邻节点 `v`,如果 `v` 未被访问过且 `d[v] > w(u, v)`,则更新 `d[v]` 为 `w(u, v)`,将 `(w(u, v), v)` 加入优先队列,并将 `parent[v]` 设为 `u`。
最后,我们遍历所有非根节点,并将它们与它们的父节点加入答案数组 `ans`,同时更新最大边长 `max_len`。最后输出答案即可。
注意,本题中所有边权均为正整数,因此我们可以将 `d` 数组初始化为正无穷,而不是负无穷。
阅读全文