View Code of Problem 3818

#include<string>
#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
using namespace std;
//int a[1000][1000];

int main()
{
	long long n, m, k;
	while (cin >> n >> m >> k)
	{
		long long a[n + 1][n + 1];//vs编译不过,但是oj和dev是可以过的
		memset(a, 0, sizeof(a));
		
		while (m--)
		{
			long long x, y, z;
			cin >> x >> y >> z;
			a[x][y] = z;
			a[y][x] = z;
			//flag[x][y] = 1;
			//flag[y][x] = 1;
		}
		for (int k = 1;k <= n;k++)
		{
			for (int i = 1;i <= n;i++)
			{
				for (int j = 1;j <= n;j++)
				{
					if (a[i][k] != 0 && a[k][j] != 0)
					{
						if (a[i][j] != 0)
						{
							if (a[i][k] + a[k][j] < a[i][j] && k != i && k != j)
							{
								a[i][j] = a[i][k] + a[k][j];
							}
						}
						else
							a[i][j] = a[i][k] + a[k][j];
					}
				}
			}
		}
		while (k--)
		{
			long long int c, d;
			cin >> c >> d;
			cout << a[c][d] << endl;
		}
	}
}

Double click to view unformatted code.


Back to problem 3818