Pagini recente » Cod sursa (job #2178704) | Cod sursa (job #2861379) | Cod sursa (job #2487013) | Cod sursa (job #3138548) | Cod sursa (job #1226736)
#include<cstdlib>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream fi("maxflow.in");
ofstream fo("maxflow.out");
const int max_n = 1003;
vector <int> a[max_n];
queue <int> q;
int Capacity[max_n][max_n],Flow[max_n][max_n],Max_Flow;
int i,n,m,x,y,c,start_node,end_node,P[max_n];
bool viz[max_n];
bool augmenting_path(const int &start_node, const int &end_node){
for(int i=1;i<=n;i++) viz[i]=0;
q.push(start_node);
viz[start_node]=1;
P[start_node]=start_node;
while(q.size())
{
int x=q.front();
q.pop();
if(x==end_node) continue;
for(int j=0;j<(int)a[x].size();j++)
{
int y=a[x][j];
if(!viz[y] && Capacity[x][y]>Flow[x][y]){
q.push(y);
viz[y]=1;
P[y]=x;
}
}
}
return viz[end_node];
}
int find_path_capacity_and_update(const int &start_node, int yyy, const int &end_node){
int y,path_capacity=Capacity[yyy][end_node]-Flow[yyy][end_node];
y=yyy;
while(y!=P[y]){
if(path_capacity>Capacity[P[y]][y]-Flow[P[y]][y])
path_capacity=Capacity[P[y]][y]-Flow[P[y]][y];
y=P[y];
}
y=yyy;
Flow[y][end_node]+=path_capacity;
Flow[end_node][y]-=path_capacity;
while(y!=P[y]){
Flow[P[y]][y]+=path_capacity;
Flow[y][P[y]]-=path_capacity;
y=P[y];
}
return path_capacity;
}
int main(){
fi>>n>>m;
for(i=1;i<=m;i++)
{
fi>>x>>y>>c;
a[x].push_back(y);
a[y].push_back(x);
Capacity[x][y]=c;
}
start_node=1;
end_node=n;
Max_Flow=0;
for(;augmenting_path(start_node,end_node);)
for(int j=0;j<(int)a[end_node].size();j++)
{
y=a[end_node][j];
if(viz[y] && Capacity[y][end_node]>Flow[y][end_node])
{
int path_capacity=find_path_capacity_and_update(start_node,y,end_node);
Max_Flow+=path_capacity;
}
}
fo<<Max_Flow;
fi.close();
fo.close();
return 0;
}