Pagini recente » Cod sursa (job #3237898) | Cod sursa (job #2857080) | Cod sursa (job #2572930) | Cod sursa (job #988670) | Cod sursa (job #2595546)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int NMAX = 1000;
const int MMAX = 10000;
const int INF = 2.e9;
struct muchii
{
int x , y , cf;
muchii(int tx = 0 , int ty = 0 , int tcf = 0)
{
x = tx;
y = ty;
cf = tcf;
}
};
muchii mch[2 * MMAX + 5];
int viz[NMAX + 5] , viz1[NMAX + 5] , s , t , n;
vector <int> G[NMAX + 5];
int bfs()
{
memset(viz , 0 , sizeof(viz));
viz[s] = -1;
int u , v , j , muchie;
queue <int> q;
q.push(s);
while(!q.empty())
{
u = q.front();
q.pop();
if(u == t)
continue;
for(j = 0 ; j < G[u].size() ; j ++)
{
muchie = G[u][j];
v = mch[muchie].y;
if(!viz[v] && mch[muchie].cf > 0)
{
viz[v] = muchie;
q.push(v);
}
}
}
if(viz[t] == 0)
return 0;
return 1;
}
int get_flow()
{
int nod , flow;
flow = INF;
for(nod = t ; viz[nod] != -1 ; nod = mch[viz[nod]].x)
flow = min(flow , mch[viz[nod]].cf);
return flow;
}
void update_flow(int flow)
{
int nod;
for(nod = t ; viz[nod] != -1 ; nod = mch[viz[nod]].x)
{
mch[viz[nod]].cf = mch[viz[nod]].cf - flow;
mch[viz[nod] ^ 1].cf = mch[viz[nod] ^ 1].cf + flow;
}
}
void max_flow()
{
int muchie , nod , cf , j;
while(bfs())
for(j = 0 ; j < G[t].size() ; j ++)
{
muchie = G[t][j];
nod = mch[muchie].y;
cf = mch[muchie ^ 1].cf;
if(cf == 0 || !viz[nod])
continue;
viz[t] = muchie ^ 1;
update_flow(get_flow());
}
}
void bfs2(int start , int finish , int *d)
{
int muchie , u , v , j;
queue <int> q;
d[start] = 1;
q.push(start);
while(!q.empty())
{
u = q.front();
q.pop();
if(u == finish)
continue;
for(j = 0 ; j < G[u].size() ; j ++)
{
muchie = G[u][j];
v = mch[muchie].y;
if(!d[v] && mch[muchie].cf > 0)
{
d[v] = 1;
q.push(v);
}
}
}
}
int main()
{
freopen("critice.in" , "r" , stdin);
freopen("critice.out" , "w" , stdout);
int m , x , y , z , i , nr;
scanf("%d%d" , &n , &m);
for(i = 0 ; i < m ; i ++)
{
scanf("%d%d%d" , &x , &y , &z);
mch[2 * i] = muchii(x , y , z);
mch[2 * i + 1] = muchii(y , x , z);
G[x].push_back(2 * i);
G[y].push_back(2 * i + 1);
}
s = 1;
t = n;
max_flow();
for(i = 0 ; i < 2 * m ; i = i + 2)
if(mch[i].cf == 0)
viz[0] ++;
printf("%d\n" , viz[0]);
for(i = 0; i < 2 * m ; i = i + 2)
if(mch[i].cf == 0)
printf("%d\n" , i / 2 + 1);
return 0;
}