题目地址:http://ac.jobdu.com/problem.php?pid=1343
2 0 7 7 0 1 1 2 3 3 0 4 5 4 0 9 5 9 0 2 2 3 8 1 2 1
3 17 12
#include <stdio.h> #include <string.h> #define MAX 303 int N, K; int road[MAX][MAX]; int Min(int a, int b, int c){ int min = (a < b) ? a : b; return (min < c) ? min : c; } int main(void){ int i, j; int a, b, len, ans; while (scanf("%d", &N) != EOF){ for (i = 0; i < N; ++i) for (j = 0; j < N; ++j){ scanf("%d", &road[i][j]); road[j][i] = road[i][j]; } for (i = 0; i < N; ++i) road[i][i] = 0; scanf("%d", &K); while (K-- != 0){ scanf("%d%d%d", &a, &b, &len); --a; --b; for (i = 0; i < N; ++i){ for (j = 0; j < N; ++j){ //如果通过新修建的公路可以缩短最短路径则更新最短路径 road[i][j] = Min(road[i][a] + len + road[b][j], road[i][b] + len + road[a][j], road[i][j]); } } ans = 0; for (i = 1; i < N; ++i){ for (j = 0; j < i; ++j){ ans += road[i][j]; } } printf("%d\n", ans); } } return 0; }