WA
1. 函数体内用的是从 1 开始的数组, 而接受的 input 是从 0 开始的, 又是好2的错误
代码
#include <iostream> #include <stdio.h> #include <memory.h> #include <algorithm> #include <vector> #include <map> #include <set> #include <string> #include <deque> #include <cstring> #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 ",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 ", res); } } return 0; }