#include <cstdlib> #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <cstdio> #include <utility> #include <vector> #include <set> #include <map> using namespace std; int main(int const /*argc*/, char const * const * /*argv*/) { int n, m, k; scanf("%d %d %d", &n, &m, &k); vector< map<int,int> > vials(n); for(int i = 0; i < n; ++i) { scanf("%d", &(vials[i][i])); } vector< pair<int,int> > instr(m); for(int i = 0; i < m; ++i) { scanf("%d %d", &(instr[i].first), &(instr[i].second)); --instr[i].first; --instr[i].second; } vector< vector<int> > reactions(n); for(int i = 0; i < k; ++i) { int c, d; scanf("%d %d", &c, &d); --c; --d; reactions[c].push_back(d); reactions[d].push_back(c); } int64_t sediment = 0; for(int i = 0; i < m; ++i) { int const &from = instr[i].first; int const &to = instr[i].second; for(map<int,int>::iterator it = vials[from].begin(); it != vials[from].end(); ++it) { int const &c = it->first; map<int,int>::iterator jt = vials[to].find(c); if(jt == vials[to].end()) { vials[to][c] = it->second; for(unsigned int j = 0; j < reactions[c].size(); ++j) { int const &d = reactions[c][j]; map<int,int>::iterator kt = vials[to].find(d); if(kt != vials[to].end()) { int const e = min(vials[to][c], kt->second); vials[to][c] -= e; kt->second -= e; sediment += e << 1; } } } else { jt->second += it->second; } } vials[from].clear(); } printf("%" PRId64 "\n", sediment); return EXIT_SUCCESS; }
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 | #include <cstdlib> #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <cstdio> #include <utility> #include <vector> #include <set> #include <map> using namespace std; int main(int const /*argc*/, char const * const * /*argv*/) { int n, m, k; scanf("%d %d %d", &n, &m, &k); vector< map<int,int> > vials(n); for(int i = 0; i < n; ++i) { scanf("%d", &(vials[i][i])); } vector< pair<int,int> > instr(m); for(int i = 0; i < m; ++i) { scanf("%d %d", &(instr[i].first), &(instr[i].second)); --instr[i].first; --instr[i].second; } vector< vector<int> > reactions(n); for(int i = 0; i < k; ++i) { int c, d; scanf("%d %d", &c, &d); --c; --d; reactions[c].push_back(d); reactions[d].push_back(c); } int64_t sediment = 0; for(int i = 0; i < m; ++i) { int const &from = instr[i].first; int const &to = instr[i].second; for(map<int,int>::iterator it = vials[from].begin(); it != vials[from].end(); ++it) { int const &c = it->first; map<int,int>::iterator jt = vials[to].find(c); if(jt == vials[to].end()) { vials[to][c] = it->second; for(unsigned int j = 0; j < reactions[c].size(); ++j) { int const &d = reactions[c][j]; map<int,int>::iterator kt = vials[to].find(d); if(kt != vials[to].end()) { int const e = min(vials[to][c], kt->second); vials[to][c] -= e; kt->second -= e; sediment += e << 1; } } } else { jt->second += it->second; } } vials[from].clear(); } printf("%" PRId64 "\n", sediment); return EXIT_SUCCESS; } |