-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2001.cpp
More file actions
95 lines (70 loc) · 1.66 KB
/
2001.cpp
File metadata and controls
95 lines (70 loc) · 1.66 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include <string>
using namespace std;
struct T
{
int idx, jewel;
};
typedef pair<int, int>P;
int N, M, K;
int jewelIsland[15];
int visited[101][1 << 14];
int maxV = 0;
vector<P>adj[101];
int main(void)
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> N >> M >> K;
for (int i = 0; i < K; i++)cin >> jewelIsland[i];
for (int i = 0; i < M; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back(P(b, c));
adj[b].push_back(P(a, c));
}
queue<T> q;
q.push(T{ 1,0 });
for (int i = 0; i < K; i++) {
if (jewelIsland[i] == 1) {
q.push(T{ 1,1 << (i) });
}
}
while (!q.empty()) {
T curr = q.front(); q.pop();
int count = 0;
for (int i = 0; i < K; i++) {
if ((curr.jewel >> i) & 1)count++;
}
if (curr.idx == 1 && visited[curr.idx][curr.jewel] == 1) {
if (maxV < count)maxV = count;
continue;
}
for (auto next : adj[curr.idx]) {
//방문여부
if (visited[next.first][curr.jewel])continue;
//무게 초과 여부
if (next.second < count)continue;
int isFlag = 0;
int isJewel = 0;
for (int i = 0; i < K; i++) {
if (jewelIsland[i] == next.first) {
isFlag = 1;
isJewel = i;
break;
}
}
//보석섬이든 아니든 보석을 안줍는 경우
visited[next.first][curr.jewel] = 1;
q.push(T{ next.first,curr.jewel });
if (isFlag != 0) {
visited[next.first][curr.jewel | (1 << isJewel)] = 1;
q.push(T{ next.first,curr.jewel | (1 << isJewel) });
}
}
}
cout << maxV;
}