Cod sursa(job #2595573)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 7 aprilie 2020 22:04:51
Problema Critice Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.46 kb
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const short int NMAX = 1000;
const short int MMAX = 10000;
const short int INF = 32000;
struct muchii
{
    short int x , y , cf;
    muchii(short int tx = 0 , short int ty = 0 , short int tcf = 0)
    {
        x = tx;
        y = ty;
        cf = tcf;
    }
};
muchii mch[2 * MMAX + 5];
short int viz[NMAX + 5] , viz1[NMAX + 5] , s , t , n;
vector <short int> G[NMAX + 5];
bool bfs()
{
    short int u , v , j , muchie;
    for(j = 1 ; j <= n ; j ++)
        viz[j] = -1;
    viz[s] = -2;
    queue <short int> q;
    q.push(s);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(u == t)
            continue;
        for(j = 0 ; j < G[u].size() ; j ++)
        {
            muchie = G[u][j];
            v = mch[muchie].y;
            if(viz[v] == -1 && mch[muchie].cf > 0)
            {
                viz[v] = muchie;
                q.push(v);
            }
        }
    }
    if(viz[t] == -1)
        return 0;
    return 1;
}
void update_flow()
{
    short int nod , flow;
    flow = INF;
    for(nod = t ; viz[nod] != -2 ; nod = mch[viz[nod]].x)
        flow = min(flow , mch[viz[nod]].cf);
    for(nod = t ; viz[nod] != -2 ; nod = mch[viz[nod]].x)
    {
        mch[viz[nod]].cf = mch[viz[nod]].cf - flow;
        mch[viz[nod] ^ 1].cf = mch[viz[nod] ^ 1].cf + flow;
    }
}
void max_flow()
{
    short int muchie , nod , cf , j;
    while(bfs())
        for(j = 0 ; j < G[t].size() ; j ++)
        {
            muchie = G[t][j];
            nod = mch[muchie].y;
            cf = mch[muchie ^ 1].cf;
            if(cf == 0 || viz[nod] == -1)
                continue;
            viz[t] = muchie ^ 1;
            update_flow();
        }
}
void bfs2(short int start , short int finish , short int *d)
{
    short int muchie , u , v , ct , j;
    if(start == 1)
        ct = 0;
    else
        ct = 1;
    queue <short int> q;
    d[start] = 1;
    q.push(start);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(u == finish)
            continue;
        for(j = 0 ; j < G[u].size() ; j ++)
        {
            muchie = G[u][j];
            v = mch[muchie].y;
            if(!d[v] && mch[muchie^ct].cf > 0)
            {
                d[v] = 1;
                q.push(v);
            }
        }
    }
}
int main()
{
    freopen("critice.in" , "r" , stdin);
    freopen("critice.out" , "w" , stdout);
    short int m , x , y , z , i;
    scanf("%hd%hd" , &n , &m);
    for(i = 0 ; i < m ; i ++)
    {
        scanf("%hd%hd%hd" , &x , &y , &z);
        mch[2 * i] = muchii(x , y , z);
        mch[2 * i + 1] = muchii(y , x , z);
        G[x].push_back(2 * i);
        G[y].push_back(2 * i + 1);
    }
    s = 1;
    t = n;
    max_flow();
    memset(viz , 0 , sizeof(viz));
    bfs2(1 , n , viz);
    bfs2(n , 1 , viz1);
    for(i = 0 ; i < 2 * m ; i = i + 2)
    {
        x = mch[i].x;
        y = mch[i].y;
        if((viz[x] && viz1[y] && mch[i].cf == 0) || (viz1[x] && viz[y] && mch[i ^ 1].cf == 0))
            viz[0] ++;
    }
    printf("%hd\n" , viz[0]);
    for(i = 0 ; i < 2 * m ; i = i + 2)
    {
        x = mch[i].x;
        y = mch[i].y;
        if((viz[x] && viz1[y] && mch[i].cf == 0) || (viz1[x] && viz[y] && mch[i ^ 1].cf == 0))
            printf("%hd\n" , i / 2 + 1);
    }
    return 0;
}