-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
17-yuyu0830
- Loading branch information
Showing
2 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <iostream> | ||
#include <queue> | ||
|
||
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); | ||
|
||
using namespace std; | ||
|
||
int f() { | ||
int n, m, t; | ||
cin >> n >> m; | ||
|
||
deque<bool> visited(n + 1, 0); | ||
vector<int> arr(n + 1, 0), enter(n + 1, 0), rule[n + 1]; | ||
|
||
// 건물 μ§λ μλ | ||
for (int i = 1; i <= n; i++) | ||
cin >> arr[i]; | ||
|
||
// 건물 μμ | ||
for (int i = 0; i < m; i++) { | ||
int a, b; cin >> a >> b; | ||
rule[a].push_back(b); | ||
enter[b]++; | ||
} | ||
|
||
cin >> t; | ||
|
||
int ret = 0; | ||
|
||
vector<int> tmp[2]; | ||
bool e = false; | ||
|
||
// μνλ 건물 μ§μ μ μμ λκΉμ§ λ°λ³΅ | ||
while (true) { | ||
// ret μκ°μ μ§μ μ μλ κ±΄λ¬Όλ€ νμ λ° tmp νμ push | ||
// μνλ 건물μ μ§μ μ μμΌλ©΄ ν¨μ μ’ λ£ λ° κ° λ°ν | ||
for (int i = 1; i <= n; i++) { | ||
if (!enter[i] && !visited[i]) { | ||
if (i == t) return ret + arr[i]; | ||
|
||
visited[i] = true; | ||
tmp[e].push_back(i); | ||
} | ||
} | ||
|
||
int m = 99999999; | ||
|
||
// μ§κΈ μ§κ³ μλ 건물 + μ§μ μ μλ 건물 μ€ μκ° κ°μ₯ μ κ² λ¨μ κ° νμ | ||
for (int i : tmp[e]) { m = min(m, arr[i]); } | ||
|
||
// tmp νμ μλ λͺ¨λ κ±΄λ¬Όλ€ mλ§νΌ κ±΄μ€ μ§ν, μμ±λλ©΄ νμμ μ κ±° | ||
while (!tmp[e].empty()) { | ||
int a = tmp[e].back(); | ||
tmp[e].pop_back(); | ||
|
||
arr[a] -= m; | ||
|
||
if (arr[a]) { tmp[!e].push_back(a); } | ||
else { for (int i : rule[a]) { enter[i]--; } } | ||
} | ||
|
||
e = !e; | ||
ret += m; | ||
} | ||
} | ||
|
||
int main() { | ||
fastio | ||
|
||
int testCase; cin >> testCase; | ||
while (testCase--) | ||
printf("%d\n", f()); | ||
} |