Pagini recente » Cod sursa (job #1902156) | Cod sursa (job #169393) | Cod sursa (job #2438539) | Cod sursa (job #2542032) | Cod sursa (job #1379689)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n,m,s,x,y,z,t,maxflow,fin ;
int cap[5010][5010],a[5010],flux[5010][5010];
vector <int> v[5010];
vector < pair<int,int> > g[55];
inline void add(int a,int b,int c){
v[a].push_back(b);
v[b].push_back(a);
cap[a][b] = c;
}
bool abc(){
queue<int> q;
for(int i=0;i<= fin + n; ++i) a[i] = -1;
q.push(0); a[0] = 0;
while( !q.empty() ){
int nod = q.front(); q.pop();
for(vector<int>::iterator it=v[nod].begin();it!=v[nod].end();it++){
if( a[ *it] != -1 || cap[nod][*it] <= flux[nod][*it] ) continue;
a[*it] = nod;
if( nod != fin ) q.push(*it);
}
}
return a[fin] != -1;
}
int main()
{
freopen("algola.in" , "r" , stdin);
freopen("algola.out", "w" , stdout);
scanf("%d %d\n",&n,&m);
for(int i =1; i <= n;++i){
scanf("%d ",&x); s+=x;
add(0,i,x);
}
for(int i=1;i<=m;++i){
scanf("%d %d %d\n",&x,&y,&z);
g[x].push_back( {y,z} );
g[y].push_back( {x,z} );
}
for(t=1; maxflow < s; ++t){
fin = t*n + 1;
for(int i=1;i<=n;++i){
add(n*(t-1)+i , n*t+i , 55 );
for(vector<pair<int,int> >::iterator it=g[i].begin(); it != g[i].end(); ++it )
add( n*(t-1)+i , n*t+it->first , it->second );
}
while( abc() ){
int _max = 55;
for(int i=fin; i ; i= a[i]) _max = min( _max , cap[a[i]][i] - flux[a[i]][i] );
for(int i=fin; i ; i= a[i]){
flux[ a[i] ][i] += _max;
flux[ i ][ a[i]]-= _max;
}
maxflow += _max;
}
}
printf("%d",t-1);
return 0;
}