Pagini recente » Cod sursa (job #491760) | Cod sursa (job #2553352) | Cod sursa (job #77077)
Cod sursa(job #77077)
#include <cstdio>
#include <cstring>
const int maxn = 1001;
const int maxm = 10000;
FILE *in = fopen("critice.in","r"), *out = fopen("critice.out","w");
struct muchie
{
int x, y;
};
int n, m;
int a[maxn][maxn];
int f[maxn][maxn];
int viz[maxn];
int C[maxn];
muchie mc[maxm];
int crit[maxn];
void read()
{
fscanf(in, "%d %d", &n, &m);
for ( int i = 0; i < m; ++i )
{
fscanf(in, "%d %d", &mc[i].x, &mc[i].y);
fscanf(in, "%d\n", &a[ mc[i].x ][ mc[i].y ]);
//if ( mc[i].x != 1 && mc[i].y != 1 && mc[i].x != n && mc[i].y != n )
a[ mc[i].y ][ mc[i].x ] = a[ mc[i].x ][ mc[i].y ];
}
}
int findway()
{
int p = 0, u = 0;
memset(viz, 0, sizeof(viz));
int x = 0;
C[p] = 1;
viz[C[p]] = 1;
while ( p <= u && !viz[n] )
{
x = C[p++];
for ( int i = 1; i <= n; ++i )
if ( !viz[i] )
if ( f[x][i] < a[x][i] )
viz[i] = x, C[++u] = i;
else if ( f[i][x] > 0 )
viz[i] = -x, C[++u] = i;
}
return viz[n];
}
inline int min(int x, int y)
{
return x < y ? x : y;
}
inline int abs(int x)
{
return x < 0 ? -x : x;
}
void flux()
{
int L[maxn];
while ( findway() )
{
int lg = 0;
L[lg] = n;
int p = maxm << 1, q = maxm << 1, v = 0;
while ( L[lg] != 1 )
{
++lg;
L[lg] = abs(viz[ L[lg-1] ]);
if ( viz[ L[lg-1] ] >= 0 )
p = min(p, a[ L[lg] ][ L[lg-1] ] - f[ L[lg] ][ L[lg-1] ]);
else if ( viz[ L[lg-1] ] < 0 )
q = min(q, f[ L[lg-1] ][ L[lg] ]);
}
v = min(p, q);
for ( int i = lg; i; --i )
if ( viz[ L[i-1] ] >= 0 )
f[ L[i] ][ L[i-1] ] += v;
else
f[ L[i-1] ][ L[i] ] -= v;
}
}
void BF(int s)
{
int p = 0, u = 0;
memset(viz, 0, sizeof(viz));
int x = 0;
viz[s] = 1;
C[p] = s;
crit[s] = s;
while ( p <= u )
{
x = C[p++];
for ( int i = 1; i <= n; ++i )
if ( !viz[i] && f[x][i] < a[x][i] && f[i][x] < a[i][x] )
viz[i] = 1, crit[i] = s, C[++u] = i;
}
}
int main()
{
read();
flux();
BF(1);
BF(n);
int cnt = 0;
int answ[maxm];
for ( int i = 0; i < m; ++i )
if ( crit[ mc[i].x ] && crit[ mc[i].y ] && crit[ mc[i].x ] != crit[ mc[i].y ] )
//&& (f[ mc[i].x ][ mc[i].y ] == a[ mc[i].x ][ mc[i].y ])
//|| (f[ mc[i].y ][ mc[i].x ] == a[ mc[i].y ][ mc[i].x ]) )
answ[cnt++] = i+1;
fprintf(out, "%d\n", cnt);
for ( int i = 0; i < cnt; ++i )
fprintf(out, "%d\n", answ[i]);
return 0;
}