Pagini recente » Cod sursa (job #228069) | Cod sursa (job #1269096) | Cod sursa (job #1686880) | Cod sursa (job #1352914) | Cod sursa (job #228071)
Cod sursa(job #228071)
#include<fstream>
using namespace std;
ifstream in("critice.in");
ofstream out("critice.out");
int n,m,x,y,c,C[1001][1001],f[1001][1001],s,d,q[1001],viz[1001],v,vizn[1001];
struct muchie
{int st,dr,cost;} lista[100];
int min(int a,int b){if(a<b) return a;
else return b;}
int abs(int x){if(x<0) return -x;
else return x;}
void citire()
{in>>n>>m;s=1;d=n;
for(int i=1;i<=m;i++) {in>>x>>y>>c;C[x][y]=c;C[y][x]=c;lista[i].st=x;lista[i].dr=y;lista[i].cost=c;}}
int bfs()
{int p,u;
p=u=0;q[p]=s;viz[s]=1;
while(p<=u && viz[d]==0)
{x=q[p++];
for(int i=s;i<=d;i++)
{ if(viz[i]==0)
if(C[x][i]>f[x][i]){q[++u]=i;viz[i]=x;}
else if(f[i][x]>0) {q[++u]=i;viz[i]=-x;}}
if (viz[d]!=0) return 0;
}
return 1;
}
void edmond_karp()
{int a,b,l[10000],lg;
do
{lg=0;a=b=10000;l[lg]=d;
for(int i=1;i<=n;i++) viz[i]=0;
if(bfs()) return;
lg=0;a=b=10000;l[lg]=d;
while(l[lg]!=s)
{++lg; l[lg]=abs(viz[l[lg-1]]);
if(viz[l[lg-1]]>0) a=min(a,C[l[lg]][l[lg-1]]-f[l[lg]][l[lg-1]]);
else if(viz[l[lg-1]]<0) b=min(b,f[l[lg-1]][l[lg]]);
}
v=min(a,b);
for(int i=lg;i>0;i--)
{if(viz[l[i-1]]>0) f[l[i]][l[i-1]]+=v;
else f[l[i-1]][l[i]]-=v;} }
while(1);}
void bfs2(int x)
{vizn[x]=1;
for(int i=1;i<=n;i++)
if(C[x][i]!=0 && (C[x][i]-f[x][i]!=0) && vizn[i]==0) bfs2(i);}
int main()
{citire();
edmond_karp();
for(int i=1;i<=n;i++) viz[i]=0;for(int i=1;i<=n;i++) vizn[i]=0;
bfs();
bfs2(d);
int nr=0,aux1[1001];
for(int i=1;i<=m;i++)
{if((viz[lista[i].st]!=0 && vizn[lista[i].dr]!=0) && (f[lista[i].st][lista[i].dr]==C[lista[i].st][lista[i].dr])) {nr++;aux1[nr]=i;}
if((viz[lista[i].dr]!=0 && vizn[lista[i].st]!=0) && f[lista[i].dr][lista[i].st]==C[lista[i].dr][lista[i].st]){nr++;aux1[nr]=i;}}
out<<nr<<'\n';
for(int i=1;i<=nr;i++)
{out<<aux1[i]<<'\n';}
}