Pagini recente » Cod sursa (job #1332301) | Cod sursa (job #2527727) | Cod sursa (job #1491419) | Cod sursa (job #2700177) | Cod sursa (job #573425)
Cod sursa(job #573425)
#include<fstream>
#include<iostream>
#include<vector>
using namespace std;
int n,sw[1005],C[1000000],t[1005],flux[1005][1006],cap[1006][1006],mc[1005][1006];
vector <int > v[1005],sol;
int bf(int x, int y)
{
vector<int>:: iterator it;
int p,u;
memset(sw,0,sizeof(sw));
p=u=1;sw[1]=1;
C[p]=1;
while(p<=u)
{
for (it=v[C[p]].begin();it<v[C[p]].end();++it)
if (sw[*it]==0 && flux[C[p]][*it]<cap[C[p]][*it])
{
sw[*it]=1;
C[++u]=*it;
}
++p;
}
if (sw[x]==0 || sw[y]==1)
return 0;
p=u=1;sw[y]=1;
C[p]=y;
while(p<=u)
{
for (it=v[C[p]].begin();it<v[C[p]].end();++it)
if (sw[*it]==0 && flux[C[p]][*it]<cap[C[p]][*it])
{
sw[*it]=1;
C[++u]=*it;
}
++p;
}
return sw[n];
}
void critice()
{
int i,j;
for (i=1;i<n;i++)
for (j=i+1;j<=n;j++)
if (mc[i][j]>0 && flux[i][j]==cap[i][j])
{
if (bf(i,j)==1)
sol.push_back(mc[i][j]);
}
else
if (mc[i][j]>0 && flux[j][i]==cap[j][i])
if (bf(j,i)==1)
sol.push_back(mc[i][j]);
}
int maxflux()
{
vector<int >:: iterator it;
int s,i,fmax,p,u,k;
p=u=1;
C[1]=1;
memset(sw,0,sizeof(sw));
sw[1]=1;
while(p<=u)
{
for (it=v[C[p]].begin();it<v[C[p]].end();++it)
if (sw[*it]==0 && cap[C[p]][*it]>flux[C[p]][*it] )
{
sw[*it]=1;
t[*it]=C[p];
C[++u]=*it;
}
++p;
}
s=0;
for (i=1;i<n;i++)
if (sw[i]==1 && cap[i][n]>flux[i][n])
{
fmax=cap[i][n]-flux[i][n];
for (k=i;k>1;k=t[k])
fmax=min(fmax,cap[t[k]][k]-flux[t[k]][k]);
flux[i][n]+=fmax;
for (k=i;k>1;k=t[k])
{
flux[t[k]][k]+=fmax;
flux[k][t[k]]-=fmax;
}
s+=fmax;
}
return s;
}
void flow()
{
int i,sol;
i=1;sol=0;
while(i!=sol)
{
i=sol;
sol+=maxflux();
}
}
void afisare()
{
vector<int> ::iterator it;
ofstream g("critice.out");
sort(sol.begin(),sol.end());
g<<sol.size()<<'\n';
for (it=sol.begin();it<sol.end();++it)
g<<*it<<'\n';
g.close();
}
void citire()
{
int i,m,x,y,c;
ifstream f("critice.in");
f>>n>>m;
for (i=1;i<=m;i++)
{
f>>x>>y>>c;
mc[x][y]=mc[y][x]=i;
cap[x][y]=cap[y][x]=c;
v[x].push_back(y);
v[y].push_back(x);
}
f.close();
}
int main()
{
citire();
flow();
critice();
afisare();
return 0;
}