#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
long long int masa[200005];
int a[200005];
int b[200005];
vector<pair<int, int> > co[200005]; //z czym, kiedy
vector<int> zaw[200005];
int gdzie[200005];
long long int MASA, osad;
priority_queue<pair<int, pair<int, int> > > Q;
int main()
{
ios_base::sync_with_stdio(0);
long long int n, m, k, s1, s2, A, B, x, y, czas;
cin >> n >> m >> k;
for(int i=1; i<=n; i++)
{
cin >> masa[i];
}
for(int i=1; i<=m; i++)
{
cin >> a[i] >> b[i];
}
for(int i=1; i<=k; i++)
{
cin >> s1 >> s2;
co[s1].push_back(make_pair(s2,i));
co[s2].push_back(make_pair(s1,i));
}
for(int i=1; i<=n; i++)
{
gdzie[i]=i;
zaw[i].push_back(i);
}
for(int i=1; i<=m; i++)
{
A=a[i];
B=b[i];
if(true)
{
for(int t=0; t<zaw[A].size(); t++)
{
x=zaw[A][t];
zaw[B].push_back(x);
gdzie[x]=B;
for(int r=0; r<co[x].size(); r++)
{
y=co[x][r].first;
czas=co[x][r].second;
if(gdzie[x]==gdzie[y])
{
Q.push(make_pair((-1)*czas, make_pair(x,y)));
}
}
}
while(!Q.empty())
{
x=Q.top().second.first;
y=Q.top().second.second;
Q.pop();
MASA=min(masa[x],masa[y]);
osad=osad+2*MASA;
masa[x]=masa[x]-MASA;
masa[y]=masa[y]-MASA;
}
zaw[A].clear();
}
}
cout << osad << endl;
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 | #include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<queue> using namespace std; long long int masa[200005]; int a[200005]; int b[200005]; vector<pair<int, int> > co[200005]; //z czym, kiedy vector<int> zaw[200005]; int gdzie[200005]; long long int MASA, osad; priority_queue<pair<int, pair<int, int> > > Q; int main() { ios_base::sync_with_stdio(0); long long int n, m, k, s1, s2, A, B, x, y, czas; cin >> n >> m >> k; for(int i=1; i<=n; i++) { cin >> masa[i]; } for(int i=1; i<=m; i++) { cin >> a[i] >> b[i]; } for(int i=1; i<=k; i++) { cin >> s1 >> s2; co[s1].push_back(make_pair(s2,i)); co[s2].push_back(make_pair(s1,i)); } for(int i=1; i<=n; i++) { gdzie[i]=i; zaw[i].push_back(i); } for(int i=1; i<=m; i++) { A=a[i]; B=b[i]; if(true) { for(int t=0; t<zaw[A].size(); t++) { x=zaw[A][t]; zaw[B].push_back(x); gdzie[x]=B; for(int r=0; r<co[x].size(); r++) { y=co[x][r].first; czas=co[x][r].second; if(gdzie[x]==gdzie[y]) { Q.push(make_pair((-1)*czas, make_pair(x,y))); } } } while(!Q.empty()) { x=Q.top().second.first; y=Q.top().second.second; Q.pop(); MASA=min(masa[x],masa[y]); osad=osad+2*MASA; masa[x]=masa[x]-MASA; masa[y]=masa[y]-MASA; } zaw[A].clear(); } } cout << osad << endl; return 0; } |
English