本文共 1388 字,大约阅读时间需要 4 分钟。
WA
1. 函数体内用的是从 1 开始的数组, 而接受的 input 是从 0 开始的, 又是好2的错误
代码
#include #include #include #include #include #include #include #include #include #include #define MIN(x,y) (x)<(y)?(x):(y)using namespace std;int matrix[100][100];int totalCost(int n) { int sum = 0; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) { sum += matrix[i][j]; } } return sum/2;}int updateMatrix(int n, int from, int to, int newCost) { matrix[from][to] = newCost; matrix[to][from] = newCost; for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) { int dist1 = matrix[i][from] + matrix[to][j] + newCost; int dist2 = matrix[i][to] + matrix[from][j] + newCost; matrix[i][j] = min(matrix[i][j], dist1); matrix[i][j] = min(matrix[i][j], dist2); printf("matrix[%d][%d] = %d\n",i, j, matrix[i][j]); } } return totalCost(n);}int main() { freopen("C:\\Users\\vincent\\Dropbox\\workplacce\\joj\\test.txt", "r", stdin); int n; while(scanf("%d", &n) != EOF) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) { scanf("%d", &matrix[i][j]); } } int m; scanf("%d", &m); for(int i = 0; i < m; i ++) { int from, to, cost; scanf("%d%d%d", &from, &to, &cost); int res = updateMatrix(n, from, to, cost); printf("%d\n", res); } } return 0;}
转载于:https://www.cnblogs.com/zhouzhuo/p/3679294.html