#include <algorithm> #include <cstdio> #include <map> #include <queue> using namespace std; const int maxn = 205; const int INF = 0x3f3f3f3f; int d[maxn][maxn]; int terminal[maxn], vis[maxn][maxn]; map<int, int> been[maxn]; int n, m, k; int line[10000]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d[i][j] = (i == j) ? 0 : INF; int u, v, len; int fare; char ch; while (m--) { int len = 0; while (scanf("%d", &u)) { line[len++] = u; ch = getchar(); if (ch == '\n') { terminal[line[0]] = terminal[line[len - 1]] = 1; for (int i = 0; i != len - 1; i += 2) { u = line[i], v = line[i + 2]; d[v][u] = d[u][v] = min(d[u][v], line[i + 1]); } break; } } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j || d[i][j] == INF) continue; fare = 2 + d[i][j] / k; if (!been[i].count(fare) || been[i][fare] < d[i][j]) been[i][fare] = d[i][j]; } } int t, cur, first; queue<int> Q; scanf("%d", &t); while (t--) { first = 1; scanf("%d", &u); vis[u][u] = 1; Q.push(u); while (!Q.empty()) { cur = Q.front(); Q.pop(); for (int i = 1; i <= n; i++) { if (vis[u][i] || d[cur][i] == INF) continue; if (terminal[i]) { Q.push(i); vis[u][i] = 1; } else { fare = 2 + d[cur][i] / k; if (d[cur][i] == been[cur][fare]) { Q.push(i); vis[u][i] = 1; } } } } for (int i = 1; i <= n; i++) { if (vis[u][i]) { if (first) { printf("%d", i); first = 0; } else printf(" %d", i); } } printf("\n"); } return 0; }把这段代码改为C语言代码
时间: 2024-03-08 09:50:01 浏览: 74
下面是将该段代码改为C语言的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define maxn 205
#define INF INT_MAX
int d[maxn][maxn];
int terminal[maxn], vis[maxn][maxn];
int been[maxn][maxn];
int n, m, k;
int line[10000];
int main() {
scanf("%d%d%d", &n, &m, &k);
// 初始化距离矩阵
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
d[i][j] = (i == j) ? 0 : INF;
}
}
// 读入边并建图
int u, v, len, fare;
char ch;
for (int i = 0; i < m; i++) {
len = 0;
while (scanf("%d", &u)) {
line[len++] = u;
ch = getchar();
if (ch == '\n') {
terminal[line[0]] = terminal[line[len - 1]] = 1;
for (int i = 0; i != len - 1; i += 2) {
u = line[i], v = line[i + 2];
d[v][u] = d[u][v] = (d[u][v] > line[i + 1]) ? line[i + 1] : d[u][v];
}
break;
}
}
}
// Floyd算法求最短路
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (d[i][k] != INF && d[k][j] != INF && d[i][j] > d[i][k] + d[k][j]) {
d[i][j] = d[i][k] + d[k][j];
}
}
}
}
// 记录每个站点到其他站点的最小票价
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == j || d[i][j] == INF) {
continue;
}
fare = 2 + d[i][j] / k;
if (been[i][fare] < d[i][j]) {
been[i][fare] = d[i][j];
}
}
}
// 处理查询
int t, cur, first;
scanf("%d", &t);
while (t--) {
first = 1;
scanf("%d", &u);
vis[u][u] = 1;
for (int i = 1; i <= n; i++) {
if (terminal[i]) {
vis[u][i] = 1;
} else {
fare = 2 + d[u][i] / k;
if (d[u][i] == been[i][fare]) {
vis[u][i] = 1;
}
}
}
for (int i = 1; i <= n; i++) {
if (vis[u][i]) {
if (first) {
printf("%d", i);
first = 0;
} else {
printf(" %d", i);
}
}
}
printf("\n");
}
return 0;
}
```
阅读全文