#include <iostream> #include <vector> #include <utility> #include <algorithm> #include <cmath> #include <string> #include <set> #include <cstdlib> #include <map> #include <list> using namespace std; typedef pair<int,int> p; void reads(vector<map<int,int> > &sub,int n){ for(int i=0;i<n;++i){ map <int,int> m; sub.push_back(m); int g; cin>>g; sub[i].insert(make_pair(i+1,g)); } } void readre(vector<p> &re,int k){ for(int i=0;i<k;++i){ int a, b; cin>>a>>b; re.push_back(make_pair(a,b)); } } void readst(vector<p> &steps,int m){ for(int i=0;i<m;++i){ int a,b; cin>>a>>b; steps.push_back(make_pair(a,b)); } } long long mix(vector<map<int,int> > &sub,vector<p > &re,vector<p> &steps){ long long grams=0; for(auto it=steps.begin();it!=steps.end();++it){ //dodawana w it->first //szukanie w reakcjach? for(auto ir=re.begin();ir!=re.end();++ir){ if((sub[it->first-1].find(ir->first)!=sub[it->first-1].end()) && (sub[it->second-1].find(ir->second)!=sub[it->second-1].end() )) { int a=sub[it->first-1][ir->first]; int b=sub[it->second-1][ir->second]; int g=min(a,b); grams+=2*g; sub[it->first-1][ir->first]-=g; sub[it->second-1][ir->second]-=g; if(a==0) sub[it->first-1].erase(sub[it->first-1].find(ir->first)); if(b==0) sub[it->second-1].erase(sub[it->second-1].find(ir->second)); } if (((sub[it->first-1].find(ir->second)!=sub[it->first-1].end()) &&( sub[it->second-1].find(ir->first)!=sub[it->second-1].end() ))) { int a=sub[it->first-1][ir->second]; int b=sub[it->second-1][ir->first]; int g=min(a,b); grams+=2*g; sub[it->first-1][ir->second]-=g; sub[it->second-1][ir->first]-=g; if(a==0) sub[it->first-1].erase(sub[it->first-1].find(ir->second)); if(b==0) sub[it->second-1].erase(sub[it->second-1].find(ir->first)); } } sub[it->second-1].insert(sub[it->first-1].begin(),sub[it->first-1].end()); // sub[it->first-1].clear(); } return grams; } int main(){ int n;//liczba fiolek cin>>n; int m;//liczba krokow cin>>m; int k;//liczba reakcji cin>>k; vector<map<int,int>> v; reads(v,n); vector<p> steps; readst(steps,m); vector<p> re; readre(re,k); cout<<mix(v,re,steps); 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> #include <utility> #include <algorithm> #include <cmath> #include <string> #include <set> #include <cstdlib> #include <map> #include <list> using namespace std; typedef pair<int,int> p; void reads(vector<map<int,int> > &sub,int n){ for(int i=0;i<n;++i){ map <int,int> m; sub.push_back(m); int g; cin>>g; sub[i].insert(make_pair(i+1,g)); } } void readre(vector<p> &re,int k){ for(int i=0;i<k;++i){ int a, b; cin>>a>>b; re.push_back(make_pair(a,b)); } } void readst(vector<p> &steps,int m){ for(int i=0;i<m;++i){ int a,b; cin>>a>>b; steps.push_back(make_pair(a,b)); } } long long mix(vector<map<int,int> > &sub,vector<p > &re,vector<p> &steps){ long long grams=0; for(auto it=steps.begin();it!=steps.end();++it){ //dodawana w it->first //szukanie w reakcjach? for(auto ir=re.begin();ir!=re.end();++ir){ if((sub[it->first-1].find(ir->first)!=sub[it->first-1].end()) && (sub[it->second-1].find(ir->second)!=sub[it->second-1].end() )) { int a=sub[it->first-1][ir->first]; int b=sub[it->second-1][ir->second]; int g=min(a,b); grams+=2*g; sub[it->first-1][ir->first]-=g; sub[it->second-1][ir->second]-=g; if(a==0) sub[it->first-1].erase(sub[it->first-1].find(ir->first)); if(b==0) sub[it->second-1].erase(sub[it->second-1].find(ir->second)); } if (((sub[it->first-1].find(ir->second)!=sub[it->first-1].end()) &&( sub[it->second-1].find(ir->first)!=sub[it->second-1].end() ))) { int a=sub[it->first-1][ir->second]; int b=sub[it->second-1][ir->first]; int g=min(a,b); grams+=2*g; sub[it->first-1][ir->second]-=g; sub[it->second-1][ir->first]-=g; if(a==0) sub[it->first-1].erase(sub[it->first-1].find(ir->second)); if(b==0) sub[it->second-1].erase(sub[it->second-1].find(ir->first)); } } sub[it->second-1].insert(sub[it->first-1].begin(),sub[it->first-1].end()); // sub[it->first-1].clear(); } return grams; } int main(){ int n;//liczba fiolek cin>>n; int m;//liczba krokow cin>>m; int k;//liczba reakcji cin>>k; vector<map<int,int>> v; reads(v,n); vector<p> steps; readst(steps,m); vector<p> re; readre(re,k); cout<<mix(v,re,steps); return 0; } |