Pagini recente » Cod sursa (job #529162) | Cod sursa (job #2412733) | Cod sursa (job #3163103) | Cod sursa (job #2413456) | Cod sursa (job #2924781)
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream in ("critice.in");
ofstream out ("critice.out");
const int max_size = 1e3 + 1, INF = 1e9 + 1;
struct str{
int x, y;
};
int cap[max_size][max_size], ct[max_size][max_size], t[max_size], n;
vector <int> mc[max_size], ans;
vector <str> muchii;
bitset <max_size> viz;
queue <int> q;
bool bfsf ()
{
for (int i = 1; i <= n; i++)
{
t[i] = 0;
viz[i] = 0;
}
viz[1] = 1;
q.push(1);
while (!q.empty())
{
int nod = q.front();
q.pop();
if (nod == n)
{
return true;
}
for (auto f : mc[nod])
{
if (viz[f] == 0 && cap[nod][f] > 0)
{
t[f] = nod;
viz[f] = 1;
q.push(f);
}
}
}
return false;
}
void bfs (int nod)
{
q.push(nod);
while (!q.empty())
{
nod = q.front();
viz[nod] = 1;
q.pop();
for (auto f : mc[nod])
{
if (viz[f] == 0)
{
if (cap[f][nod] == 0 || cap[nod][f] == 0)
{
ct[f][nod]++;
ct[nod][f]++;
}
else
{
q.push(f);
}
}
}
}
}
int main ()
{
int m;
in >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, z;
in >> x >> y >> z;
muchii.push_back({x, y});
cap[x][y] = z;
cap[y][x] = z;
mc[x].push_back(y);
mc[y].push_back(x);
}
int flux = 0;
while (bfsf())
{
for (auto f : mc[n])
{
if (cap[f][n] <= 0 || viz[f] == 0)
continue;
t[n] = f;
int nod = n, mn = INF;
while (nod != 1)
{
mn = min(mn, cap[t[nod]][nod]);
nod = t[nod];
}
if (mn == 0)
continue;
nod = n;
while (nod != 1)
{
cap[t[nod]][nod] -= mn;
cap[nod][t[nod]] += mn;
nod = t[nod];
}
flux += mn;
}
}
//out << flux << '\n';
for (int i = 1; i <= n; i++)
{
viz[i] = 0;
}
bfs(1);
for (int i = 1; i <= n; i++)
{
viz[i] = 0;
}
bfs(n);
for (int i = 0; i < muchii.size(); i++)
{
if (ct[muchii[i].x][muchii[i].y] == 2)
{
ans.push_back(i + 1);
}
}
out << ans.size() << '\n';
for (auto f : ans)
{
out << f << '\n';
}
in.close();
out.close();
return 0;
}