我之前奇怪为什么网上别人的博客都没有给出各个步骤的详细解释。我试了一下,也懒得写了。以后有空或者理解够深了再补上吧
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
#define Max 0x3f3f3f3f
int road, near, want, total;
int Lu[1010][1010], Yi[1010], Hui[1010], Lisa[1010];
bool Zheng[1010];
void dijsktra(int start) //start为相邻的城市
{
int temp, k; //temp用处在于与数组Hui进行比较选出离start最近的点并记录为k
memset(Zheng, 0, sizeof(Zheng));
for (int i = 1; i <= total; i++)
Hui[i] = Lu[start][i];
Hui[start] = 0; Zheng[start] = 1;
for (int i = 1; i <= total; i++)
{
temp = Max;
for (int j = 1; j <= total; j++)
if (!Zheng[j] && temp > Hui[j])
temp = Hui[k = j];
Zheng[k] = 1;
for (int j = 1; j <= total; j++)
if (!Zheng[j] && Hui[j] > Hui[k] + Lu[k][j])
Hui[j] = Hui[k] + Lu[k][j];
}
}
int main()
{
int x;
int a, b, c;
while (cin >> road >> near >> want)
{
total = 0;
memset(Lu, Max, sizeof(Lu));
for (int i = 1; i <= road; i++)
{
cin >> a >> b >> c;
if (Lu[a][b] > c)
Lu[a][b] = Lu[b][a] = c;
total = max(total, max(a, b));
}
for (int i = 1; i <= near; i++)
cin >> Yi[i];
for (int i = 1; i <= want; i++)
cin >> Lisa[i];
int min = Max;
for (int i = 1; i <= near; i++)
{
x = Max;
dijsktra(Yi[i]);
for (int j = 1; j <= want; j++)
if (Hui[Lisa[j]] < x)
x = Hui[Lisa[j]];
if (min > x)
min = x;
}
cout << min << endl;
}
return 0;
}