View Code of Problem 3818

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
	int a[500][500];
	for(int i=0;i<500;i++){
		for(int j=0;j<500;j++){
			if(i==j){
				a[i][j]=0;
			}
			else{
				a[i][j]=99999;
			}
		}
	}
	int n,m,k;
	while(cin>>n>>m>>k){
		for(int i=0;i<m;i++){
			int x,y,di;
			cin>>x>>y>>di;
			a[x][y]=di;
			a[y][x]=di;
		}
		for(int k=0;k<n;k++){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(a[i][j]>a[i][k]+a[k][j]){
						a[i][j]=a[i][k]+a[k][j];
					}
				}
			}
		}
		while(k--){
			int a1,b1;
			cin>>a1>>b1;
			cout<<a[a1][b1]<<endl;
		}
	}
}

Double click to view unformatted code.


Back to problem 3818