#include<iostream>
using namespace std;
int main()
{
int n,m,k,os=0;
cin>>n>>m>>k;
int t[n];
bool s[n][n];
pair <int , int> t1[m], t2[k];
for(int i=0; i<n; i++)
{
cin>>t[i];
}
for(int i =0 ; i<m; i++)
{
cin>>t1[i].first>>t1[i].second;
}
for(int i =0; i<k; i++)
{
cin>>t2[i].first>>t2[i].second;
s[t2[i].first][t2[i].second]=1;
s[t2[i].second][t2[i].first]=1;
}
for(int i =0; i<m; i++)
{
if(s[t1[i].first][t1[i].second]==1)
{
if(t[t1[i].first]>= t[t1[i].second])
{ t[t1[i].first]-=t[t1[i].second];
os+=t[t1[i].second]*2;
t[t1[i].second]=0;
}
else if(t[t1[i].first]< t[t1[i].second])
{
t[t1[i].second]-=t[t1[i].first];
os+=t[t1[i].first]*2;
t[t1[i].first]=0;
}
}
else if(s[t1[i].first][t1[i].second]==0)
{
for(int j=0; j<n; j++)
{
if(s[j][t1[j].second]==0 && s[t1[j].first][j]==1)
{
s[j][t1[j].second]==1;
}
}
t[t1[i].second]+=t[t1[i].first];
t[t1[i].first]=0;
}
}
cout<<os<<endl;
}
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 | #include<iostream> using namespace std; int main() { int n,m,k,os=0; cin>>n>>m>>k; int t[n]; bool s[n][n]; pair <int , int> t1[m], t2[k]; for(int i=0; i<n; i++) { cin>>t[i]; } for(int i =0 ; i<m; i++) { cin>>t1[i].first>>t1[i].second; } for(int i =0; i<k; i++) { cin>>t2[i].first>>t2[i].second; s[t2[i].first][t2[i].second]=1; s[t2[i].second][t2[i].first]=1; } for(int i =0; i<m; i++) { if(s[t1[i].first][t1[i].second]==1) { if(t[t1[i].first]>= t[t1[i].second]) { t[t1[i].first]-=t[t1[i].second]; os+=t[t1[i].second]*2; t[t1[i].second]=0; } else if(t[t1[i].first]< t[t1[i].second]) { t[t1[i].second]-=t[t1[i].first]; os+=t[t1[i].first]*2; t[t1[i].first]=0; } } else if(s[t1[i].first][t1[i].second]==0) { for(int j=0; j<n; j++) { if(s[j][t1[j].second]==0 && s[t1[j].first][j]==1) { s[j][t1[j].second]==1; } } t[t1[i].second]+=t[t1[i].first]; t[t1[i].first]=0; } } cout<<os<<endl; } |
English