Pagini recente » Cod sursa (job #2786502) | Cod sursa (job #2823176) | Cod sursa (job #1946253) | Cod sursa (job #790787) | Cod sursa (job #515812)
Cod sursa(job #515812)
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define N_MAX 1002
#define M_MAX 10002
int C[N_MAX][N_MAX],F[N_MAX][N_MAX];
struct muchie
{
int x,y;
muchie()
{
}
muchie(const int &a,const int &b)
{
this->x=a;
this->y=b;
}
};
muchie muchii[M_MAX];
int n,m,i,j;
vector <int> a[N_MAX];
bool ut[N_MAX];
int tata[N_MAX];
queue<int> Q;
int bf()
{
memset(ut,0,sizeof(ut));
while(!Q.empty())
Q.pop();
ut[1]=1; Q.push(1);
vector <int> ::iterator it;
while(!Q.empty())
{
int nd=Q.back();
Q.pop();
for(it=a[nd].begin();it!=a[nd].end();it++)
{
if(C[nd][*it]<=F[nd][*it]||ut[*it])
continue;
ut[*it]=1;
tata[*it]=nd;
Q.push(*it);
if(*it==n)
return 1;
}
}
if(ut[n])
return 1;
return 0;
}
bool A1[N_MAX],A2[N_MAX];
void df1(int nd)
{
A1[nd]=1;
vector <int> ::iterator it;
for(it=a[nd].begin();it!=a[nd].end();it++)
{
if(!A1[*it]&&C[nd][*it]>F[nd][*it]&&C[*it][nd]>F[*it][nd])
df1(*it);
}
}
void df2(int nd)
{
A2[nd]=1;
vector <int> ::iterator it;
for(it=a[nd].begin();it!=a[nd].end();it++)
{
if(!A2[*it]&&C[*it][nd]>F[*it][nd]&&C[nd][*it]>F[nd][*it])
df2(*it);
}
}
bool M[M_MAX];
int main()
{
freopen("critice.in","r",stdin);
freopen("critice.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
a[x].push_back(y);
a[y].push_back(x);
C[x][y]=C[y][x]=z;
muchii[i]=muchie(x,y);
}
while(bf())
{
//vector <int> ::iterator it;
//for(it=a[n].begin();it!=a[n].end();it++)
//{
//tata[n]=*it;
int MIN=(1<<30);
int nd;
for(nd=n;nd!=1;nd=tata[nd])
MIN=min(MIN,C[tata[nd]][nd]-F[tata[nd]][nd]);
for(nd=n;nd!=1;nd=tata[nd])
{
F[tata[nd]][nd]+=MIN;
F[nd][tata[nd]]-=MIN;
}
//}
}
df1(1);
df2(n);
int nr=0;
for(i=1;i<=m;i++)
{
int x,y;
x=muchii[i].x;
y=muchii[i].y;
if(C[x][y]==F[x][y])
{
if(A1[x]==1&&A2[y]==1||A1[y]==1&&A2[x]==1)
M[i]=1,nr++;
}
if(C[y][x]==F[y][x])
{
if(A1[x]==1&&A2[y]==1||A1[y]==1&&A2[x]==1)
M[i]=1,nr++;
}
}
printf("%d\n",nr);
for(i=1;i<=m;i++)
if(M[i])
printf("%d\n",i);
return 0;
}