POJ 1459(EK)

这题是学着小媛学姐写的..

 1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #include<queue>
5 #include <climits>
6 using namespace std;
7 #define N 120
8
9 int n, np, nc, m;
10 int cap[N][N];
11 int EK(int s, int t)
12 {
13 queue<int> q;
14 int flow[N][N];
15 int low[N];
16 int u,v,maxflow=0;
17 int pre[N];
18 memset(flow,0,sizeof(flow));
19 while(1)
20 {
21 q.push(s);
22 memset(low,0,sizeof(low));
23 low[s] = INT_MAX;
24 while(!q.empty())
25 {
26 u = q.front();
27 q.pop();
28 for(v = 0; v <= t; v ++)
29 {
30 if(!low[v] && cap[u][v] > flow[u][v])
31 {
32 q.push(v);
33 low[v] = min(low[u], cap[u][v] - flow[u][v]);
34 pre[v] = u;
35 }
36 }
37 }
38 if(low[t] == 0) break;
39 for(u = t; u != s; u = pre[u])
40 {
41 flow[pre[u]][u] += low[t];
42 flow[u][pre[u]] -= low[t];
43 }
44 maxflow += low[t];
45 }
46 return maxflow;
47 }
48 int main()
49 {
50 char ch;
51 int from, to, len, ans;
52 while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
53 {
54 memset(cap, 0, sizeof(cap));
55 while(m--)
56 {
57 scanf(" (%d,%d)%d", &from, &to, &len);
58 cap[from][to] = len;
59 }
60 while(np--)
61 {
62 scanf(" (%d)%d",&from, &len);
63 cap[n+1][from] = len;
64 }
65 while(nc--)
66 {
67 scanf(" (%d)%d",&from, &len);
68 cap[from][n+2] = len;
69 }
70 ans = EK(n+1, n+2);
71 printf("%d\n",ans);
72 }
73 return 0;
74 }

时间: 2024-08-04 02:12:19

POJ 1459(EK)的相关文章

POJ 1459 EK算法

题意: 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条:(0,1)20 (1,0)10 代表(起点,终点)最大传递的能量 (0)15 (产生能量的点)产生的最大能量(1)20 (消费能量的点)消费的最大能量 初学网络流,我想从基础练起:就先用EK算法写一遍 这道题看似很难,但其实只要加一个源点以及汇点,让所有的产生能量的点指向源点,让所有的消费能量的点指向汇点: #include

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

POJ 1459 &amp; ZOJ 1734 Power Network (网络最大流)

http://poj.org/problem?id=1459 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22674   Accepted: 11880 Description A power network consists of nodes (power s

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 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(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 多源多汇点最大流

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 (dinic)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23059   Accepted: 12072 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 最大流

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