poj 1459 多源汇最大流

分析在这里

窝把原来的那个方法也写上好了,反正也差不多....

其实....还写了最高标号预流推进....................................就是这么蛋疼....然而在这道题目上来说,,这三种方法其实都一样。。。

code:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
using namespace std;

#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))

typedef pair<int,int> pii;
typedef long long LL;
//------------------------------
const int maxn = 205;

int n,m;

struct Edge{
      int to, cap, rev;//终点 容量 反向边
      Edge(int to_ = 0, int cap_ = 0, int rev_ = 0){
            to = to_;
            cap = cap_;
            rev = rev_;
      }
};
vector<Edge> g[maxn];
int level[maxn];//顶点到源点的距离标号
int iter[maxn];

void add_Edge(int from, int to, int cap){
      Edge tmp1(to, cap, g[to].size());
      g[from].push_back(tmp1);
      Edge tmp2(from, 0, g[from].size()-1);
      g[to].push_back(tmp2);
}
void bfs(int s){
      memset(level, -1, sizeof(level));
      queue<int> q;
      level[s] = 0;
      q.push(s);
      while(!q.empty()){
            int v = q.front();
            q.pop();
            for(int i = 0; i < g[v].size(); i++){
                  Edge& e = g[v][i];
                  if(e.cap > 0 && level[e.to] < 0){
                  level[e.to] = level[v] + 1;
                  q.push(e.to);
                  }
            }
      }
}
int dfs(int v, int t, int f){
      if(v == t) return f;
      for(int& i = iter[v]; i < g[v].size(); i++){
            Edge& e = g[v][i];
            if(e.cap > 0 && level[v] < level[e.to]){
                  int d = dfs(e.to, t, min(e.cap, f));
                  if(d > 0){
                        e.cap -= d;
                        g[e.to][e.rev].cap += d;
                        return d;
                  }
            }
      }
      return 0;
}
int max_flow(int s, int t){
      int flow = 0;
      for(;;){
            bfs(s);
            if(level[t] < 0) return flow;
            memset(iter, 0, sizeof(iter));
            int f;
            while((f = dfs(s, t, INF)) > 0){
                  flow += f;
            }
      }
}
//------------------上面是最大流模板部分 正确----------------------
int np, nc;
void init(){
      for(int i = 0; i < maxn; i++) g[i].clear();
      int u, v, w;
      char ch;
      for(int i = 1; i <= m; i++){
            while(scanf("%c", &ch)){
                  if(ch == '(') break;
            }
            scanf("%d,%d%c%d",&u,&v,&ch,&w);
            add_Edge(u, v, w);
      }
      for(int i = 1; i <= np; i++){
            while(scanf("%c", &ch)){
                  if(ch == '(') break;
            }
            scanf("%d%c%d",&u, &ch, &w);
            add_Edge(n, u, w);
      }
      for(int i = 1; i <= nc; i++){
            while(scanf("%c", &ch)){
                  if(ch == '(') break;
            }
            scanf("%d%c%d",&v, &ch, &w);
            add_Edge(v, n+1, w);
      }
      int ans = max_flow(n, n+1);
      printf("%d\n",ans);
}
int main(){
      while(scanf("%d%d%d%d",&n,&np, &nc, &m) != EOF){
            init();
      }
      return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 04:38:03

poj 1459 多源汇最大流的相关文章

poj 1459 Power Network, 最大流,多源多汇

点击打开链接 多源多汇最大流,虚拟一个源点s'和一个汇点t',原来的源点.汇点向它们连边. #include<cstdiO> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> using namespace std; const int maxn = 500 + 5; const int INF = 100

POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

poj 1459 多源多汇点最大流

Sample Input 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7 (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5 (0)5 (1)2 (3)2 (4)1 (5)4 7个点包括电站和用户,2个电站,3个用户,13条边,输入13条边,输入2个电站,输入3个用户 Sample Output 15 6 增加一个源点一个汇点

POJ 1459 Power Network (网络流最大流基础 多源点多汇点 Edmonds_Karp算法)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24056   Accepted: 12564 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

BZOJ-1305: [CQOI2009]dance跳舞(最大流 增加超级源汇+分裂点+二分 水题)

1305: [CQOI2009]dance跳舞 Time Limit: 5 Sec  Memory Limit: 162 MB Description 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢”).每个男孩最多只愿意和k个不喜欢的女孩跳舞,而每个女孩也最多只愿意和k个不喜欢的男孩跳舞.给出每对男孩女孩是否相互喜欢的信息,舞会最多能有几首舞曲? Inpu

POJ 1459 Power Network(ISAP 裸最大流)

题目链接:http://poj.org/problem?id=1459 注意输入格式就行,还是ISAP #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 300; const int ma

POJ 1459 Power Network 最大流

建模不难,就读入有点麻烦,无脑拍完dinic 1A happy- #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

POJ 1459 &amp;amp;&amp;amp; ZOJ 1734--Power Network【最大流dinic】

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25108   Accepted: 13077 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied

POJ 1459 Power Network 经典网络流构图问题 最大流,EK算法

题目链接:POJ 1459 Power Network Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23347   Accepted: 12231 Description A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport line