Pagini recente » Cod sursa (job #2356364) | Cod sursa (job #1405171) | Cod sursa (job #1681299) | Cod sursa (job #2779494) | Cod sursa (job #2288320)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 1005, maxm = 5005, inf = 0x3f3f3f3f;
ifstream f("critice.in");
ofstream g("critice.out");
int n, m, i, ans;
struct xy {
int x, y;
};
vector <int> gr[maxn];
int c[maxn][maxn], us[maxn][maxn]; // cost, used
int p[maxn]; bool is[maxn];
queue <int> q;
int rez[maxm], rezk;
int eligible[maxm];
int id[maxn][maxn];
bool bfs()
{
memset(is, false, sizeof(is));
while(!q.empty()) {
q.pop();
}
q.push(1);
is[1] = true;
while(!q.empty())
{
int x = q.front();
q.pop();
if(x == n) { continue; }
for(auto u : gr[x])
{
if(!(is[u] == true || us[x][u] == c[x][u])) {
p[u] = x;
q.push(u);
is[u] = true;
}
}
}
return is[n];
}
int maxflow(bool first)
{
ans = 0;
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= n; j ++) {
us[i][j] = 0;
}
}
while(bfs())
{
for(auto u : gr[n])
{
if(us[u][n] == c[u][n] || is[u] == false) {
continue;
}
p[n] = u;
int minim = inf;
xy imin;
for(int nod = n; nod != 1; nod = p[nod]) {
int cst = c[p[nod]][nod] - us[p[nod]][nod];
if(cst < minim) {
minim = cst;
imin = {p[nod], nod};
}
}
eligible[ id[imin.x][imin.y] ] = true;
//cout << imin.x << ' ' << imin.y << '\n';
for(int nod = n; nod != 1; nod = p[nod]) {
us[p[nod]][nod] += minim;
us[nod][p[nod]] -= minim;
}
ans += minim;
}
}
return ans;
}
xy v[maxm];
int main()
{
int x, y, z;
f >> n >> m;
for(i = 1; i <= m ; i++)
{
f >> x >> y >> z;
v[i] = {x, y};
gr[x].push_back(y);
gr[y].push_back(x);
id[x][y] = i; id[y][x] = i;
c[x][y] = z;
c[y][x] = z;
}
int initial = maxflow(true);
for(i = 1; i <= m; i ++)
{
if(eligible[i] == false) {
continue;
}
int aux = c[v[i].x][v[i].y];
c[v[i].x][v[i].y] = inf;
c[v[i].y][v[i].x] = inf;
int now = maxflow(false);
if(now > initial) {
rez[++rezk] = i;
}
c[v[i].x][v[i].y] = aux;
c[v[i].y][v[i].x] = aux;
}
g << rezk << '\n';
for(i = 1; i <= rezk; i ++) {
g << rez[i] << '\n';
}
f.close();
g.close();
return 0;
}