Pagini recente » Cod sursa (job #2667910) | Cod sursa (job #631103) | Cod sursa (job #597470) | Cod sursa (job #806020) | Cod sursa (job #675343)
Cod sursa(job #675343)
#include <fstream>
#include <vector>
#include <queue>
#define nmax 1024
#define mmax 10004
#define INF (1<<30)
using namespace std;
ifstream in("critice.in");
ofstream out("critice.out");
inline int _min(int a,int b){if(a<b) return a;return b;}
queue<int> Q;
vector<int> V[nmax];
int Mx[mmax],My[mmax],Mc[mmax],T[nmax];
int C[nmax][nmax];
int L[nmax][nmax];
bool Critic[mmax];
int M,N,critic;
bool BS(int S,int D)
{
if(S==D)
return 1;
int x,i,y;
while(!Q.empty())Q.pop();
for(i=1;i<=N;i++)T[i]=0;
Q.push(S);
while(!Q.empty())
{
x = Q.front();
Q.pop();
for(i=0;i<V[x].size();i++)
{
y = V[x][i];
if(!T[y]&&C[x][y])
{
T[y] = x;
Q.push(y);
if(y==D)
return 1;
}
}
}
return 0;
}
int main()
{
int i,x,y,muchie_minima,t,tt;
in>>N>>M;
for(i=1;i<=M;i++)
{
in>>Mx[i]>>My[i]>>Mc[i];
V[Mx[i]].push_back(My[i]);
V[My[i]].push_back(Mx[i]);
C[Mx[i]][My[i]] +=Mc[i];
C[My[i]][Mx[i]] +=Mc[i];
}
while(BS(1,N))
{
for(i=0;i<V[N].size();i++)
{
x = V[N][i];
if(!T[x]||!C[x][N])continue;
T[N] = x;
muchie_minima = INF;
x = N;
while(x!=1)
muchie_minima=_min(muchie_minima,C[T[x]][x]),x=T[x];
if(!muchie_minima)continue;
x = N;
while(x!=1)
C[T[x]][x]-=muchie_minima,C[x][T[x]]+=muchie_minima,x=T[x];
}
}
for(i=1;i<=M;i++)
if(!C[Mx[i]][My[i]]||!C[My[i]][Mx[i]])
if((BS(Mx[i],N)&&BS(1,My[i]))||(BS(1,Mx[i])&&BS(My[i],N)))
critic++,Critic[i]=1;
out<<critic<<'\n';
for(i=1;i<=M;i++)
if(Critic[i])
out<<i<<' ';
return 0;
}