Cod sursa(job #1807874)

Utilizator ade_tomiEnache Adelina ade_tomi Data 16 noiembrie 2016 23:47:15
Problema Algola Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int NMAX  = 5004;
queue<int> q;
int cap[NMAX][NMAX], flux[NMAX][NMAX], dad[NMAX];
vector <int> v[NMAX];
vector <pair<int, int> > g[53];
void bfs (int source, int dest)
{
    memset(dad, -1, sizeof (dad));
    dad[source] = 0;
    q.push(source);
    while(!q.empty())
    {
        int k = q.front();
        q.pop();
        if (k == dest)
            continue;
        for (int i = 0 ; i < v[k].size(); i++)
        {
            if (dad[v[k][i]] == -1 && flux[k][v[k][i]] < cap[k][v[k][i]])
            {
                dad[v[k][i]] = k;
                q.push(v[k][i]);
            }
        }
    }
}
int sol, staph, np;
int flow(int source, int dest)
{
    
    
    while (1)
    {
        bfs(source, dest);
        if (dad[dest] == -1)
            break;
        
        for (int i = 0 ; i < v[dest].size(); i++)
        {
            int nod = v[dest][i];
            if (flux[nod][dest] < cap[nod][dest] && dad[nod] != -1)
            {
                dad[dest] = nod;
                int capmin = INF;
                int p = dest;
                while (p > source)
                {
                    capmin = min(capmin, cap[dad[p]][p] - flux[dad[p]][p]);
                    p = dad[p];
                }
                p = dest;
                while (p > source)
                {
                    flux[p][dad[p]] -= capmin;
                    flux[dad[p]][p] += capmin;
                    p = dad[p];
                }
                sol += capmin;
            }
            
        }
    }
    return sol;
} 

int main ()
{
    int n, m, x, y, l, dest;
    ifstream cin ("algola.in");
    ofstream cout ("algola.out");
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> cap[0][i];
        staph += cap[0][i];
        
        v[0].push_back(i);
        v[i].push_back(0);
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> x >> y >> l;
        g[x].push_back({y,l});
        g[y].push_back({x,l});
    }
    dest = 5001;
    v[1].push_back(dest);
    v[dest].push_back(1);
    cap[1][dest] = INF;
    int t = 0;
  //  cout << flow(0,dest) << "\n";
    while (flow(0,dest)< staph)
    {
   //     cout << sol <<" " << np<< endl;
        t++;
        for (int i = 1; i <= n; i++)
        {
            v[n * (t - 1) + i].push_back(n * t + i);
            v[n * t + i].push_back(n * (t - 1)  + i);
            cap[n * (t - 1) + i][n * t + i] = INF;
            
            v[n * t + 1].push_back(dest);
            v[dest].push_back(n * t + 1);
            cap[n * t + 1][dest] = INF;
            for (int j = 0 ; j < g[i].size(); j++)
            {
                int nod = g[i][j].first;
                v[n * (t - 1) + nod].push_back(n * t + i);
                v[n * t + i].push_back(n * (t - 1) + nod);
                
                cap[n * (t - 1) + nod][n * t + i] = g[i][j].second;
            }
            

        }

    }
    cout << t;
    return 0;
}