码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1273

时间:2017-08-19 18:46:14      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:efi   inf   int   func   algorithm   while   return   pat   turn   

题意:网络流的裸题,1为源点,n为汇点,给定每条边的容量,求最大流,用EK算法。

代码:

#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
#define N 300
#define INF 0x7fffffff
using namespace std;
int Map[N][N];
int path[N];
int n,m;

bool bfs(int s,int t){//寻找最短路径
     int p;
   queue<int> q;
   memset(path,-1,sizeof(path));
   path[s]=s;
   q.push(s);
   while(!q.empty()){
      p=q.front();
      q.pop();
      for(int i=1;i<=m;i++){
         if(Map[p][i]>0&&path[i]==-1){
             path[i]=p;
             if(i==t) return true;
             q.push(i);
          }
       }
   }
   return false;
}
int Ek(int s,int t){
   int d,flow=0;
   while(bfs(s,t)){//直到无法找到最短路径
      d=INF;
      for(int i=t;i!=s;i=path[i]){
         d=min(d,Map[path[i]][i]);
       }
      for(int i=t;i!=s;i=path[i]){
         Map[path[i]][i]-=d;//正向
         Map[i][path[i]]+=d;//反向
       }
       flow+=d;
    }
   return flow;
}
int main(){
      while(scanf("%d%d",&n,&m)!=EOF){
        memset(Map,0,sizeof(Map));
        for(int i=1;i<=n;i++){
      int from,to,flow;
      scanf("%d%d%d",&from,&to,&flow);
      Map[from][to]+=flow;
    }
    printf("%d\n",Ek(1,m));
  }
 return 0;
}

poj 1273

标签:efi   inf   int   func   algorithm   while   return   pat   turn   

原文地址:http://www.cnblogs.com/LMissher/p/7396970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!