AcWing 1140. 最短网络
原题链接
简单
作者:
Value
,
2020-10-12 22:53:24
,
所有人可见
,
阅读 435
#include <iostream>
#include <cstring>
#define inf 0x3f3f3f
using namespace std;
const int N = 110;
int graph[N][N];
int n;
typedef long long ll;
ll res = 0;
int dist[N];
bool st[N];
void read(){
cin >> n;
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ ) cin >> graph[i][j];
}
}
void prim(){
memset(dist, inf, sizeof dist);
dist[1] = 0;
for(int i = 1; i <= n; i ++ ){
int nxt = -1;
for(int j = 1; j <= n; j ++ ){
if(!st[j] && (nxt == -1 || dist[nxt] > dist[j])) nxt = j;
}
st[nxt] = true;
res += dist[nxt];
for(int i = 1; i <= n; i ++ ){
dist[i] = min(dist[i], graph[nxt][i]);
}
}
}
int main(){
read();
prim();
cout << res << endl;
return 0;
}