题目大意,给你n个城市,m条道路,每条道路有最大承重量。问你要从城市1到城市n能接受的最大载重是多少。
是迪杰斯特拉算法变形,将松弛操作改成d[i]=max(d[i],min(d[v],mp[i][v])。还有相应的初始化的0和INF修改一下即可。
int d[MAX], t[MAX], mp[MAX][MAX];
int main()
{
int n, m, time;
cin >> time;
for (int kkk = 1; kkk <= time; kkk++)
{
scanf("%d%d", &n, &m);
fill(d, d + MAX, -1);
fill(t, t + MAX, 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == j)
mp[i][j] = INF;
else
mp[i][j] = -1;
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (mp[a][b] < c)
mp[a][b] = mp[b][a] = c;
}
d[1] = INF;
for (int l = 1; l <= n; l++)
{
int v;
int minn = -1;
for (int i = 1; i <= n; i++)
if (!t[i] && d[i] > minn)
{
minn = d[i];
v = i;
}
t[v] = 1;
for (int i = 1; i <= n; i++)
d[i] = max(d[i], min(d[v], mp[v][i]));
}
printf("Scenario #%d:\n%d\n\n", kkk, d[n]);
}
return 0;
}