-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1078_t.cpp
More file actions
56 lines (52 loc) · 1.28 KB
/
1078_t.cpp
File metadata and controls
56 lines (52 loc) · 1.28 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
/*************************************************************************
> File Name: 1078_t.cpp
> Author: dulun
> Mail: [email protected]
> Created Time: 2016年05月14日 星期六 20时18分50秒
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 586;
int a[N][N];
bool v[N];
int d[N];
int n;
int prim()
{
memset(v, false, sizeof(v));
memset(d, 0x3f3f3f3f, sizeof(d));
int ans = 0;
int k = 0, min = 0x3f3f3f3f;
v[0] = true;
for(int i = 1; i < n; i++) // 控制,一共找n-1条边
{
for(int j = 0; j < n; j++) //更新与k点相连的边
if(!v[j] && d[j] > a[j][k])
d[j] = a[j][k];
for(int j = 0; j < n; j++) //找当前预备点中离组织最近的一条边
if(!v[j] && d[j] < min)
min = d[k=j];
v[k] = 1;
ans += min;
min = 0x3f3f3f3f;
}
return ans;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("%d\n", prim());
return 0;
}