Pagini recente » Cod sursa (job #249128) | Cod sursa (job #1668654) | Cod sursa (job #128146) | Cod sursa (job #2512141) | Cod sursa (job #333939)
Cod sursa(job #333939)
#include <fstream>
#include <queue>
#define INF INT_MAX
using namespace std;
struct node {
int vertex,priority,from;
};
bool operator<(const node &St,const node &Dr) {
return (St.priority>Dr.priority);
}
node nodez (int a,int b, int c) {
node my;
my.vertex=a;
my.priority=b;
my.from=c;
return (my);
}
node mynod;
priority_queue<node> q;
int N,M,from[1010],flow[1010][1010],visit[1010],capacity[1010][1010],a,b,i,c;
vector<int> vecin[1010];
vector<int>::iterator it;
int PFS(){
int m=0,where,cost,new_cost,i;
q.push(nodez(1,INF,-1));
while (!q.empty()) {
mynod=q.top();
q.pop();
where=mynod.vertex; cost=mynod.priority;
if (visit[where]) continue;
from[where]=mynod.from;
if (where==N)
{
m=cost;
break;
}
visit[where]=1;
for (it=vecin[where].begin();it!=vecin[where].end();it++) {
if (capacity[where][*it]>0) {
new_cost=min(cost,capacity[where][*it]);
q.push(nodez(*it,new_cost,where));
}
}
}
where=N;
int prev;
while (from[where]>-1) {
prev=from[where];
capacity[prev][where]-=m;
capacity[where][prev]+=m;
where=prev;
}
return m;
}
int main () {
ifstream in;
ofstream out;
in.open("maxflow.in");
out.open("maxflow.out");
in >> N >> M;
for (i=0;i<M;i++) {
in >> a >> b >> c;
vecin[a].push_back(b);
vecin[b].push_back(a);
capacity[a][b]=c;
}
int sum=0,m=1;
while(m) {
m=PFS();
sum+=m;
for (i=1;i<=N;i++) visit[i]=0;
while(!q.empty()) q.pop();
}
out << sum;
out.close();
return 0;
}