forked from carlfriess/MinSpantree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinSpantree.m
160 lines (113 loc) · 3.17 KB
/
MinSpantree.m
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// main.m
// MinSpantree
//
// Created by Carl Friess on 10/05/2017.
// Copyright © 2017 Carl Friess. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Scanner : NSObject
{
NSFileHandle* source;
NSScanner* buffer;
}
- (int)nextInt;
@end
@implementation Scanner
- (instancetype)init
{
self = [super init];
if (self) {
source = [NSFileHandle fileHandleWithStandardInput];
}
return self;
}
- (int)nextInt
{
if (buffer == nil || buffer.atEnd) {
NSString* nextInput = [[NSString alloc] initWithData:source.availableData encoding:NSUTF8StringEncoding];
if (nextInput != nil) {
buffer = [[NSScanner alloc] initWithString: nextInput];
}
}
int value = -1;
[buffer scanInt:&value];
return value;
}
@end
@interface Vertex : NSObject
{
int value;
Vertex* parent;
}
@property Vertex* parent;
- (Vertex*)getRoot;
@end
@implementation Vertex
- (instancetype)initWithValue:(int)defaultValue
{
self = [super init];
if (self) {
value = defaultValue;
parent = self;
}
return self;
}
@synthesize parent;
-(Vertex *)getRoot
{
return parent == self ? self : [parent getRoot];
}
@end
@interface Edge : NSObject
{
Vertex* u;
Vertex* v;
int weight;
}
@property Vertex* u;
@property Vertex* v;
@property int weight;
@end
@implementation Edge
@synthesize u;
@synthesize v;
@synthesize weight;
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Scanner* scanner = [[Scanner alloc] init];
for (int numTests = [scanner nextInt]; numTests > 0; numTests--) {
int numVertices = [scanner nextInt];
int numEdges = [scanner nextInt];
NSMutableArray* vertices = [NSMutableArray arrayWithCapacity:numVertices];
NSMutableArray* edges = [NSMutableArray arrayWithCapacity:numEdges];
for (int i = 0; i < numVertices; i++) {
[vertices addObject:[[Vertex alloc]initWithValue:i]];
}
for (int i = 0; i < numEdges; i++) {
int uValue = [scanner nextInt] - 1;
int vValue = [scanner nextInt] - 1;
Edge* edge = [[Edge alloc] init];
edge.u = vertices[uValue];
edge.v = vertices[vValue];
edge.weight = [scanner nextInt];
[edges addObject:edge];
}
[edges sortUsingComparator:^NSComparisonResult(Edge* first, Edge* second) {
return first.weight > second.weight;
}];
int totalWeight = 0;
Edge* edge;
for (edge in edges) {
if ([edge.u getRoot] != [edge.v getRoot]) {
[edge.v getRoot].parent = [edge.u getRoot];
totalWeight += edge.weight;
}
}
printf("%d\n", totalWeight);
}
}
return 0;
}