当前位置: 代码迷 >> 综合 >> poj 1273 http://poj.org/problem?id=1273
  详细解决方案

poj 1273 http://poj.org/problem?id=1273

热度:82   发布时间:2024-01-13 20:12:21.0
题意:最基本的网络流,思路是增广路算法的一个简单实现;
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define dotNum 220
#define MAX 214748364
typedef __int64 big;
big cap[dotNum][dotNum];
big flow[dotNum][dotNum];
int path[dotNum];
int map[dotNum][dotNum];
int visit[dotNum];
void bfs(int begin,int end)
{queue<int> Q;memset(visit,0,sizeof(visit));memset(path,-1,sizeof(path));Q.push(begin);visit[begin]=1;while(!Q.empty()){int pre=Q.front();Q.pop();for(int i=1;i<=end;i++){if(!visit[i]&&cap[pre][i]!=0)//&&map[pre][i]!=0;{Q.push(i);visit[i]=1;path[i]=pre;}}}
}
big MaxFlow(int begin,int end)
{big maxx=0;memset(flow,0,sizeof(flow));bfs(begin,end);while(path[end]!=-1){int min=MAX;int loc=end;while(loc!=begin){if(min>cap[path[loc]][loc])min=cap[path[loc]][loc];loc=path[loc];}loc=end;while(loc!=begin){cap[path[loc]][loc]-=min;flow[path[loc]][loc]+=min;cap[loc][path[loc]]+=min;flow[loc][path[loc]]-=min;loc=path[loc];}bfs(begin,end);}for(int i=1;i<=end;i++)if(map[begin][i]!=0)maxx+=flow[begin][i];return maxx;
}
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){memset(cap,0,sizeof(cap));memset(map,0,sizeof(map));int a,b;__int64 c;for(int i=0;i<n;i++){scanf("%d%d%I64d",&a,&b,&c);cap[a][b]+=c;map[a][b]=1;}printf("%I64d\n",MaxFlow(1,m));}return 0;
}

  相关解决方案