Pagini recente » Cod sursa (job #1238031) | Cod sursa (job #1463211) | Cod sursa (job #2049149) | Cod sursa (job #2388139) | Cod sursa (job #1123723)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int NMax = 1024, MMax = 10010;
queue <int> Q;
int from[NMax];
bool viz[NMax];
bool v[2][NMax];
int cap[NMax][NMax];
int flow[NMax][NMax];
int n, m;
vector <int> G[NMax], ans;
struct muchie
{
int x, y, c;
muchie () {x = y = c;}
muchie (const int x, const int y, const int c)
{
this -> x = x;
this -> y = y;
this -> c = c;
}
};
muchie a[MMax];
void Read()
{
ifstream f("critice.in");
f>>n>>m;
for (int i = 1; i<=m; ++i)
{
int x, y, c; f>>x>>y>>c;
G[x].push_back(y);
G[y].push_back(x);
cap[x][y] = cap[y][x] = c;
a[i] = muchie (x, y, c);
}
f.close();
}
bool BFS()
{
for (int i = 1; i<=n; ++i)
viz[i] = false, from[i] = 0;
viz[1] = true;
Q.push(1);
while (!Q.empty())
{
int node = Q.front(); Q.pop();
for (vector <int> :: iterator it = G[node].begin(); it!=G[node].end(); ++it)
if (!viz[*it] && cap[node][*it] > flow[node][*it])
{
from[*it] = node;
viz[*it] = true;
Q.push(*it);
}
}
return viz[n];
}
void MaxFlow()
{
while (BFS())
{
for (vector <int> :: iterator it = G[n].begin(); it!=G[n].end(); ++it)
{
int i = *it;
if (cap[i][n] > flow[i][n] && from[i])
{
int localflow = cap[i][n] - flow[i][n];
int node = i;
while (node != 1)
{
localflow = min(localflow, cap[from[node]][node] - flow[from[node]][node]);
node = from[node];
}
if (localflow == 0)
continue;
flow[i][n] += localflow;
flow[n][i] -= localflow;
node = i;
while (node != 1)
{
flow[from[node]][node] += localflow;
flow[node][from[node]] -= localflow;
node = from[node];
}
}
}
}
}
void DFS(const int &node, const int &ind)
{
v[ind][node] = true;
for (vector <int> :: iterator it = G[node].begin(); it != G[node].end(); ++it)
if (!v[ind][*it] && cap[node][*it] > flow[node][*it])
DFS(*it, ind);
}
void Solve()
{
MaxFlow();
DFS(1, 0);
DFS(n, 1);
for (int i = 1; i<=m; ++i)
{
int x, y, c;
x = a[i].x;
y = a[i].y;
c = a[i].c;
if ((v[0][x] && v[1][y] && flow[x][y] == cap[x][y]) || (v[0][y] && v[1][x] && flow[y][x] == cap[y][x]))
ans.push_back(i);
}
}
void Write()
{
ofstream g("critice.out");
g<<ans.size()<<"\n";
for (vector <int> :: iterator it = ans.begin(); it!=ans.end(); ++it)
g<<*it<<"\n";
g.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}