Pagini recente » Cod sursa (job #3235411) | Cod sursa (job #3235410) | Cod sursa (job #3274107) | Cod sursa (job #3270527) | Cod sursa (job #3274109)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in("critice.in");
ofstream out("critice.out");
int n, m, ok, ans;
vector<pair<int, int>> v[1005];
pair<int, int> muc[20005];
pair<int, int> viz[1005];
int viz2[1005];
int INF = (1 << 30);
void bfs()
{
viz[1] = {-1, -1};
queue<int> q;
q.push(1);
while(!q.empty())
{
int nod = q.front();
q.pop();
if(nod == n)
{
break;
}
for(auto it: v[nod])
{
if(viz[it.first].first == 0 && muc[it.second].second - muc[it.second].first > 0)
{
viz[it.first] = {nod, it.second};
q.push(it.first);
}
}
}
if(viz[n].first == 0)
{
return;
}
ok = 1;
int nod = n;
int flux = INF;
while(viz[nod].first != -1)
{
flux = min(flux, muc[viz[nod].second].second - muc[viz[nod].second].first);
nod = viz[nod].first;
}
nod = n;
while(viz[nod].first != -1)
{
if(viz[nod].second <= m)
{
muc[viz[nod].second].first += flux;
muc[viz[nod].second + m].first -= flux;
}
else
{
muc[viz[nod].second].first += flux;
muc[viz[nod].second - m].first -= flux;
}
nod = viz[nod].first;
}
}
void dfs2(int nod)
{
viz2[nod] = 1;
for(auto it: v[nod])
{
if(viz2[it.first] == 0 && muc[it.second].second - muc[it.second].first > 0)
{
dfs2(it.first);
}
}
}
int main()
{
in>>n>>m;
int x, y, z;
for(int i = 1; i<=m; i++)
{
in>>x>>y>>z;
v[x].push_back({y, i});
muc[i] = {0, z};
v[y].push_back({x, i + m});
muc[i + m] = {0, z};
}
ok = 1;
while(ok == 1)
{
ok = 0;
for(int j = 1; j<=n; j++)
{
viz[j] = {0, 0};
}
bfs();
}
dfs2(1);
for(int i = 1; i<=n; i++)
{
if(viz2[i] == 1)
{
for(auto it: v[i])
{
if(viz2[it.first] == 0)
{
ans++;
}
}
}
}
out<<ans<<'\n';
for(int i = 1; i<=n; i++)
{
if(viz2[i] == 1)
{
for(auto it: v[i])
{
if(viz2[it.first] == 0)
{
if(it.second <= m)
{
out<<it.second<<'\n';
}
else
{
out<<it.second - m<<'\n';
}
}
}
}
}
return 0;
}