Pagini recente » Cod sursa (job #1049757) | Cod sursa (job #1287831) | Cod sursa (job #2566046) | Cod sursa (job #3142040) | Cod sursa (job #1567258)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
int oo = 1 << 30, n;
vector<int> V[1005];
int c[1005][1005];
int f[1005][1005];
int prec[1005];
vector<int> aux;
queue<int> Q;
void bfs() {
prec[1] = -1; Q.push(1);
while(!Q.empty()) {
int top = Q.front(); Q.pop();
for(int i = 0; i < V[top].size(); i ++) {
int cur = V[top][i];
if(c[top][cur] && !prec[cur]) {
prec[cur] = top;
if(c[cur][n]) aux.push_back(cur);
else Q.push(cur);
}
}
}
}
vector< pair<int, int> > M;
int poss[10005];
bool verif(int i);
int main()
{
M.push_back(make_pair(0, 0));
ifstream d("critice.in");
ofstream g("critice.out");
int m, x, y, z; d >> n >> m;
for(int i = 1; i <= m; i ++) {
d >> x >> y >> z;
M.push_back(make_pair(x, y));
V[x].push_back(y);
V[y].push_back(x);
c[x][y] = c[y][x] = z;
f[x][y] = f[y][x] = i;
}
while(true) {
bfs();
if(!aux.size()) break;
for(int i = 0; i < aux.size(); i ++) {
int j = aux[i];
int mn = c[j][n];
for(int l = j; prec[l] != -1; l = prec[l]) {
mn = min(mn, c[prec[l]][l]);
}
c[j][n] -= mn; c[n][j] -= mn; if(!c[j][n]) poss[f[j][n]] = true;
for(int l = j; prec[l] != -1; l = prec[l]) {
c[prec[l]][l] -= mn; c[l][prec[l]] -= mn;
if(!c[prec[l]][l]) poss[f[prec[l]][l]] = true;
}
}
for(int i = 1; i <= n; i ++) prec[i] = 0;
while(!aux.empty()) aux.pop_back();
}
for(int i = 1; i <= m; i ++)
if(poss[i] && verif(i)) aux.push_back(i);
g << aux.size() << "\n";
for(int i = 0; i < aux.size(); i ++) g << aux[i] << "\n";
return 0;
}
void DFS(int node);
bool viz[1005];
bool verif(int i) {
bool ok1 = false, ok2 = false;
for(int j = 1; j <= n; j ++) viz[j] = false;
DFS(M[i].first);
ok1 = viz[1];
ok2 = viz[n];
for(int j = 1; j <= n; j ++) viz[j] = false;
DFS(M[i].second);
ok1 |= viz[1];
ok2 |= viz[n];
if(ok1 && ok2) return true;
return false;
}
void DFS(int node) {
viz[node] = true;
for(int i = 0; i < V[node].size(); i ++) {
int now = V[node][i];
if(!viz[now] && c[now][node]) DFS(now);
}
}