Cod sursa(job #1641373)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 8 martie 2016 22:42:18
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.97 kb
// Template v2
#define pb push_back
#define mp make_pair
#define first x
#define second y
#define l(x) x<<1
#define r(x) x<<1 | 1
#define P 73
#define MOD1 3000017
#define MOD2 3000541
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PKK;
// primes less than 100
const int PRIM[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
const int CMAX = 10005;
const int MOD = 1000000007;
const int NMAX = 1005;
const short INF16 = 32000;
const int INF = int(1e9);
const LL INF64 = LL(1e18);
const LD EPS = 1e-9, PI = acos(-1.0);

int C[NMAX][NMAX];
vector<pair<int, int> > Edgiz;
VI rs;
int F[NMAX][NMAX];
VI G[NMAX];
int p[NMAX];
bool viz[NMAX];
int N,M,flow,x,y,z,minf;
queue<int> q;

bool bfs()
{
    q.push(1);
    memset(viz, 0, sizeof(viz));
    viz[1]=1;
    
    while(!q.empty())
    {
        int curr=q.front(); q.pop();
        
        if(curr==N)
            continue;
        for(int i=0; i<G[curr].size(); ++i)
        {
            int v=G[curr][i];
            if(C[curr][v] == F[curr][v] || viz[v])
                continue;
            viz[v]=true;
            q.push(v);
            p[v]=curr;
        }
    }
    return viz[N];
}

void read()
{
    cin>>N>>M;
    for(int i=1; i<=M; ++i)
    {
        cin>>x>>y>>z;
        C[x][y]+=z;
        C[y][x]+=z;
        Edgiz.pb(mp(x,y));
        G[x].pb(y);
        G[y].pb(x);
    }
    
    flow=0;
    bfs();
    /*for(int i=1; i<=N; ++i)
        cout<<viz[i]<<" ";
    cout<<"\n";
    */
    while(bfs())
    {
        for(int i=0; i<G[N].size(); ++i)
        {
            int curr=G[N][i];
            //cout<<"ok "<<'\n';
            if(C[curr][N]==F[curr][N] || !viz[curr])
                continue;
            minf = INF;
            p[N]=curr;
            for(int i=N; i!=1; i=p[i])
                minf=min(minf, C[p[i]][i]-F[p[i]][i]);//, cout<<i<<" ";
            //cout<<"\n";
            //cout<<minf<<" ";
            if(minf==0)
                continue;
            for(int i=N; i!=1; i=p[i])
            {
                F[p[i]][i]+=minf;
                F[i][p[i]]-=minf;
            }
            flow+=minf;
        }
   }
   // cout<<flow<<"\n";
   for(int i=0; i<Edgiz.size(); ++i)
   {
        if(C[Edgiz[i].x][Edgiz[i].y]==F[Edgiz[i].x][Edgiz[i].y] || C[Edgiz[i].y][Edgiz[i].x]==F[Edgiz[i].y][Edgiz[i].x])
        {
        C[Edgiz[i].x][Edgiz[i].y]++;
        C[Edgiz[i].y][Edgiz[i].x]++;
        if(bfs())
            rs.pb(i+1);
        C[Edgiz[i].x][Edgiz[i].y]--;
        C[Edgiz[i].y][Edgiz[i].x]--;
        }
   }
   cout<<rs.size()<<'\n';
   for(int i=0; i<rs.size(); ++i)
        cout<<rs[i]<<'\n';
    
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    cout << setprecision(16) << fixed;
    
    assert(freopen("critice.in", "rt", stdin));
    assert(freopen("critice.out", "wt", stdout));
    
    read();

    return 0;
}