#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 107; const int INF = 0x3f3f3f3f; typedef long long ll; int graph[MAXN][MAXN]; int main() { int n, e; while (~scanf("%d %d", &n, &e)) { memset(graph, INF, sizeof(graph)); for (int i = 1; i <= e; i++) { int from, to, v; scanf("%d %d %d", &from, &to, &v); ++from, ++to; if (from == to) { graph[from][to] = 0; } else { graph[from][to] = v; graph[to][from] = v; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (graph[i][j] > graph[i][k] + graph[k][j]) { graph[i][j] = graph[i][k] + graph[k][j]; } } } } ll dis[MAXN] = {0}; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i != j) { dis[i] += (ll)graph[i][j]; } } } ll min_dis = INF; int min_pos = 1; for (int i = 1; i <= n; i++) { if (dis[i] < min_dis) { min_dis = dis[i]; min_pos = i; } } --min_pos; printf("%d\n", min_pos); } return 0; }帮我把这段代码转换为c++
时间: 2023-12-03 12:43:48 浏览: 147
在这里提供一份已经是 C++ 代码的版本,可以直接使用:
```cpp
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 107;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int graph[MAXN][MAXN];
int main() {
int n, e;
while (cin >> n >> e) {
memset(graph, INF, sizeof(graph));
for (int i = 1; i <= e; i++) {
int from, to, v;
cin >> from >> to >> v;
++from, ++to;
if (from == to) {
graph[from][to] = 0;
} else {
graph[from][to] = v;
graph[to][from] = v;
}
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (graph[i][j] > graph[i][k] + graph[k][j]) {
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}
ll dis[MAXN] = {0};
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
dis[i] += (ll)graph[i][j];
}
}
}
ll min_dis = INF;
int min_pos = 1;
for (int i = 1; i <= n; i++) {
if (dis[i] < min_dis) {
min_dis = dis[i];
min_pos = i;
}
}
--min_pos;
cout << min_pos << endl;
}
return 0;
}
```
阅读全文