View Code of Problem 3818

#include<stdio.h>
#define max 999999;
int main(){
	int n, m, k, i, j,s,x,y,z;
	int a[100][100];
	while (scanf("%d%d%d", &n, &m, &k) != EOF) {
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				if (i == j) {
					a[i][j] = 0;
				}
				else {
					a[i][j] = max;
				}
			}
		}
		for (i = 0; i < m; i++) {
			scanf("%d%d%d", &x, &y,&z);
			a[x][y] = z;
			a[y][x] = z;
		}
		for (i = 0; i < n; i++) {
			for (j = 0; j < n; j++) {
				for (s = 0; s < n; s++) {
					if (a[j][s] > a[j][i] + a[i][s]) {
						a[j][s] = a[j][i] + a[i][s];
					}
				}
			}
		}
		for (i = 0; i < k; i++) {
			scanf("%d%d", &x, &y);
			printf("%d\n", a[x][y]);
		}
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3818