Pagini recente » Cod sursa (job #909182) | Cod sursa (job #2325500) | Cod sursa (job #2206493) | Cod sursa (job #986022) | Cod sursa (job #2681404)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
const int NMAX = 1000;
int N, M;
vector <int> g[NMAX + 5];
int capacity[NMAX + 5][NMAX + 5], flow[NMAX + 5][NMAX + 5];
bool seen[NMAX + 5];
int bef[NMAX + 5];
bool d1[NMAX + 5], d2[NMAX + 5];
bool BFS()
{
for(int i = 1; i <= N; i++)
seen[i] = 0;
queue <int> Q;
Q.push(1);
seen[1] = true;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(auto it : g[nod])
if(!seen[it] && flow[nod][it] < capacity[nod][it])
{
seen[it] = true;
bef[it] = nod;
Q.push(it);
}
}
return seen[N];
}
void BFS1()
{
queue <int> Q;
Q.push(1);
d1[1] = true;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(auto it : g[nod])
if(!d1[it] && flow[nod][it] < capacity[nod][it])
{
d1[it] = true;
Q.push(it);
}
}
}
void BFS2()
{
queue <int> Q;
Q.push(N);
d2[N] = true;
while(!Q.empty())
{
int nod = Q.front();
Q.pop();
for(auto it : g[nod])
if(!d2[it] && -flow[nod][it] < capacity[nod][it])
{
d2[it] = true;
Q.push(it);
}
}
}
vector < pair < pair <int, int>, int> > edges;
int main()
{
fin >> N >> M;
for(int i = 1; i <= M; i++)
{
int x, y, c;
fin >> x >> y >> c;
g[x].push_back(y);
g[y].push_back(x);
edges.push_back({{x, y}, i});
capacity[x][y] = capacity[y][x] = c;
}
while(BFS())
{
for(auto it : g[N])
{
int adFlow = capacity[it][N] - flow[it][N];
for(int i = it; i != 1; i = bef[i])
adFlow = min(adFlow, capacity[bef[i]][i] - flow[bef[i]][i]);
flow[it][N] += adFlow;
flow[N][it] -= adFlow;
for(int i = it; i != 1; i = bef[i])
{
flow[bef[i]][i] += adFlow;
flow[i][bef[i]] -= adFlow;
}
}
}
BFS1();
BFS2();
vector <int> sol;
for(auto it : edges)
{
int x = it.first.first;
int y = it.first.second;
if((d1[x] && !d1[y] && d2[y] && !d2[x]) || (d2[x] && !d2[y] && d1[y] && !d1[x]))
sol.push_back(it.second);
}
fout << sol.size() << '\n';
for(auto it : sol)
fout << it << '\n';
return 0;
}