Pagini recente » Cod sursa (job #2871839) | Cod sursa (job #2478719) | Cod sursa (job #2752915) | Cod sursa (job #2661249) | Cod sursa (job #2797158)
#include <bits/stdc++.h>
using namespace std;
ifstream in ("critice.in");
ofstream out("critice.out");
const int nmax = 1e3, mmax = 1e4;
int n, m, source, sink;
int rez[nmax + 2][nmax + 2];
vector <int> muchii[nmax + 2];
int lay[nmax + 2];
int ptrM[nmax + 2];
bool bfs(){
fill(lay + 1, lay + n + 1, -1);
queue <int> q;
lay[source] = 0;
q.push(source);
while(!q.empty()){
int elem = q.front(); q.pop();
for(auto & x:muchii[elem])
if(lay[x] == -1 && rez[elem][x] != 0){
lay[x] = lay[elem] + 1;
q.push(x);
}
}
return (lay[sink] != -1);
}
int dfs(int nod, int f){
if(nod == sink)
return f;
for(;ptrM[nod] < muchii[nod].size(); ptrM[nod]++){
int x = muchii[nod][ptrM[nod]];
if(lay[nod] == lay[x] - 1 && rez[nod][x] != 0){
int ret = dfs(x, min(f, rez[nod][x]));
if(ret == -1)
continue;
rez[nod][x] -= ret;
rez[x][nod] += ret;
return ret;
}
}
return -1;
}
vector <pair<int, int> > mch;
bool mrk1[nmax + 2], mrk2[nmax + 2];
void bfs1(){
queue <int> q;
mrk1[source] = true;
q.push(source);
while(!q.empty()){
int elem = q.front(); q.pop();
for(auto & x:muchii[elem])
if(mrk1[x] == false && rez[elem][x] != 0){
mrk1[x] = true;
q.push(x);
}
}
}
void bfs2(){
queue <int> q;
mrk2[sink] = true;
q.push(sink);
while(!q.empty()){
int elem = q.front(); q.pop();
for(auto & x:muchii[elem])
if(mrk2[x] == false && rez[x][elem] != 0){
mrk2[x] = true;
q.push(x);
}
}
}
int main()
{
in >> n >> m;
source = 1, sink = n;
for(int i = 1; i <= m; i++){
int x, y, c;
in >> x >> y >> c;
mch.push_back({x, y});
rez[x][y] = c;
rez[y][x] = c;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
while(bfs()){
fill(ptrM + 1, ptrM + n + 1, 0);
while(dfs(source, 1000000) != -1);
}
bfs1();
bfs2();
vector <int> ans;
for(int i = 0; i < mch.size(); i++){
pair<int, int> & x = mch[i];
if( (mrk1[x.first] == true && mrk2[x.second] == true) || (mrk2[x.first] == true && mrk1[x.second] == true) )
ans.push_back(i + 1);
}
out << ans.size() << "\n";
for(auto & x:ans)
out << x << "\n";
return 0;
}