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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
| #include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<string> split(const string &str, char delim) {
vector<string> res;
size_t current = 0, found;
while((found = str.find_first_of(delim, current)) != string::npos) {
res.push_back(string(str, current, found - current));
current = found + 1;
}
res.push_back(string(str, current, str.size() - current));
return res;
}
int f(string s) {
int ret;
stringstream ss(s);
ss >> ret;
return ret;
}
struct UnionFind {
vector<int> par, rank;
int N;
UnionFind(int n) {
N = n;
par.resize(n);
rank.resize(n);
rep(i, n) {
par[i] = i;
rank[i] = 0;
}
}
int find(int x) {
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return;
if(rank[x] < rank[y]) {
par[x] = y;
} else {
par[y] = x;
if(rank[x] == rank[y]) rank[x]++;
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size() {
int cnt = 0;
rep(i, N) if(find(i) == i) cnt++;
return cnt;
}
};
struct edge {
int from,to;
int cost;
edge(int t,int c) : to(t),cost(c) {}
edge(int f,int t,int c) : from(f),to(t),cost(c) {}
bool operator<(const edge &e) const {
return cost < e.cost;
}
};
vector<edge> G[55];
int dist[55][55];
int kruskal(int n, vector<edge> v) {
sort(v.begin(),v.end());
UnionFind uf(n);
rep(i, 55) G[i].clear();
rep(i, 55) rep(j, 55) dist[i][j] = INF;
int ret = 0;
rep(i, v.size()) {
edge e = v[i];
if(!uf.same(e.from,e.to)) {
uf.unite(e.from,e.to);
ret += e.cost;
G[e.from].push_back(edge(e.to, e.cost));
G[e.to].push_back(edge(e.from, e.cost));
dist[e.from][e.to] = e.cost;
dist[e.to][e.from] = e.cost;
// cout << e.from << " -- " << e.to << " [label = \"" << e.cost << "\"];" << endl;
}
}
return ret;
}
bool O[55];
int dp[55]; // 部分木iを消す時の最善
int n;
int dfs2(int cur, int pre) {
int cnt = 0;
rep(i, G[cur].size()) {
edge e = G[cur][i];
if(e.to == pre) continue;
cnt++;
}
if(cnt == 0) {
if(O[cur]) {
dp[cur] = dist[cur][pre];
return 0;
} else {
dp[cur] = 0;
return 0;
}
}
vector<int> v;
int ret = 0;
rep(i, G[cur].size()) {
edge e = G[cur][i];
if(e.to == pre) continue;
int x = dfs2(e.to, cur);
ret += x;
int res = min(dp[e.to], dist[e.to][cur]);
if(res == 0) continue;
v.push_back(res);
}
if(O[cur]) {
int sum = 0;
rep(i, v.size()) {
sum += v[i];
}
return sum + ret;
} else {
if(v.size() == 0) {
dp[cur] = 0;
return ret;
}
else if(v.size() == 1) {
dp[cur] = v[0];
return ret;
} else {
sort(v.begin(), v.end());
int sum = 0;
rep(i, v.size()-1) {
sum += v[i];
}
dp[cur] = v[v.size()-1];
return sum + ret;
}
}
}
class BlockEnemy {
public:
int minEffort(int N, vector <string> roads, vector <int> occupiedTowns) {
n = N;
memset(O, 0, sizeof(O));
rep(i, occupiedTowns.size()) {
O[occupiedTowns[i]] = true;
}
rep(i, 55) dp[i] = INF;
vector<edge> E;
rep(i, roads.size()) {
vector<string> ret = split(roads[i], ' ');
int a = f(ret[0]);
int b = f(ret[1]);
int e = f(ret[2]);
E.push_back(edge(a, b, e));
}
kruskal(N, E);
return dfs2(0, -1);
}
};
|