Pagini recente » Cod sursa (job #3285449) | Cod sursa (job #373938) | Cod sursa (job #1432080) | Cod sursa (job #3292597) | Cod sursa (job #2701705)
#include <iostream>
#include <vector>
#include <cstring>
#include <fstream>
#include <queue>
using namespace std;
ifstream in("algola.in");
ofstream out("algola.out");
const int INF = 1e9 + 9;
const int dim = 55;
vector <int> vec[dim];
int parent[dim],viz[dim],c[dim][dim],n,m,a[dim],initial[dim][dim],suma;
int BFS(int sursa,int destinatie)
{
memset(viz,0,sizeof(viz));
queue<pair<int,int> > q;
q.push({sursa, INF});
viz[sursa] = 1;
while (!q.empty())
{
pair<int,int> x = q.front();
int flow = x.second;
q.pop();
for (int j=0; j<vec[x.first].size(); j++)
{
int y = vec[x.first][j];
if (!viz[y] && c[x.first][y] > 0)
{
viz[y] = 1;
flow = min(flow, c[x.first][y]);
parent[y] = x.first;
q.push({y, flow});
if (y == destinatie) return flow;
}
}
}
return 0;
}
int Get_Flux(int sursa,int destinatie)
{
int maxflow = 0;
int flow = 0;
int ok;
while ((flow = BFS(sursa, destinatie)))
{
maxflow += flow;
int x = destinatie;
while (x != sursa)
{
int p = parent[x];
c[parent[x]][x] -= flow;
c[x][parent[x]] += flow;
x = parent[x];
}
}
return maxflow;
}
int main()
{
in >> n >> m;
for (int i=1; i<=n; i++)
{
in >> a[i];
suma += a[i];
vec[0].push_back(i);
vec[i].push_back(0);
if (a[i] == 0) c[0][i] = INF;
else c[0][i] = a[i];
}
for (int i=1; i<=m; i++)
{
int x,y,f;
in >> x >> y >> f;
vec[x].push_back(y);
vec[y].push_back(x);
initial[x][y] = f;
}
int ok = 0;
int t = 1;
while (1)
{
for (int i=1; i<=n; i++)
{
for (auto y:vec[i])
{
c[i][y] = initial[i][y] * t;
}
}
t++;
int cat = Get_Flux(0, 1);
if (cat >= suma)
{
out << t;
return 0;
}
}
return 0;
}