Pagini recente » Cod sursa (job #393993) | Cod sursa (job #2046220) | Cod sursa (job #175925) | Cod sursa (job #593867) | Cod sursa (job #1727059)
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#define se second
#define fi first
using namespace std;
ifstream f ("critice.in");
ofstream g ("critice.out");
int t[1005] , c[1005][1005] , flow[1005][1005] , v[5][1005];
int n , m , cost , flux , node1 , node2 , x , y;
vector <vector <int> > G;
vector <int> sol;
pair <int , int> edge[10005];
bool bfs();
void maxflow();
void bfs1(int node , int pos) {
queue <int> q;
q.push(node);
v[pos][node] = 1;
memset(t , 0 , sizeof(t));
t[1] = -1;
while(!q.empty()) {
int node = q.front();
for(vector <int> :: iterator it = G[node].begin() ; it != G[node].end() ; ++it) {
int qw = *it;
if(v[pos][*it] == 0 && abs(flow[node][*it]) < c[node][*it]) {
q.push(*it);
t[*it] = node;
v[pos][*it] = 1;
}
}
q.pop();
}
}
int main() {
f >> n >> m;
G.resize(n + 5);
for (int i = 1; i <= m; ++i) {
f >> x >> y >> cost;
edge[i].fi = x;
edge[i].se = y;
c[x][y] = cost;
c[y][x] = cost;
G[x].push_back(y);
G[y].push_back(x);
}
maxflow();
bfs1(1 , 1);
bfs1(n , 2);
for (int i = 1; i <= m; ++i) {
int node1 = edge[i].fi;
int node2 = edge[i].se;
if ((v[1][node1] == 1 && v[2][node2] == 1) || (v[2][node1] == 1 && v[1][node2] == 1)) {
sol.push_back(i);
}
}
g << sol.size() << '\n';
for (vector <int> :: iterator it = sol.begin(); it != sol.end(); ++it) {
g << *it << '\n';
}
return 0;
}
void maxflow() {
while(bfs()) {
for(vector <int>::iterator it = G[n].begin() ; it != G[n].end() ; ++it) {
if(flow[*it][n] < c[*it][n] && t[*it]) {
int u = *it , val = c[*it][n] - flow[*it][n];
while(u != 1) {
val = min(val , c[t[u]][u] - flow[t[u]][u]);
u = t[u];
}
u = *it;
flow[*it][n] += val;
flow[n][*it] -= val;
while(u != 1)
{
flow[t[u]][u] += val;
flow[u][t[u]] -= val;
u = t[u];
}
flux += val;
}
}
}
}
bool bfs() {
queue <int> q;
q.push(1);
memset(t , 0 , sizeof(t));
t[1] = -1;
while(!q.empty()) {
int node = q.front();
for(vector <int> :: iterator it = G[node].begin() ; it != G[node].end() ; ++it) {
if(flow[node][*it] < c[node][*it] && !t[*it]) {
q.push(*it);
t[*it] = node;
}
}
q.pop();
}
return (t[n] != 0);
}