Pagini recente » Cod sursa (job #924462) | Cod sursa (job #2769941) | Cod sursa (job #2478199) | Cod sursa (job #628243) | Cod sursa (job #1358421)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
#define MAXSIZE 355
#define FIN "fmcm.in"
#define FOUT "fmcm.out"
int n,m;
vector<int> graph[MAXSIZE];
int capacity[MAXSIZE][MAXSIZE];
int cost[MAXSIZE][MAXSIZE];
int maxFlow;
int minCost;
int father[MAXSIZE];
int source,destination;
int read()
{
freopen(FIN,"r",stdin);
freopen(FOUT,"w",stdout);
scanf("%d %d %d %d \n", &n,&m, &source, &destination);
int x,y,z,t;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d %d \n", &x,&y,&z,&t);
graph[x].push_back(y);
capacity[x][y] = z;
cost[x][y] = t;
}
return 0;
}
int BFS()
{
queue<int> q;
vector<int>::iterator it;
for(int i=1;i<=n;i++)
father[i] = 0;
q.push(source);
while(!q.empty())
{
int nod = q.front();
if(nod == destination)
{
nod = father[nod];
int minim = cost[nod][destination];
for(it = graph[nod].begin();it!=graph[nod].end();it++)
{
if(*it == destination && capacity[*it][destination] > 0)
{
if(cost[*it][destination] < minim)
{
minim = cost[*it][destination];
father[destination] = *it;
}
}
}
return 1;
}
else
{
q.pop();
for(it = graph[nod].begin();it!=graph[nod].end();it++)
{
if(father[*it] == 0 && capacity[nod][*it] > 0)
{
q.push(*it);
father[*it] = nod;
}
}
}
}
return 0;
}
int solve()
{
while(BFS() == 1)
{
//get min value
int minn = capacity[father[destination]][destination];
for(int i=destination;i!=source;i=father[i])
{
if(minn > capacity[father[i]][i])
minn = capacity[father[i]][i];
}
//printf("\n path ");
for(int i=destination;i!=source;i=father[i])
{
// printf("%d capacity %d ", i, minn);
minCost += minn * cost[father[i]][i];
capacity[father[i]][i] -= minn;
}
maxFlow += minn;
}
return 0;
}
int write()
{
printf("%d",minCost);
return 0;
}
int main()
{
read();
solve();
write();
return 0;
}