#include <stdio.h>
#define FOR(ii, ll, uu) for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii)
#define REP(ii, nn) FOR(ii, 0, nn)
// from stackoverflow
#define getcx getchar_unlocked
inline int giu()
{
int n = 0;
int ch=getcx();
while (ch < '0' || ch > '9')
{
ch = getcx();
}
while (ch >= '0' && ch <= '9')
n = (n<<3)+(n<<1) + ch-'0', ch = getcx();
return n;
}
#define GI (giu())
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
typedef pair<int, int> PII;
int main()
{
int n=GI, m=GI, k=GI; /* substrates, steps, reactions */
vector<int> amount; amount.reserve(n);
REP(i, n)
{
amount.push_back(GI);
// printf("Amount %d: %d\n", i+1, amount.back());
}
vector<PII> steps; steps.reserve(m);
REP(i, m)
{
int a=GI-1, b=GI-1;
steps.push_back(PII(a, b));
// printf("Step %d: %d -> %d\n", i+1, steps.back().first+1, steps.back().second+1);
}
vector<deque<int> > substrate_reactions(n);
vector<PII> reactions; reactions.reserve(k);
REP(i, k)
{
int a=GI-1, b=GI-1;
reactions.push_back(PII(a, b));
substrate_reactions[a].push_back(i);
substrate_reactions[b].push_back(i);
}
long long total = 0;
REP(i, m)
{
int a = steps[i].first, b = steps[i].second;
// printf("Pouring %d into %d...\n", a+1, b+1);
deque<int> new_reactions;
while (!substrate_reactions[a].empty() && !substrate_reactions[b].empty())
{
if (substrate_reactions[a].front() == substrate_reactions[b].front())
{
int r = substrate_reactions[a].front();
int ing1 = reactions[r].first;
int ing2 = reactions[r].second;
int reacting_amount = min(amount[ing1], amount[ing2]);
total += 2*reacting_amount;
amount[ing1] -= reacting_amount;
amount[ing2] -= reacting_amount;
substrate_reactions[a].pop_front();
substrate_reactions[b].pop_front();
}
else
{
int r;
if (substrate_reactions[a].front() < substrate_reactions[b].front())
{
r = substrate_reactions[a].front(); substrate_reactions[a].pop_front();
}
else
{
r = substrate_reactions[b].front(); substrate_reactions[b].pop_front();
}
if (amount[reactions[r].first] && amount[reactions[r].second])
new_reactions.push_back(r);
}
}
if (!substrate_reactions[a].empty())
{
new_reactions.insert(new_reactions.end(), substrate_reactions[a].begin(), substrate_reactions[a].end());
substrate_reactions[a].clear();
}
if (!substrate_reactions[b].empty())
{
new_reactions.insert(new_reactions.end(), substrate_reactions[b].begin(), substrate_reactions[b].end());
substrate_reactions[b].clear();
}
new_reactions.swap(substrate_reactions[b]);
}
printf("%lld\n", total);
return 0;
}
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 | #include <stdio.h> #define FOR(ii, ll, uu) for(int ii##lim = (uu), ii = (ll); ii < ii##lim; ++ii) #define REP(ii, nn) FOR(ii, 0, nn) // from stackoverflow #define getcx getchar_unlocked inline int giu() { int n = 0; int ch=getcx(); while (ch < '0' || ch > '9') { ch = getcx(); } while (ch >= '0' && ch <= '9') n = (n<<3)+(n<<1) + ch-'0', ch = getcx(); return n; } #define GI (giu()) #include <algorithm> #include <vector> #include <deque> using namespace std; typedef pair<int, int> PII; int main() { int n=GI, m=GI, k=GI; /* substrates, steps, reactions */ vector<int> amount; amount.reserve(n); REP(i, n) { amount.push_back(GI); // printf("Amount %d: %d\n", i+1, amount.back()); } vector<PII> steps; steps.reserve(m); REP(i, m) { int a=GI-1, b=GI-1; steps.push_back(PII(a, b)); // printf("Step %d: %d -> %d\n", i+1, steps.back().first+1, steps.back().second+1); } vector<deque<int> > substrate_reactions(n); vector<PII> reactions; reactions.reserve(k); REP(i, k) { int a=GI-1, b=GI-1; reactions.push_back(PII(a, b)); substrate_reactions[a].push_back(i); substrate_reactions[b].push_back(i); } long long total = 0; REP(i, m) { int a = steps[i].first, b = steps[i].second; // printf("Pouring %d into %d...\n", a+1, b+1); deque<int> new_reactions; while (!substrate_reactions[a].empty() && !substrate_reactions[b].empty()) { if (substrate_reactions[a].front() == substrate_reactions[b].front()) { int r = substrate_reactions[a].front(); int ing1 = reactions[r].first; int ing2 = reactions[r].second; int reacting_amount = min(amount[ing1], amount[ing2]); total += 2*reacting_amount; amount[ing1] -= reacting_amount; amount[ing2] -= reacting_amount; substrate_reactions[a].pop_front(); substrate_reactions[b].pop_front(); } else { int r; if (substrate_reactions[a].front() < substrate_reactions[b].front()) { r = substrate_reactions[a].front(); substrate_reactions[a].pop_front(); } else { r = substrate_reactions[b].front(); substrate_reactions[b].pop_front(); } if (amount[reactions[r].first] && amount[reactions[r].second]) new_reactions.push_back(r); } } if (!substrate_reactions[a].empty()) { new_reactions.insert(new_reactions.end(), substrate_reactions[a].begin(), substrate_reactions[a].end()); substrate_reactions[a].clear(); } if (!substrate_reactions[b].empty()) { new_reactions.insert(new_reactions.end(), substrate_reactions[b].begin(), substrate_reactions[b].end()); substrate_reactions[b].clear(); } new_reactions.swap(substrate_reactions[b]); } printf("%lld\n", total); return 0; } |
English