简单题
View Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 1005
int n, m;
bool vis[maxn];
int cost[maxn][maxn];
int weight[maxn];
void input()
{
scanf("%d%d", &n, &m);
memset(cost, 0, sizeof(cost));
for (int i = 0; i < m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;
b--;
cost[a][b] = c;
cost[b][a] = c;
}
}
int dijkstra()
{
int pre = 0;
memset(vis, 0, sizeof(vis));
vis[0] = true;
memset(weight, 0, sizeof(weight));
weight[0] = 0x3f3f3f3f;
while (1)
{
for (int i = 0; i < n; i++)
if (!vis[i] && min(weight[pre],cost[pre][i]) > weight[i])
weight[i] = min(weight[pre],cost[pre][i]);
int best = -1, besti = -1;
for (int i = 0; i < n; i++)
if (!vis[i] && weight[i] > best)
{
best = weight[i];
besti = i;
}
if (besti == -1)
break;
vis[besti] = true;
pre = besti;
}
return weight[n - 1];
}
int main()
{
//freopen("t.txt", "r", stdin);
int t;
scanf("%d", &t);
for (int i = 0; i < t; i++)
{
input();
int ans = dijkstra();
printf("Scenario #%d:\n%d\n\n", i + 1, ans);
}
return 0;
}