#include <iostream>
#include <vector>
using namespace std;
struct fiolka
{
long nr, il;
};
struct krok
{
long pocz, kon;
};
struct osadzajace
{
long s1, s2;
};
inline long long minimal(long long a, long long b)
{
if (a < b) return a;
return b;
}
int main()
{
ios_base::sync_with_stdio(0);
long n, m, k;
cin >> n >> m >> k;
vector< vector<fiolka> > fiolki(n);
fiolka temp;
for (int i = 0; i < n; ++i)
{
temp.nr = i;
cin >> temp.il;
fiolki[i].push_back(temp);
}
vector<krok> kroki(m);
for (int i = 0; i < m; ++i)
{
cin >> kroki[i].pocz >> kroki[i].kon;
--kroki[i].pocz;
--kroki[i].kon;
}
vector<osadzajace> osady(k);
for (int i = 0; i < k; ++i)
{
cin >> osady[i].s1 >> osady[i].s2;
--osady[i].s1;
--osady[i].s2;
}
long osad = 0;
long minimum;
for (int i = 0; i < m; ++i)
{
vector<bool> wyst(n, false);
for (int x = 0; x < fiolki[kroki[i].pocz].size(); ++x) wyst[fiolki[kroki[i].pocz][x].nr] = true;
for (int x = 0; x < fiolki[kroki[i].kon].size(); ++x) wyst[fiolki[kroki[i].kon][x].nr] = true;
for (int j = 0; j < k; ++j)
{
if (wyst[osady[j].s1] && wyst[osady[j].s2])
{
for (int f1 = 0; f1 < fiolki[kroki[i].pocz].size(); ++f1)
{
int f2;
for (f2 = 0; f2 < fiolki[kroki[i].kon].size(); ++f2)
{
if ((fiolki[kroki[i].pocz][f1].nr == osady[j].s1 ||
fiolki[kroki[i].pocz][f1].nr == osady[j].s2) &&
(fiolki[kroki[i].kon][f2].nr == osady[j].s1 ||
fiolki[kroki[i].kon][f2].nr == osady[j].s2) &&
(fiolki[kroki[i].pocz][f1].il != 0 &&
fiolki[kroki[i].kon][f2].il != 0))
{
minimum = minimal(fiolki[kroki[i].pocz][f1].il, fiolki[kroki[i].kon][f2].il);
osad += 2 * minimum;
fiolki[kroki[i].pocz][f1].il -= minimum;
fiolki[kroki[i].kon][f2].il -= minimum;
break;
}
}
if (f2 != fiolki[kroki[i].kon].size()) break;
}
}
}
for (int j = 0; j < fiolki[kroki[i].pocz].size(); ++j)
{
if (fiolki[kroki[i].pocz][j].il != 0) fiolki[kroki[i].kon].push_back(fiolki[kroki[i].pocz][j]);
}
while (fiolki[kroki[i].pocz].size() != 0) fiolki[kroki[i].pocz].pop_back();
}
cout << osad;
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 | #include <iostream> #include <vector> using namespace std; struct fiolka { long nr, il; }; struct krok { long pocz, kon; }; struct osadzajace { long s1, s2; }; inline long long minimal(long long a, long long b) { if (a < b) return a; return b; } int main() { ios_base::sync_with_stdio(0); long n, m, k; cin >> n >> m >> k; vector< vector<fiolka> > fiolki(n); fiolka temp; for (int i = 0; i < n; ++i) { temp.nr = i; cin >> temp.il; fiolki[i].push_back(temp); } vector<krok> kroki(m); for (int i = 0; i < m; ++i) { cin >> kroki[i].pocz >> kroki[i].kon; --kroki[i].pocz; --kroki[i].kon; } vector<osadzajace> osady(k); for (int i = 0; i < k; ++i) { cin >> osady[i].s1 >> osady[i].s2; --osady[i].s1; --osady[i].s2; } long osad = 0; long minimum; for (int i = 0; i < m; ++i) { vector<bool> wyst(n, false); for (int x = 0; x < fiolki[kroki[i].pocz].size(); ++x) wyst[fiolki[kroki[i].pocz][x].nr] = true; for (int x = 0; x < fiolki[kroki[i].kon].size(); ++x) wyst[fiolki[kroki[i].kon][x].nr] = true; for (int j = 0; j < k; ++j) { if (wyst[osady[j].s1] && wyst[osady[j].s2]) { for (int f1 = 0; f1 < fiolki[kroki[i].pocz].size(); ++f1) { int f2; for (f2 = 0; f2 < fiolki[kroki[i].kon].size(); ++f2) { if ((fiolki[kroki[i].pocz][f1].nr == osady[j].s1 || fiolki[kroki[i].pocz][f1].nr == osady[j].s2) && (fiolki[kroki[i].kon][f2].nr == osady[j].s1 || fiolki[kroki[i].kon][f2].nr == osady[j].s2) && (fiolki[kroki[i].pocz][f1].il != 0 && fiolki[kroki[i].kon][f2].il != 0)) { minimum = minimal(fiolki[kroki[i].pocz][f1].il, fiolki[kroki[i].kon][f2].il); osad += 2 * minimum; fiolki[kroki[i].pocz][f1].il -= minimum; fiolki[kroki[i].kon][f2].il -= minimum; break; } } if (f2 != fiolki[kroki[i].kon].size()) break; } } } for (int j = 0; j < fiolki[kroki[i].pocz].size(); ++j) { if (fiolki[kroki[i].pocz][j].il != 0) fiolki[kroki[i].kon].push_back(fiolki[kroki[i].pocz][j]); } while (fiolki[kroki[i].pocz].size() != 0) fiolki[kroki[i].pocz].pop_back(); } cout << osad; return 0; } |
English