-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBOJ1506.cpp
89 lines (71 loc) · 1.48 KB
/
BOJ1506.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
vector<int> sccid(111, -1), discovered(111, -1);
stack<int> st;
vector<vector<int>> sccList(111);
int sccCount, vertexCount;
int adjMat[111][111], cost[111];
int N;
int scc(int here) {
discovered[here] = vertexCount;
++vertexCount;
int ret = discovered[here];
st.push(here);
for (int i = 1; i <= N; ++i) {
bool chk = adjMat[here][i];
int there = i;
if (chk && discovered[there] == -1) {
ret = min(ret, scc(there));
}
else if (chk && sccid[there] == -1 && here != there) {
ret = min(ret, discovered[there]);
}
}
if (ret == discovered[here]) {
while (1) {
int top = st.top();
st.pop();
sccid[top] = sccCount;
sccList[sccCount].push_back(top);
if (top == here) break;
}
++sccCount;
}
return ret;
}
void tarjan() {
sccCount = 0;
vertexCount = 0;
for (int i = 1; i <= N; ++i) {
if (discovered[i] == -1) scc(i);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
memset(adjMat, 0, sizeof(adjMat));
memset(cost, 0, sizeof(cost));
cin >> N;
for (int i = 1; i <= N; ++i) {
cin >> cost[i];
}
for (int i = 1; i <= N; ++i) {
string s;
cin >> s;
for (int j = 1; j <= N; ++j) {
adjMat[i][j] = s[j-1] - '0';
}
}
tarjan();
long long ans = 0;
for (int i = 0; i < sccCount; ++i) {
int tmp = INT_MAX;
for (int j = 0; j < sccList[i].size(); ++j) {
tmp = min(tmp, cost[sccList[i][j]]);
}
ans += tmp;
}
printf("%lld", ans);
return 0;
}