Pagini recente » Cod sursa (job #2683755) | Cod sursa (job #1873488) | Cod sursa (job #674398) | Cod sursa (job #2894388) | Cod sursa (job #702459)
Cod sursa(job #702459)
#include<iostream>
#include <queue>
#include <fstream>
#include<vector>
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
using namespace std;
int i,j,n,m,x,y,c,a[352][352],co[352][352],viz[352],s,d,tata[352],cost[352],flux;
vector<pair<int,int> > v[352];
int rez;
class Compare
{
public :
inline bool operator() ( const pair<int,int>& x, pair<int,int>& y ) const
{
return cost[x.first] > cost[y.first];
}
};
priority_queue<pair<int,int> ,vector<pair<int,int> > ,Compare> q;
pair<int,int> w;
bool dij()
{for(i=1;i<=n;i++)
{cost[i]=1<<25;
viz[i]=0;}
w.first=s;
w.second=0;
q.push(w);
cost[s]=0;
while(!q.empty())
{
w=q.top();
viz[w.first]=false;
q.pop();
tata[w.first]=w.second;
if(w.first!=d)
for(i=0;i<v[w.first].size();i++)
if(a[w.first][v[w.first][i].first]&&cost[v[w.first][i].first]>cost[w.first]+co[w.first][v[w.first][i].first])
{
cost[v[w.first][i].first]=cost[w.first]+co[w.first][v[w.first][i].first];
if(!viz[v[w.first][i].first])
{q.push(v[w.first][i]);
viz[v[w.first][i].first]=true;}
}
}
if(cost[d]!=(1<<25))
return true;
else
return false;
}
int main ()
{ifstream f("fmcm.in");
ofstream g("fmcm.out");
f>>n>>m>>s>>d;
while(m)
{
f>>x>>y;
f>>a[x][y];
f>>c;
co[x][y]=c;
co[y][x]=-c;
v[x].pb(mp(y,x));
v[y].pb(mp(x,y));
m--;
}
for(rez=0;dij();rez+=c)
{
x=d; flux=20000; c=0;
while(tata[x])
{
if(a[tata[x]][x]<flux) flux=a[tata[x]][x];
x=tata[x];
}
x=d;
while(tata[x])
{ a[tata[x]][x]-=flux;
a[x][tata[x]]+=flux;
c+=flux*co[tata[x]][x];
x=tata[x];
}
}
g<<rez;
f.close(); g.close();
return 0;
}