Pagini recente » Cod sursa (job #2956047) | Cod sursa (job #2430277) | Cod sursa (job #2477356) | Cod sursa (job #195631) | Cod sursa (job #971309)
Cod sursa(job #971309)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
#define newn a[x][i]
#define ff first
#define ss second
using namespace std;
ifstream fin("critice.in");
ofstream fout("critice.out");
const int N = 1005;
const int M = 10005;
typedef pair <int, int> muchie;
int n, m, t[N], c[N][N], f[N][N], rez;
bool uz1[N], uz2[N];
vector <muchie> mc(M);
vector <int> a[N], sol;
vector <bool> viz(N);
queue <int> q;
bool bfs()
{
for(int i=1; i<=n; i++)
viz[i] = 0;
q.push(1); viz[1] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
for(unsigned i=0; i<a[x].size(); i++)
if(!viz[newn] && f[x][newn] < c[x][newn])
{
viz[newn] = 1;
t[newn] = x;
q.push(newn);
}
}
return viz[n];
}
void dfs(const int s, bool uz[])
{
for(int i=1; i<=n; i++)
viz[i] = 0;
q.push(s); viz[s] = 1; uz[s] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
for(unsigned i=0; i<a[x].size(); i++)
if(!uz[newn] && c[x][newn] > (f[x][newn] < 0 ? -f[x][newn] : f[x][newn]))
{
viz[newn] = 1;
uz[newn] = 1;
q.push(newn);
}
}
}
int main()
{
fin>>n>>m;
for(int i=1; i<=m; i++)
{
int x, y, r;
fin>>x>>y>>r;
a[x].push_back(y);
a[y].push_back(x);
c[x][y] = c[y][x] = r;
mc[i] = muchie(x, y);
}
while(bfs())
{
for(int i=1; i<n; i++)
if(f[i][n] < c[i][n])
{
int fmin = c[i][n] - f[i][n];
for(int j=i; j!=1; j=t[j])
fmin = min(fmin, c[t[j]][j] - f[t[j]][j]);
for(int j=i; j!=1; j=t[j])
{
f[t[j]][j] += fmin;
c[j][t[j]] -= fmin;
}
f[i][n] += fmin;
f[n][i] -= fmin;
rez += fmin;
}
}
dfs(1, uz1); dfs(n, uz2);
for(int i=1; i<=n; i++)
{
int x = mc[i].ff, y = mc[i].ss;
if(((uz1[x] && uz2[y]) || (uz1[y] && uz2[x])) && (c[x][y] == f[x][y] || c[y][x] == f[y][x]))
sol.push_back(i);
}
fout<<sol.size()<<'\n';
for(unsigned j=0; j<sol.size(); j++)
fout<<sol[j]<<'\n';
return 0;
}