Power Network(最大流(EK算法))
2024-08-28 20:59:13
http://poj.org/problem?id=1459
题意:有一个电路网络,每个节点可以产生、传递、消耗若干电量,有点线连接结点,每个电线有最大传输量,求这个网络的最大消费量。
思路:从源点到发电站连边,流量为发电量,从用户到汇点连边,流量为消费量,再根据电线连双向边,求最大流即可。
#include<stdio.h>
#include<string.h>
#include<queue>
const int N=;
const int INF=<<;
using namespace std;
int map[N][N];
int pre[N];
int n,np,nc,m; int bfs(int s,int d)
{
queue<int>q;
memset(pre,-,sizeof(pre));
pre[s] = ;
int k;
q.push(s);
while(!q.empty())
{
k = q.front();
q.pop();
for (int i = ; i <= n+; i ++)
{
if (pre[i]==- && map[k][i] > )
{
pre[i] = k;
if (i==d)
return ;
q.push(i);
}
}
}
return ;
}
int maxflow(int s,int d)
{
int maxf = ;
while(bfs(s,d))
{
int minf = INF;
for (int i = d; i!=s; i = pre[i])
//minf = minf < map[pre[i]][i] ? minf:map[pre[i]][i];
minf = min(minf,map[pre[i]][i]);
for (int i = d; i!=s; i = pre[i])
{
map[pre[i]][i] -= minf;
map[i][pre[i]] += minf;
}
maxf += minf;
}
return maxf;
}
int main()
{
int s,d;
int u,v,w;
char str[];
while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(map,,sizeof(map));
s = n;
d = n+;
for (int i = ; i < m; i ++)
{
scanf("%s",str);
sscanf(str,"(%d,%d)%d",&u,&v,&w);
map[u][v] = w; }
for (int i = ; i < np; i ++)
{
scanf("%s",str);
sscanf(str,"(%d)%d",&v,&w);
map[s][v] = w;
}
for (int i = ; i < nc; i ++)
{
scanf("%s",str);
sscanf(str,"(%d)%d",&u,&w);
map[u][d] = w;
}
printf("%d\n",maxflow(s,d));
}
return ;
}
最新文章
- 去除inline-block元素间间距
- 字符串、数组方法实战--charAt(),split(),indexOf(),substring()
- LeetCode之371. Sum of Two Integers
- iOS开发——网络实用技术OC篇&;网络爬虫-使用青花瓷抓取网络数据
- JSONP理解和使用
- Android多线程----异步消息处理机制之Handler详解
- Java网络编程(UDP协议-聊天程序)
- Net中的AOP
- fork();
- JavaScript-打开新窗口
- Jsp中使用数据库连接池.
- 通常编译亲测56Y国际象棋源代码,精仿56Y国际象棋完整的源代码下载!
- POI-处理大Excel文件(xls)
- ubuntu16.04 英文环境安装google chrome
- jstl常用语句
- nyoj 数的长度
- 人工智能算法综述(二) RNN and LSTM
- 一个spinner控件使用的实例
- 【搬运】 Page Object 官方文档 (新增了Widget特性)
- day11-(cookie&;&;session)