# Cod sursa(job #2628841)

Utilizator Data 17 iunie 2020 18:10:23 Traseu 100 cpp-64 done Arhiva de probleme 1.88 kb
``````#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("traseu.in");
ofstream cout("traseu.out");
const int inf=1e9+7;
vector<pair<int,int> > vec[70];
int out[70],in[70];
int c[70][70];
int pred[70];
int dist[70];
queue<int> q;
bool inq[70];
int main()
{
int n,m,x,y,z;
cin>>n>>m;
int sum=0;
for(int i=1;i<=m;++i)
{
cin>>x>>y>>z;
vec[x].push_back({y,z});
vec[y].push_back({x,-z});
c[x][y]=inf;
sum+=z;
++out[x];
++in[y];
}
for(int i=1;i<=n;++i)
if(in[i]>out[i])
{
vec[0].push_back({i,0});
vec[i].push_back({0,0});
c[0][i]=in[i]-out[i];
}
else if(out[i]>in[i])
{
vec[i].push_back({n+1,0});
vec[n+1].push_back({i,0});
c[i][n+1]=out[i]-in[i];
}
do
{
for(int i=0;i<=n+1;++i)
{
pred[i]=-1;
dist[i]=inf;
}
dist[0]=0;
inq[0]=true;
q.push(0);
while(!q.empty())
{
int x=q.front(); q.pop();
inq[x]=false;
for(auto y:vec[x])
if(c[x][y.first]>0 and dist[y.first]>dist[x]+y.second)
{
dist[y.first]=dist[x]+y.second;
pred[y.first]=x;
if(!inq[y.first]) inq[y.first]=true,q.push(y.first);
}
}
if(pred[n+1]>=0)
{
for(int k=n+1;pred[k]>=0;k=pred[k])