In order to prepare the “The First National ACM School Contest” (in 20??) the major of the citydecided to provide all the schools with a reliable source of power. (The major is really afraid ofblackoutsJ). So, in order to do that, power station “Future” and one school (doesn’t matter which one)must be connected; in addition, some schools must be connected as well.
You may assume that a school has a reliable source of power if it’s connected directly to “Future”,or to any other school that has a reliable source of power. You are given the cost of connection betweensome schools. The major has decided to pick out two the cheapest connection plans – the cost of theconnection is equal to the sum of the connections between the schools. Your task is to help the major— find the cost of the two cheapest connection plans.
Input
The Input starts with the number of test cases, T (1 < T < 15) on a line. Then T test cases follow. Thefirst line of every test case contains two numbers, which are separated by a space, N (3 < N < 100)the number of schools in the city, and M the number of possible connections among them. Next Mlines contain three numbers Ai, Bi, Ci, where Ciis the cost of the connection (1 < Ci < 300) betweenschools Ai and Bi. The schools are numbered with integers in the range 1 to N.
Output
For every test case print only one line of output. This line should contain two numbers separated by asingle space – the cost of two the cheapest connection plans. Let S1 be the cheapest cost and S2 thenext cheapest cost. It’s important, that S1 = S2 if and only if there are two cheapest plans, otherwiseS1 < S2. You can assume that it is always possible to find the costs S1 and S2.
Sample Input
2
5 8
1 3 75
3 4 51
2 4 19
3 2 95
2 5 42
5 4 31
1 2 9
3 5 66
9 14
1 2 4
1 8 8
2 8 11
3 2 8
8 9 7
8 7 1
7 9 6
9 3 2
3 4 7
3 6 4
7 6 2
4 6 14
4 5 9
5 6 10
Sample Output
110 121
37 37
#include <bits/stdc++.h>
using namespace std;
const int mn = 110;
const int inf = 0x7fffffff;
int cost[mn][mn];
bool vis[mn];
int dis[mn];
int f[mn];
int far[mn][mn];
bool is[mn][mn];
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i != j)
cost[i][j] = inf;
for (int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
cost[a][b] = c;
cost[b][a] = c;
}
memset(is, 0, sizeof is);
memset(vis, 0, sizeof vis);
memset(far, 0, sizeof far);
dis[1] = 0;
vis[1] = 1;
for (int i = 1; i <= n; i++)
{
f[i] = 1;
dis[i] = cost[1][i];
}
int fr = 0;
while (1)
{
int t = -1;
for (int i = 1; i <= n; i++)
if (!vis[i] && (t == -1 || dis[i] < dis[t]))
t = i;
if (t == -1)
break;
vis[t] = 1;
is[f[t]][t] = is[t][f[t]] = 1;
fr += dis[t];
for (int i = 1; i <= n; i++)
{
if (!vis[i] && cost[t][i] < dis[i])
{
f[i] = t;
dis[i] = cost[t][i];
}
else if (vis[i] && i != t)
{
far[i][t] = max(far[i][f[t]], cost[f[t]][t]);
far[t][i] = far[i][t];
}
}
}
int sc = inf;
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
if (!is[i][j] && cost[i][j] != inf)
sc = min(sc, fr - far[i][j] + cost[i][j]);
printf("%d %d\n", fr, sc);
}
return 0;
}