-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1212.cpp
More file actions
81 lines (69 loc) · 1.48 KB
/
1212.cpp
File metadata and controls
81 lines (69 loc) · 1.48 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*************************************************************************
> File Name: 1212.cpp
> Author: dulun
> Mail: [email protected]
> Created Time: 2015年12月03日 星期四 19时03分24秒
************************************************************************/
#include<iostream>
#include<stdio.h>
#include<cstdio>
using namespace std;
int a[1009][1009] = {0};
const int maxv = 999999;
int adj[1008];
int low[1008];
int main()
{
int n, m;
cin>>n>>m;
for(int i = 0; i < n+1; i++)
{
for(int j = 0; j < n+1; j++)
a[i][j] = maxv;
}
for(int i = 0; i < n+1; i++)
a[i][i] = 0;
int x, y,w;
for(int i = 0; i < m; i++)
{
//cin>>x>>y>>w;
scanf("%d%d%d", &x, &y, &w);
a[x][y] = w;
a[y][x] = w;
}
for(int i = 0; i < n+1; i++)
{
low[i] = a[1][i];
adj[i] = 0;
}
low[0] = 0;
int count = 0;
int sum = 0;
int k;
int min;
while(count < n-1)
{
min = maxv;
for(int i = 1; i < n+1; i++)
{
if(low[i] < min && low[i] != 0)
{
k = i;
min = low[i];
}
}
sum += low[k];
low[k] = 0;
for(int i = 1; i < n+1; i++)
{
if(low[i] > a[k][i] && low[i] != 0)
{
low[i] = a[k][i];
adj[i] = k;
}
}
count++;
}
cout<<sum;
return 0;
}