Pagini recente » Cod sursa (job #2199331) | Cod sursa (job #1552248) | Cod sursa (job #3280932) | Cod sursa (job #552238) | Cod sursa (job #1277369)
#include <fstream>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 51, M = 601, Timp = 100, inf = 0x3f3f3f3f;
int cod[N][N], cap[N][N], flux[Timp][M + N], nrPpl[N];
int T[N * Timp], n, m, timp = -1;
vector<int> graph[N];
queue<int> Q;
inline int getCap(int T, int x, int y){
if (x == y)
return inf;
return cap[x][y] - flux[T][ cod[x][y] ];
}
inline int getCap(int x, int y){
if (x == 0 && y < N)
return nrPpl[y];
return getCap(x / N, x % N, y % N);
}
inline void relax(int x, int y, int C){
if (x == 0 && y < N)
nrPpl[y] -= C;
flux[x / N][ cod[x % N][y % N] ] += C;
flux[x / N][ cod[y % N][x % N] ] -= C;
}
void bfs(){
memset(T, -1, sizeof(T));
for (int i = 1 ; i <= n ; i++)
if (nrPpl[i]){
T[i] = 0;
Q.push(i);
}
while (!Q.empty()){
int x = Q.front();
Q.pop();
if ( x / N == timp )
continue;
for (int y = (x / N + 1) * N + 1, i = 1 ; i <= n ; y++, i++)
if ( T[y] == - 1 && getCap(x, y) > 0 ){
T[y] = x;
Q.push(y);
}
}
}
int maxFlow(){
int desiredFlow = 0;
for (int i = 1 ; i <= n ; i++)
desiredFlow += nrPpl[i];
int S = 0;
while (desiredFlow){
int newFlow = 0;
timp++;
do {
desiredFlow -= newFlow;
newFlow = 0;
bfs();
for (int vec = 1 ; vec <= timp * N + 1 ; vec += N)
if (T[vec] != -1){
int F = inf;
for (int i = vec ; i != S ; i = T[i])
F = min(F, getCap(T[i], i));
for (int i = vec ; i != S ; i = T[i])
relax(T[i], i, F);
newFlow += F;
}
} while (newFlow);
}
return timp;
}
int main(){
int x, y, c;
ifstream in("algola.in");
in >> n >> m;
for (int i = 1 ; i <= n ; i++)
in >> nrPpl[i];
for (int i = 1 ; i <= m ; i++){
in >> x >> y >> c;
cap[x][y] = cap[y][x] = c;
cod[x][y] = i;
cod[y][x] = i + m;
}
m <<= 1;
in.close();
ofstream out("algola.out");
out << maxFlow() << '\n';
out.close();
return 0;
}