USACO Agri-Net

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
ID: wcr19961
PROG: agrinet
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxn = 110;
int d[maxn][maxn], len[maxn], ans;
bool vis[maxn];

int main() {
freopen("agrinet.in", "r", stdin);
freopen("agrinet.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
len[i] = inf;
for (int j = 1; j <= n; ++j) {
scanf("%d", &d[i][j]);
}
}
int u = 1, cnt = n - 1;
while (cnt--) {
vis[u] = true;
int v = -1, best = inf;
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
len[i] = min(len[i], d[u][i]);
if (len[i] < best) {
best = len[i];
v = i;
}
}
}
ans += best;
u = v;
}
printf("%d\n", ans);
return 0;
}