Pagini recente » Cod sursa (job #2806133) | Cod sursa (job #1077997) | Cod sursa (job #3186711) | Cod sursa (job #114215) | Cod sursa (job #1395123)
#include <fstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <queue>
#include <set>
#define mp make_pair
#define pb push_back
#define INF numeric_limits<int>::max()
#define int64 long long
using namespace std;
ifstream in("critice.in");
ofstream out("critice.out");
vector< vector<int> > a;
queue<int> q;
set<int> sol;
int flow[1005][1005],n,k,s,d,maxflow,pre[1005],f[1005],v1[1005],v2[1005];
vector< pair<int,int> > mc;
//bfs
void bfs(int start)
{
for(int i=1;i<=n;i++)pre[i]=0;
k=0;
pre[s]=s;
q.push(start);
while(!q.empty())
{
int x=q.front();q.pop();
for(auto i: a[x])
if(pre[i]==0 && flow[x][i]>0)
{
if(i==d)
{
f[++k]=x;
continue;
}
pre[i]=x;
q.push(i);
}
}
}
void bf(int start,int v[])
{
v[start]=1;
q.push(start);
while(!q.empty())
{
int x=q.front();q.pop();
for(auto i: a[x])
if(flow[x][i] && v[i]==0)
{
v[i]=1;
q.push(i);
}
}
}
int main()
{
int m;
in>>n>>m;
a=vector< vector<int> > (n+1);
for(int i=1;i<=m;i++)
{
int x,y,z;
in>>x>>y>>z;
a[x].pb(y);
a[y].pb(x);
flow[x][y]=flow[y][x]=z;
mc.pb({x,y});
}
for(s=1,d=n;;)//sursa, destinatie
{
bfs(s);
if(k==0)break;
for(int i=1;i<=k;i++)
{
int mx=INF;
pre[d]=f[i];
for(int x=d;pre[x]!=x;x=pre[x])
mx=min(mx,flow[pre[x]][x]);
for(int x=d;pre[x]!=x;x=pre[x])
{
flow[pre[x]][x]-=mx;
flow[x][pre[x]]+=mx;
}
maxflow+=mx;
}
}
bf(s,v1);
bf(d,v2);
for(unsigned i=0;i<m;i++)
if((v1[mc[i].first] && v2[mc[i].second]) || (v2[mc[i].first] && v1[mc[i].second]) && flow[mc[i].first][mc[i].second]==0)
sol.insert(i+1);
out<<sol.size()<<'\n';
for(auto i: sol)
out<<i<<'\n';
return 0;
}