POJ 1459 Power Network(最大流)

本文介绍了使用最大流算法解决实际问题的模板题解过程,包括构造源点和汇点,将所有核电站视为源点,所有用户视为汇点,通过跑一次最大流算法来求解最优解。

题目链接

算是一个模版题把。构造一个源点和汇点,所有的核电站都是从源点出发的,所有的用户都汇集到汇点,跑一次最大流就可以了。模版敲的不是很熟。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5 const int INF = 0xffffff;
 6 int flow[201][201],low[201],path[201];
 7 int str,end,n;
 8 int bfs()
 9 {
10     int t,i;
11     queue<int> que;
12     memset(path,-1,sizeof(path));
13     que.push(str);
14     low[str] = INF;
15     while(!que.empty())
16     {
17         t = que.front();
18         que.pop();
19         if(t == end) break;
20         for(i = 0; i <= n+1; i ++)
21         {
22             if(i != str&&path[i] == -1&&flow[t][i])
23             {
24                 path[i] = t;
25                 low[i] = low[t] < flow[t][i] ? low[t]:flow[t][i];
26                 que.push(i);
27             }
28         }
29     }
30     if(path[end] == -1)
31         return -1;
32     else
33         return low[end];
34 }
35 int EK()
36 {
37     int ans = 0,now,res;
38     while((res = bfs()) != -1)
39     {
40         ans += res;
41         now = end;
42         while(now != str)
43         {
44             flow[path[now]][now] -= res;
45             flow[now][path[now]] += res;
46             now = path[now];
47         }
48     }
49     return ans;
50 }
51 inline void clean()
52 {
53     char ch;
54     for(;;)
55     {
56         scanf("%c",&ch);
57         if(ch == '(') break;
58     }
59 }
60 int main()
61 {
62     int i,m,sv,ev,w,np,nc;
63     while(scanf("%d%d%d%d%*c",&n,&np,&nc,&m)!=EOF)
64     {
65         memset(flow,0,sizeof(flow));
66         for(i = 1; i <= m; i ++)
67         {
68             clean();
69             scanf("%d,%d)%d",&sv,&ev,&w);
70             flow[sv][ev] += w;
71         }
72         for(i = 1; i <= np; i ++)
73         {
74             clean();
75             scanf("%d)%d",&ev,&w);
76             flow[n][ev] += w;
77         }
78         for(i = 1; i <= nc; i ++)
79         {
80             clean();
81             scanf("%d)%d%*c",&sv,&w);
82             flow[sv][n+1] += w;
83         }
84         str = n;
85         end = n+1;
86         printf("%d\n",EK());
87     }
88     return 0;
89 }

 

转载于:https://www.cnblogs.com/naix-x/archive/2012/12/11/2813237.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值