Cod sursa(job #5301)

Utilizator mariusdrgdragus marius mariusdrg Data 11 ianuarie 2007 18:03:20
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.63 kb
#include<stdio.h>
#include<vector>
#include<string.h>

using namespace std;

const int maxn = 10000;


vector <int> vect[maxn];

int st[maxn];
int min;
int st2[maxn];
int x;
int k;
int y;
int i;
int n;
int m;
int mu[maxn];
const int inf = 1000000;
int mu1[maxn];
int l;
int masa[maxn];
const int maxn2 = 1000;
int a[maxn2][maxn2];
int c[maxn2][maxn2];



int min1(int a,int b)
{
	if (a<b) return a;
        return b;
}

int dr()

{


	st[1]=1;
        st2[1]=inf;
        l=1;
        vector <int> :: iterator it;
        for(i=1;i<=l;i++)
        {
        	for(it=vect[st[i]].begin();it!=vect[st[i]].end();it++)
                {
	          	int x=*it;
                	if (masa[*it]==0&&c[st[i]][*it])
                        {
                        	l++;
                                st[l]=*it;
                                masa[*it]=st[i];
              			st2[l]=min1(c[st[i]][*it],st2[i]);
                                if ((*it)==n) return l;
                        }
                }
        }
	return 0;
}




int main()
{
	freopen("critice.in","r",stdin);
        freopen("critice.out","w",stdout);
        scanf("%d %d",&n,&m);
        for(i=1;i<=m;i++)
        {
                scanf("%d %d %d",&x,&y,&k);
                mu[i]=x;
                mu1[i]=y;
                vect[x].push_back(y);
                vect[y].push_back(x);
                c[x][y]+=k;
                c[y][x]+=k;
        }
        int x1=dr();
        int min;
        int nod;
        
        min=st2[x1];
        while(min)
        {
        	x=n;
                nod=masa[x];
                masa[1]=-1;
                while(nod!=-1)
        	{
                        c[x][nod] +=min;
                        //if (c[nod][x]==0) vect[nod].push_back(x);
                        c[nod][x]-=min;
                        nod=masa[nod];
                        x=masa[x];
                }
                memset(masa,0,sizeof(masa));
                min=st2[dr()];
        }
        
        st[1]=1;
        masa[1]=-1;
        for(i=1;i<=l;i++)
        {
        	vector <int> :: iterator it;
                for(it=vect[st[i]].begin();it!=vect[st[i]].end();it++)
                	if (c[st[i]][*it]!=0&&masa[*it]==0)
                        {
                                	l++;
                                        masa[*it]=1;
                                        st[l]=*it;
                        }
                        else
                        {
                        	a[st[i]][*it]=1;
                        }

        
        }
        memset(masa,0,sizeof(masa));
        l=1;
        st[1]=n;
        masa[n]=-1;
        for(i=1;i<=l;i++)
        {
        	vector <int> :: iterator it;
                for(it=vect[st[i]].begin();it!=vect[st[i]].end();it++)
                {
                	int x = *it;
                	if (c[*it][st[i]]!=0&&masa[*it]==0)
                        {
                                	l++;
                                        masa[*it]=1;
                                        st[l]=*it;
                        }
                        else
                        {
                        	a[*it][st[i]]|=2;
                        }
        	}

        
        }

        int nr=0;
        
        for(i=1;i<=m;i++)
        {
                if (a[mu[i]][mu1[i]]==3||a[mu1[i]][mu[i]]==3)nr++;
        }
        printf("%d\n",nr);
        for(i=1;i<=m;i++)
        {
                if (a[mu[i]][mu1[i]]==3||a[mu1[i]][mu[i]]==3)printf("%d\n",i);
        }

	return 0;
}