#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <utility>
using namespace std;
class Reakcja{
public:
int sub1;
int sub2;
int priority;
};
class Compare{
public:
bool operator()(Reakcja l , Reakcja p){
if(l.priority < p.priority)
return true;
return false;
}
};
class Fiolka{
public:
map <int,int> v; // substancje
map <int, vector<Reakcja> > r; // reakcje
};
int main(){
int n,m,k;
cin >> n >> m >> k;
vector <Fiolka> f(n);
int temp1 , temp2;
for(int i=0 ; i<n ;i++){
cin >> temp1;
temp1;
f[i].v[i]=temp1;
}
vector <pair<int,int> > kroki(m);
for(int i=0 ;i<m ; i++){
cin >> temp1 >> temp2;
kroki[i].first = temp1-1;
kroki[i].second = temp2-1;
}
Reakcja temp;
for(int i=0 ; i<k ; i++){
cin >> temp1 >> temp2;
temp1--;temp2--;
temp.sub1 = temp1;
temp.sub2 = temp2;
temp.priority = i;
f[temp1].r[temp2].push_back(temp);
f[temp2].r[temp1].push_back(temp);
}
/////////////////////////////////////
priority_queue <Reakcja , vector<Reakcja> , Compare> kolejka;
long long odp = 0;
for(int i=0 ; i<m ; i++){
for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){
if(f[kroki[i].second].r.find(it->first) != f[kroki[i].second].r.end())
for(int j=0 ; j<f[kroki[i].second].r.find(it->first)->second.size() ; j++){
kolejka.push(f[kroki[i].second].r.find(it->first)->second[j]);
}
}
while(!kolejka.empty()){
temp = kolejka.top();
kolejka.pop();
if (f[kroki[i].first].v.find(temp.sub1) != f[kroki[i].first].v.end()){
odp += min(f[kroki[i].first].v[temp.sub1], f[kroki[i].second].v[temp.sub2]);
int mini = min(f[kroki[i].first].v[temp.sub1], f[kroki[i].second].v[temp.sub2]);
f[kroki[i].first].v[temp.sub1] -= mini;
f[kroki[i].second].v[temp.sub2] -= mini;
}
else{
int mini = min(f[kroki[i].first].v[temp.sub2], f[kroki[i].second].v[temp.sub1]);
odp += mini;
f[kroki[i].first].v[temp.sub2] -= mini;
f[kroki[i].second].v[temp.sub1] -= mini;
}
}
for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){
if(it->second == 0)
f[kroki[i].first].v.erase(it);
}
/////////////////////////////////////
for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){
f[kroki[i].second].v[it->first] = it->second;
}
for(map <int, vector<Reakcja> >::iterator it = f[kroki[i].first].r.begin() ; it!=f[kroki[i].first].r.end() ; it++){
for(int a=0 ; a<it->second.size() ; a++)
f[kroki[i].second].r[it->first].push_back(it->second[a]);
}
f[kroki[i].first].v.clear();
f[kroki[i].first].r.clear();
}
odp *= 2;
cout << odp;
}
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 | #include <iostream> #include <map> #include <queue> #include <vector> #include <utility> using namespace std; class Reakcja{ public: int sub1; int sub2; int priority; }; class Compare{ public: bool operator()(Reakcja l , Reakcja p){ if(l.priority < p.priority) return true; return false; } }; class Fiolka{ public: map <int,int> v; // substancje map <int, vector<Reakcja> > r; // reakcje }; int main(){ int n,m,k; cin >> n >> m >> k; vector <Fiolka> f(n); int temp1 , temp2; for(int i=0 ; i<n ;i++){ cin >> temp1; temp1; f[i].v[i]=temp1; } vector <pair<int,int> > kroki(m); for(int i=0 ;i<m ; i++){ cin >> temp1 >> temp2; kroki[i].first = temp1-1; kroki[i].second = temp2-1; } Reakcja temp; for(int i=0 ; i<k ; i++){ cin >> temp1 >> temp2; temp1--;temp2--; temp.sub1 = temp1; temp.sub2 = temp2; temp.priority = i; f[temp1].r[temp2].push_back(temp); f[temp2].r[temp1].push_back(temp); } ///////////////////////////////////// priority_queue <Reakcja , vector<Reakcja> , Compare> kolejka; long long odp = 0; for(int i=0 ; i<m ; i++){ for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){ if(f[kroki[i].second].r.find(it->first) != f[kroki[i].second].r.end()) for(int j=0 ; j<f[kroki[i].second].r.find(it->first)->second.size() ; j++){ kolejka.push(f[kroki[i].second].r.find(it->first)->second[j]); } } while(!kolejka.empty()){ temp = kolejka.top(); kolejka.pop(); if (f[kroki[i].first].v.find(temp.sub1) != f[kroki[i].first].v.end()){ odp += min(f[kroki[i].first].v[temp.sub1], f[kroki[i].second].v[temp.sub2]); int mini = min(f[kroki[i].first].v[temp.sub1], f[kroki[i].second].v[temp.sub2]); f[kroki[i].first].v[temp.sub1] -= mini; f[kroki[i].second].v[temp.sub2] -= mini; } else{ int mini = min(f[kroki[i].first].v[temp.sub2], f[kroki[i].second].v[temp.sub1]); odp += mini; f[kroki[i].first].v[temp.sub2] -= mini; f[kroki[i].second].v[temp.sub1] -= mini; } } for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){ if(it->second == 0) f[kroki[i].first].v.erase(it); } ///////////////////////////////////// for(map <int,int>::iterator it = f[kroki[i].first].v.begin() ; it!=f[kroki[i].first].v.end() ; it++){ f[kroki[i].second].v[it->first] = it->second; } for(map <int, vector<Reakcja> >::iterator it = f[kroki[i].first].r.begin() ; it!=f[kroki[i].first].r.end() ; it++){ for(int a=0 ; a<it->second.size() ; a++) f[kroki[i].second].r[it->first].push_back(it->second[a]); } f[kroki[i].first].v.clear(); f[kroki[i].first].r.clear(); } odp *= 2; cout << odp; } |
English