Pagini recente » Cod sursa (job #2881659) | Cod sursa (job #789184) | Cod sursa (job #983901) | Cod sursa (job #2329607) | Cod sursa (job #1129095)
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
vector<int> a[355];
queue <int> qu;
int viz[355],csm,pred[355],n,m,pi,i,x,y,dist[355],pf,cst[355][355],cost[355][355],fl[355][355];
int bellmanford()
{
qu.push(pi);
for(i=1;i<=n;i++)
dist[i]=2000000000;
memset(viz,0,sizeof(viz));
dist[pi]=0;
pred[pf]=0;
while(!qu.empty())
{
x=qu.front();
qu.pop();
viz[x]=0;
for(vector<int>::iterator it=a[x].begin();it!=a[x].end();it++)
{
if(dist[x]+cost[x][*it]<dist[*it]&&fl[x][*it])
{
dist[*it]=dist[x]+cost[x][*it];
pred[*it]=x;
if(!viz[*it])
{
qu.push(*it);
viz[*it]++;
}
}
}
}
return pred[pf];
}
int dijkstra()
{
return 0;
}
int main()
{
f>>n>>m>>pi>>pf;
for(i=1;i<=m;i++)
{
f>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
f>>fl[x][y];
f>>cost[x][y];
cost[y][x]=-cost[x][y];
}
bellmanford();
for(i=1;i<=n;i++)
{
for(vector<int>::iterator it=a[i].begin();it!=a[i].end();it++)
{
cst[i][*it]=cost[i][*it]+(dist[i]-dist[*it]);
}
}
while(bellmanford())
{
x=pf;
y=2000000000;
while(pred[x])
{
if(fl[pred[x]][x]<y)
y=fl[pred[x]][x];
x=pred[x];
}
x=pf;
while(pred[x])
{
fl[pred[x]][x]-=y;
fl[x][pred[x]]+=y;
csm+=(y*cost[pred[x]][x]);
x=pred[x];
}
}
g<<csm<<'\n';
f.close();
g.close();
return 0;
}