Pagini recente » Cod sursa (job #194569) | Cod sursa (job #3040025) | Cod sursa (job #38068) | Cod sursa (job #91730) | Cod sursa (job #3193576)
#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 < pair <int, 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,1e9});
uz.reset();
uz[1] = 1;
while(!q.empty()){
for(auto x : G[q.front().first]){
if(!uz[x] && cap[q.front().first][x] > f[q.front().first][x]){
uz[x] = 1;
t[x] = q.front().first;
int new_flow = min(q.front().second, cap[t[x]][x] - f[t[x]][x]);
if(x == n) return new_flow;
q.push({x, new_flow});
}
}
q.pop();
}
return 0;
}
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(){
int flow = 0, new_flow;
while(new_flow = bfs()){
flow += new_flow;
int e = n;
while(t[e] != -1){
f[t[e]][e] += new_flow;
f[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(2, 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;
}