You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
987 B
C++
43 lines
987 B
C++
#include <iostream>
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cctype>
|
|
#include <cstring>
|
|
using namespace std;
|
|
const int inf = 0x3f3f3f3f;
|
|
const int maxn = 1010;
|
|
int G[maxn][maxn], to[maxn][maxn], tail[maxn], fa[maxn], f[maxn][maxn];
|
|
bool vis[maxn];
|
|
int n, l, r, mid, ans = inf;
|
|
void dfs(int x, int tmp, int step)
|
|
{
|
|
if (step == n)
|
|
ans = min(ans, tmp);
|
|
else for (int i = 1; i <= n; i++)
|
|
if ((!vis[i]) && (tmp + G[x][i] < ans))
|
|
{
|
|
vis[i] = true;
|
|
dfs(i, tmp + G[x][i], step + 1);
|
|
vis[i] = false;
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
freopen("tour.in", "r", stdin);
|
|
freopen("tour.out", "w", stdout);
|
|
memset(G, inf, sizeof(G));
|
|
scanf("%d", &n);
|
|
for (int i = 1, a, b, c; i < n; i++)
|
|
{
|
|
scanf("%d%d%d", &a, &b, &c);
|
|
G[a][b] = G[b][a] = c;
|
|
}
|
|
for (int k = 1; k <= n; k++)
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = 1; j <= n; j++)
|
|
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
|
|
vis[1] = true;
|
|
dfs(1, 0, 1);
|
|
cout << ans;
|
|
return 0;
|
|
} |