Pagini recente » Cod sursa (job #1187444) | Cod sursa (job #1521290) | Cod sursa (job #1863946) | Cod sursa (job #2865746) | Cod sursa (job #3193584)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
int n;
vector <int> G[1002];
int cap[1002][1002], f[1002][1002];
queue <int> q;
int t[1002];
bitset <1002> uz;
bool v1[1002], v2[1002];
int bfs(){
while(!q.empty()) q.pop();
memset(t, -1, sizeof t);
q.push(1);
uz.reset();
uz[1] = 1;
while(!q.empty()){
for(auto x : G[q.front()]){
if(!uz[x] && cap[q.front()][x] > f[q.front()][x]){
uz[x] = 1;
t[x] = q.front();
q.push(x);
}
}
q.pop();
}
return uz[n];
}
queue <int> q2;
void bfs2(int start, bool v[]){
while(!q2.empty()) q2.pop();
q2.push(start);
v[start] = 1;
while(!q2.empty()){
for(auto x : G[q2.front()]){
if(!v[x] && cap[q2.front()][x] > f[q2.front()][x]){
v[x] = 1;
q2.push(x);
}
}
q2.pop();
}
}
void bfs3(int start, bool v[]){
while(!q2.empty()) q2.pop();
q2.push(start);
v[start] = 1;
while(!q2.empty()){
for(auto x : G[q2.front()]){
if(!v[x] && -f[q2.front()][x] < cap[q2.front()][x]){
v[x] = 1;
q2.push(x);
}
}
q2.pop();
}
}
void edmonds_karp(){
while(bfs()){
for(auto x : G[n]){
if(!uz[x] || cap[t[x]][x] <= f[t[x]][x]) continue;
int e = x, new_flow = 1e9;
while(t[e] != -1){
new_flow = min(new_flow, cap[t[e]][e] - f[t[e]][e]);
e = t[e];
}
e = x;
if(!new_flow) continue;
while(t[e] != -1){
cap[t[e]][e] -= new_flow;
cap[e][t[e]] += new_flow;
e = t[e];
}
}
}
}
vector < pair <int, int> > edges;
vector <int> sol;
int main()
{
int i,m,u,v,c,t = 0;
fin >> n >> m;
for(i = 1; i <= m; i++){
fin >> u >> v >> c;
G[u].push_back(v);
G[v].push_back(u);
cap[u][v] = cap[v][u] = c;
edges.push_back({u,v});
}
edmonds_karp();
bfs2(1, v1);
bfs3(n, v2);
for(i = 0; i < edges.size(); i++){
auto x = edges[i];
if((v1[x.first] & v2[x.second]) || (v2[x.first] & v1[x.second])){
sol.push_back(i + 1);
t++;
}
}
fout << t << "\n";
for(auto x : sol) fout << x << "\n";
return 0;
}