-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP107.cpp
More file actions
65 lines (58 loc) · 1.29 KB
/
Copy pathP107.cpp
File metadata and controls
65 lines (58 loc) · 1.29 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* 最小生成树
* (自行将 txt 中的 "," 替换成空格)
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
char Inp[50];
int char2int(){
if(Inp[0]=='-') return INF;
int ret=0;
for(int i=0;Inp[i]!='\0';i++)
ret=ret*10+Inp[i]-'0';
return ret;
}
int n=40;
int cost[50][50];
int lowc[50];
bool vis[50];
int Prim(){
int ans=0;
vis[1]=1;
for(int i=2;i<=n;i++) lowc[i]=cost[1][i];
for(int i=2;i<=n;i++){
int minc=INF;
int p=-1;
for(int j=1;j<=n;j++){
if(!vis[j] && minc>lowc[j]){
minc=lowc[j];
p=j;
}
}
if(minc==INF) return -1;
ans+=minc;
vis[p]=1;
for(int j=1;j<=n;j++)
if(!vis[j]&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return ans;
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
freopen("p107_network.txt","r",stdin);
int tot=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%s",Inp);
cost[i][j]=char2int();
if(i==j) cost[i][j]=0;
else if(cost[i][j]<INF)
tot+=cost[i][j];
}
}
tot/=2;
printf("%d\n",tot-Prim());
}