Pagini recente » Cod sursa (job #2328226) | Cod sursa (job #2919833) | Cod sursa (job #2767296) | Cod sursa (job #788455) | Cod sursa (job #2582721)
#define NMAX 1005
#define oo 0x3f3f3f3f
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
ifstream f("critice.in");
ofstream g("critice.out");
typedef pair<pair<int, int>, int> tii;
int n, m;
int viz[NMAX], t[NMAX], vizr[NMAX];
int flo[NMAX][NMAX], cap[NMAX][NMAX];
vector<tii> edges;
vector<int> graph[NMAX];
vector<int> sol;
void read()
{
int x,y,t;
f>>n>>m;
for(int i = 1; i <= m; ++i)
{
f>>x>>y>>t;
cap[x][y] = cap[y][x] = t;
graph[x].push_back(y);
graph[y].push_back(x);
edges.push_back({{x, y}, i});
}
}
bool bfs()
{
queue<int> Q;
memset(viz, 0, sizeof(viz));
Q.push(1);
viz[1] = 1;
while(!Q.empty())
{
int from = Q.front();
Q.pop();
for(auto &v:graph[from])
if(viz[v] == 0 && cap[from][v] - flo[from][v] > 0)
{
viz[v] = 1;
t[v] = from;
if(v != n)
Q.push(v);
}
}
return viz[n];
}
void edmond_karp()
{
while(bfs())
{
for(auto &fin:graph[n])
{
if(viz[fin] == 0 || cap[t[fin]][fin] - flo[t[fin]][fin] == 0)
continue;
int act_flomax = oo;
t[n] = fin;
for(int v = n; v != 1; v = t[v])
act_flomax = min(act_flomax, cap[t[v]][v] - flo[t[v]][v]);
for(int v = n; v != 1; v = t[v])
{
flo[t[v]][v] += act_flomax;
flo[v][t[v]] -= act_flomax;
}
}
}
}
void bfs_from_left()
{
queue<int> Q;
memset(viz, 0, sizeof(viz));
Q.push(1);
viz[1] = 1;
while(!Q.empty())
{
int from = Q.front();
Q.pop();
for(auto &v:graph[from])
if(viz[v] == 0 && flo[from][v] < cap[from][v])
{
viz[v] = 1;
Q.push(v);
}
}
}
void bfs_from_right()
{
queue<int> Q;
Q.push(n);
vizr[n] = 1;
while(!Q.empty())
{
int from = Q.front();
Q.pop();
for(auto &v:graph[from])
if(vizr[v] == 0 && -flo[from][v] < cap[from][v])
{
vizr[v] = 1;
Q.push(v);
}
}
}
void findrez()
{
for(int i = 0; i < m; ++i)
{
tii act = edges[i];
if(abs(flo[act.first.first][act.first.second]) == cap[act.first.first][act.first.second] &&
(viz[act.first.first] && vizr[act.first.second] || viz[act.first.second] && vizr[act.first.first]))
sol.push_back(act.second);
}
g<<sol.size()<<'\n';
for(auto &v:sol)
g<<v<<"\n";
}
int main()
{
read();
edmond_karp();
bfs_from_left();
bfs_from_right();
findrez();
return 0;
}