把这段代码从C语言改为C++语言 for(a = 0; a < n; ++a) for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) if(D[i][a] != -1 && D[a][j] != -1) if(D[i][j] == -1 || D[i][a] + D[a][j] < D[i][j]) D[i][j] = D[i][a] + D[a][j]; //Preprocess int canArr[n][n]; for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) canArr[i][j] = 0; for(i = 0; i < n; ++i) { for(j = 0; j < m*2; ++j) if(D[i][terminal[j]] != -1) canArr[i][terminal[j]] = 1; for(j = 0; j < n; ++j) { if(j != i) { if(D[i][j] == -1) continue; for(a = 0; a < n; ++a) if(a != i && a != j) if(D[i][a] != -1 && D[i][a] / k == D[i][j] / k && D[i][a] > D[i][j]) break; if(a == n) canArr[i][j] = 1; } } }
时间: 2024-03-09 16:48:08 浏览: 89
在C++中,可以使用vector代替数组,同时可以使用range-based for循环。以下是将此代码转换为C++的方式:
```
for(int a = 0; a < n; ++a)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(D[i][a] != -1 && D[a][j] != -1)
if(D[i][j] == -1 || D[i][a] + D[a][j] < D[i][j])
D[i][j] = D[i][a] + D[a][j];
//Preprocess
vector<vector<int>> canArr(n, vector<int>(n, 0));
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < m*2; ++j)
if(D[i][terminal[j]] != -1)
canArr[i][terminal[j]] = 1;
for(int j = 0; j < n; ++j)
{
if(j != i)
{
if(D[i][j] == -1)
continue;
int a;
for(a = 0; a < n; ++a)
if(a != i && a != j)
if(D[i][a] != -1 && D[i][a] / k == D[i][j] / k && D[i][a] > D[i][j])
break;
if(a == n)
canArr[i][j] = 1;
}
}
}
```
请注意,由于在C++中,vector的大小可以在运行时动态分配,因此不需要在编译时指定数组大小。
阅读全文