//
// main.cpp
// PA_2014_FIO
//
// Created by Michal Kowalski on 18/05/14.
// Copyright (c) 2014 Michal Kowalski. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <functional>
#include <map>
using namespace std;
typedef map<int,long long> FIOLKA;
#define MAX_N 200000
int n,m,k;
int IL[MAX_N+5];
vector<pair<int,int> > STEPS;
vector<pair<int,int> > REACTS;
vector<FIOLKA> FIOLKI;
long long OSAD;
void mieszaj(int f1,int f2);
int main()
{
FIOLKA f0;
scanf("%d %d %d",&n,&m,&k);
FIOLKI.push_back(f0);
for (int i=1;i<=n;++i)
{
scanf("%d",&IL[i]);
FIOLKA f;
f[i] = IL[i];
FIOLKI.push_back(f);
}
for (int i=0;i<m;++i)
{
int s,k;
scanf("%d %d",&s,&k);
STEPS.push_back(pair<int,int>(s,k));
}
for (int i=0;i<k;++i)
{
int s,k;
scanf("%d %d",&s,&k);
REACTS.push_back(pair<int,int>(s,k));
}
for(int s=0;s<m;s++)
{
mieszaj(STEPS[s].first,STEPS[s].second);
}
printf("%lld\n",OSAD);
return 0;
}
void mieszaj(int f1,int f2)
{
FIOLKA F1 = FIOLKI[f1];
FIOLKA F2 = FIOLKI[f2];
///sprawdzamy wzystkei reakcje
for (int r=0;r<REACTS.size(); ++r) {
int p = REACTS[r].first;
int k = REACTS[r].second;
FIOLKA::iterator itF1 = F1.find(p);
FIOLKA::iterator itF2 = F2.find(k);
if ( itF1 != F1.end() && itF2 != F2.end())
{
long long ilF1 = (*itF1).second;
long long ilF2 = (*itF2).second;
long long s = min(ilF1, ilF2);
OSAD += 2*s;
(*itF1).second -= s;
(*itF2).second -= s;
} else
{
FIOLKA::iterator itF1 = F1.find(k);
FIOLKA::iterator itF2 = F2.find(p);
if ( itF1 != F1.end() && itF2 != F2.end())
{
long long ilF1 = (*itF1).second;
long long ilF2 = (*itF2).second;
long long s = min(ilF1, ilF2);
OSAD += 2*s;
(*itF1).second -= s;
(*itF2).second -= s;
}
}
}
////
///przlewamy resztki
FIOLKA::iterator itF1 = F1.begin();
while(itF1 != F1.end())
{
if ((*itF1).second > 0)
{
F2[(*itF1).first] = F2[(*itF1).first] + (*itF1).second;
}
itF1++;
}
FIOLKI[f2] = F2;
}
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 111 112 113 114 | // // main.cpp // PA_2014_FIO // // Created by Michal Kowalski on 18/05/14. // Copyright (c) 2014 Michal Kowalski. All rights reserved. // #include <iostream> #include <algorithm> #include <vector> #include <set> #include <functional> #include <map> using namespace std; typedef map<int,long long> FIOLKA; #define MAX_N 200000 int n,m,k; int IL[MAX_N+5]; vector<pair<int,int> > STEPS; vector<pair<int,int> > REACTS; vector<FIOLKA> FIOLKI; long long OSAD; void mieszaj(int f1,int f2); int main() { FIOLKA f0; scanf("%d %d %d",&n,&m,&k); FIOLKI.push_back(f0); for (int i=1;i<=n;++i) { scanf("%d",&IL[i]); FIOLKA f; f[i] = IL[i]; FIOLKI.push_back(f); } for (int i=0;i<m;++i) { int s,k; scanf("%d %d",&s,&k); STEPS.push_back(pair<int,int>(s,k)); } for (int i=0;i<k;++i) { int s,k; scanf("%d %d",&s,&k); REACTS.push_back(pair<int,int>(s,k)); } for(int s=0;s<m;s++) { mieszaj(STEPS[s].first,STEPS[s].second); } printf("%lld\n",OSAD); return 0; } void mieszaj(int f1,int f2) { FIOLKA F1 = FIOLKI[f1]; FIOLKA F2 = FIOLKI[f2]; ///sprawdzamy wzystkei reakcje for (int r=0;r<REACTS.size(); ++r) { int p = REACTS[r].first; int k = REACTS[r].second; FIOLKA::iterator itF1 = F1.find(p); FIOLKA::iterator itF2 = F2.find(k); if ( itF1 != F1.end() && itF2 != F2.end()) { long long ilF1 = (*itF1).second; long long ilF2 = (*itF2).second; long long s = min(ilF1, ilF2); OSAD += 2*s; (*itF1).second -= s; (*itF2).second -= s; } else { FIOLKA::iterator itF1 = F1.find(k); FIOLKA::iterator itF2 = F2.find(p); if ( itF1 != F1.end() && itF2 != F2.end()) { long long ilF1 = (*itF1).second; long long ilF2 = (*itF2).second; long long s = min(ilF1, ilF2); OSAD += 2*s; (*itF1).second -= s; (*itF2).second -= s; } } } //// ///przlewamy resztki FIOLKA::iterator itF1 = F1.begin(); while(itF1 != F1.end()) { if ((*itF1).second > 0) { F2[(*itF1).first] = F2[(*itF1).first] + (*itF1).second; } itF1++; } FIOLKI[f2] = F2; } |
English