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
| #include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#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;
struct SCC {
int n;
vector<vector<int> > g, rg, ng, scc; // rg: 逆グラフ, ng: 分解後のグラフ
vector<int> res; // scc: 強連結成分に属する頂点, res:強連結成分の番号
bool used[100005];
SCC(int _n) {
n = _n;
g.resize(n); rg.resize(n) ; scc.resize(n); res.resize(n);
}
SCC(const vector<vector<int> > &g) : n(g.size()), g(g), rg(n), scc(n), res(n) {
rep(i, n) {
rep(j, g[i].size()) rg[g[i][j]].push_back(i);
}
}
// i-jに辺を追加する
void add(int i, int j) {
g[i].push_back(j);
rg[j].push_back(i);
}
vector<int> vs;
void dfs(int v) {
used[v] = true;
rep(i, g[v].size()) {
if(!used[ g[v][i] ]) dfs(g[v][i]);
}
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
res[v] = k;
scc[k].push_back(v);
rep(i, rg[v].size()) {
if(!used[ rg[v][i] ]) rdfs(rg[v][i], k);
}
}
void ng_make(int k) {
ng.resize(k);
rep(i, k) {
set<int> st;
rep(j, scc[i].size()) {
int ii = scc[i][j];
rep(l, g[ii].size()) {
int ll = g[ii][l];
int id = res[ll];
if(i == id) continue;
st.insert(id);
}
}
vector<int> v(st.begin(), st.end());
ng[i] = v;
}
}
int run() {
memset(used, 0, sizeof(used));
rep(i, n) {
if (!used[i]) dfs(i);
}
memset(used, 0, sizeof(used));
int k = 0;
for (int i = vs.size()-1; i >= 0; i--) {
if (!used[vs[i]]) rdfs(vs[i], k++);
}
ng_make(k);
return k;
}
};
class AntarcticaPolice {
public:
double minAverageCost(vector <int> cost, vector <string> roads) {
int n = cost.size();
SCC scc(n);
rep(i, n) {
rep(j, n) {
if(roads[i][j] == 'Y') {
scc.add(i, j);
}
}
}
int k = scc.run();
vector<int> res(k, INF), res_id(k, -1);
rep(i, n) {
int j = scc.res[i];
if(res[j] > cost[i]) {
res[j] = cost[i];
res_id[j] = i;
}
}
vector<int> out(k);
rep(i, k) {
rep(j, scc.ng[i].size()) {
int l = scc.ng[i][j];
out[l]++;
}
}
int sum = 0, cnt = 0;
vector<int> ret;
rep(i, k) {
if(out[i] == 0) {
sum += res[i];
cnt++;
} else {
ret.push_back(res[i]);
}
}
set<int> id;
rep(i, k) id.insert(res_id[i]);
rep(i, n) {
if(id.find(i) == id.end()) {
ret.push_back(cost[i]);
}
}
sort(ret.begin(), ret.end());
rep(i, ret.size()) {
double a = double(sum) / cnt;
double b = double(sum + ret[i]) / (cnt + 1);
if(a > b) {
sum += ret[i];
cnt++;
} else break;
}
return double(sum) / cnt;
}
};
|