poj 1459 网络流EK算法/ Dinic算法

POJ1459解题报告 已经好几天没写解题报告了,今天年初一,呵呵,写一个,为今年开个好头。下面步入正题:POJ1459这道题目的要求是求最大的消耗量。我们添加一个源点s和一个汇点t,与s相连的是所有的np(生产点),边权值为其自己能产生的数值;与t相连的是所有的nc(消耗点),边权值为其消耗最大值。至此便完成了网络的构图。然后直接套用最大流的算法模版即可解决。我使用的是EK算法。什么是EK算法?EK算法的原理是 阅读详情

Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 24008 Accepted: 12534

Description

A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= p max(u) of power, may consume an amount 0 <= c(u) <= min(s(u),c max(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= l max(u,v) of power delivered by u to v. Let Con=Σ uc(u) be the power consumed in the net. The problem is to compute the maximum value of Con. 

An example is in figure 1. The label x/y of power station u shows that p(u)=x and p max(u)=y. The label x/y of consumer u shows that c(u)=x and c max(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and l max(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6. 

Input

There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of l max(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of p max(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of c max(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.

Output

For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.

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

Sample Output

15
6

Hint

The sample input contains two data sets. The first data set encodes a network with 2 nodes, power station 0 with pmax(0)=15 and consumer 1 with cmax(1)=20, and 2 power transport lines with lmax(0,1)=20 and lmax(1,0)=10. The maximum value of Con is 15. The second data set encodes the network from figure 1.

Source


第一道网络流题目,用的是EK算法。。。1600+ms,比较慢


#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

#define maxn 150
#define inf 0x3f3f3f3f

int first[maxn], top;
int next[maxn*maxn], cap[maxn*maxn],rev[maxn*maxn], to[maxn*maxn];


void add(int u, int v, int c)  //有向图
{
    to[top] = v, next[top] = first[u];
    cap[top] = c, rev[top] = top+1;
    first[u] = top++;

    to[top] = u, next[top] = first[v];
    cap[top] = 0, rev[top] = top-1;
    first[v] = top++;
}


int vis[maxn];  //遍历时检查每个点是否搜索过
int pre[maxn]; //pre[v],无权最短路中v的前一个点
int pre_edge[maxn]; //pre_edge[v],  无权最短路中连接v的边
int f[maxn]; //f[v],残余网络中到点v的最大流量

int bfs(int s, int t)  //bfs找增广路
{
    queue<int> que;
    que.push(s);
    pre[s] = -1;
    vis[s] = 1;
    f[s] = inf;

    while(!que.empty()){
        int u = que.front();
        que.pop();

        int v;
        for(int i = first[u]; i !=-1; i = next[i]){
            v = to[i];
            if(vis[v] || !cap[i]) continue;

            f[v] = min(f[u], cap[i]);
            que.push(v);
            vis[v] = 1;
            pre[v] = u, pre_edge[v] = i;
            if(v == t){
                while(v != s){
                    cap[pre_edge[v]] -= f[t];
                    cap[rev[pre_edge[v]]] += f[t];
                    v = pre[v];
                }

                return f[t];
            }
        }
    }

    return 0;
}

int ek_max_flow(int s, int t) //ek算法求最大流
{
    int ret = 0, f;
    while(1){
        memset(vis, 0, sizeof(vis));
        f = bfs(s, t); //查找增广路
        if(!f) break;
        ret += f;
    }

    return ret;
}

int value[5];

void convert(char *str)
{
    int len = strlen(str);
    int val = 0;
    int cnt = 0;
    for(int i = 1; i < len; i++){
        if(str[i] == ',' || str[i] == ')'){
            value[cnt++] = val;
            val = 0;
        }
        else
        val = val*10+str[i]-'0';
    }

    value[cnt] = val;
}

int main()
{
    int s, t;
    int n, m, np, nc;
    char str[100];
    int u,v,z;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m)){
        memset(first, -1, sizeof(first));
        top = 0;

        for(int i = 0; i < m; i++){
            scanf("%s", str);
            convert(str);
            add(value[0], value[1], value[2]);
        }

        for(int i = 0; i < np; i++){
            scanf("%s", str);
            convert(str);
            add(n+1, value[0], value[1]); //n+1为增加的超级源点
        }

        for(int i = 0; i < nc; i++){
            scanf("%s", str);
            convert(str);
            add(value[0], n+2, value[1]);   //n+2为增加的超级汇点
        }

        printf("%d\n", ek_max_flow(n+1, n+2));
    }
    return 0;
}

学完Dinic算法再来做一遍,只要63ms。。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

#define maxn 150
#define inf 0x3f3f3f3f

struct edge
{
    edge(int a, int b, int c){to=a; cap = b; rev = c;}
    int to, cap, rev;
};
vector<edge> g[maxn];


void add(int u, int v, int c)  //有向图
{
    g[u].push_back(edge(v, c, g[v].size()));
    g[v].push_back(edge(u, 0, g[u].size()-1));
}


int level[maxn];
void bfs(int s)
{
    memset(level, -1, sizeof(level));
    level[s] = 0;
    queue<int> que;
    que.push(s);

    while(!que.empty()){
        int u = que.front(); que.pop();
        for(int i = 0; i < g[u].size(); i++){
            int v = g[u][i].to, cap = g[u][i].cap;
            if(level[v] < 0 && cap > 0){
                level[v] = level[u]+1;
                que.push(v);
            }
        }
    }
}

int cur[maxn];
int dfs(int u, int t, int f)
{
    if(u == t) return f;

    for(int &i = cur[u]; i < g[u].size(); i++){
        int v = g[u][i].to, &cap = g[u][i].cap, rev = g[u][i].rev;
        if(level[v] == level[u]+1 && cap>0){
            int tmp = dfs(v, t, min(cap, f));
            if(tmp>0){
                cap -= tmp;
                g[v][rev].cap += tmp;
                return tmp;
            }
        }
    }

    return 0;
}

int Dinic(int s, int t)
{
    int flow = 0;
    while(1){
        bfs(s);
        if(level[t] < 0) return flow;
        int f;
        memset(cur, 0, sizeof(cur));
        while((f = dfs(s, t, 0x3f3f3f3f))) flow += f;
    }
}

int value[5];

void convert(char *str)
{
    int len = strlen(str);
    int val = 0;
    int cnt = 0;
    for(int i = 1; i < len; i++){
        if(str[i] == ',' || str[i] == ')'){
            value[cnt++] = val;
            val = 0;
        }
        else
        val = val*10+str[i]-'0';
    }

    value[cnt] = val;
}

int main()
{
    int s, t;
    int n, m, np, nc;
    char str[100];
    int u,v,z;
    while(~scanf("%d%d%d%d", &n, &np, &nc, &m)){
        for(int i = 0; i < maxn; i++) g[i].clear();
        for(int i = 0; i < m; i++){
            scanf("%s", str);
            convert(str);
            add(value[0], value[1], value[2]);
        }

        for(int i = 0; i < np; i++){
            scanf("%s", str);
            convert(str);
            add(n+1, value[0], value[1]); //n+1为增加的超级源点
        }

        for(int i = 0; i < nc; i++){
            scanf("%s", str);
            convert(str);
            add(value[0], n+2, value[1]);   //n+2为增加的超级汇点
        }

        printf("%d\n", Dinic(n+1, n+2));
    }
    return 0;
}



算法特训营第17周刷题题目 算法特训营本周内容: 1. 录播视频:最大网络流EK算法、Dinic算法及当前弧优化。 2. 直播刷题题目:HDU1532、HDU3549、POJ1149、POJ1459。 友情提示:以下是直播刷题链接(收费),不需要看直播请忽略。 【直播地址】https://www.epubit.com/courseDetails?id=PCCbf16b01a6788&recommenderCode=1540556 欢迎大家一起刷题。 ... 阅读详情

相关推荐

用Typora画流程图和序列图:Mermaid语法实战教程(含常见报错解决)

本文是一份详细的Typora与Mermaid语法实战教程,教你如何在Markdown文档中高效绘制流程图和序列图。文章涵盖从环境配置、核心语法到复杂图表构建的全过程,并重点解析了常见报错原因与解决方案,助你将可视化图表无缝融入文档工作流。

weixin_29279101的博客 299

网站default未启动_无法启动虚拟机,错误为“无网络,名称匹配为'default'”

网站default未启动I update libvirt version and want to start VM with the new libvirt tools but I failed as follows. 我更新了libvirt版本,并想使用新的libvirt工具启动VM,但是失败如下。 > sudo virsh start kvm1error: Failed to star...

cuma2369的博客 2352

棋牌App逆向工程实战:透视风险分析与反作弊加固思路

在移动应用安全领域,逆向工程是一项关键的攻防技术,它通过静态分析、动态调试与协议抓包等手段,深入还原客户端真实逻辑。这项技术的核心原理,是从已编译的安装包中提取数据流,验证服务端下发的信息是否超出了客户端应有的可见范围。从技术价值看,安全测试人员可以借此定位协议层、内存层与缓存层的潜在风险,为反作弊体系提供精准的加固建议。在棋牌类App等实时对局场景中,逆向工程常被用于排查牌局信息泄露类隐患,而合规授权与测试环境是开展此类研究的必要前提。本文结合一次棋牌App的安全评估实践,梳理了从环境准备到数据流还原的完

weixin_29326909的博客 174

KVM【virsh start centos7.0】启动时失败,报错:error: Network not found: no network with matching name ‘default‘

KVM虚拟机之【virsh start centos7.0】启动时失败,报错:error: Network not found: no network with matching name 'default'

qq_50685659的博客 3157

AGV舵轮选型实战:从核心参数到精准计算的完整指南

本文提供了一份详尽的AGV舵轮选型实战指南,强调从经验决策转向数据驱动。文章系统讲解了如何将工况需求转化为核心参数,并通过牵引力、扭矩、功率等关键计算步骤,指导工程师完成精准选型,避免因选型不当导致的运行故障与效率低下。

醉三国 495

基于Coze平台从零构建AI智能体:以测试用例生成为例的实战指南

在人工智能技术快速发展的今天,大语言模型(LLM)和提示词工程(Prompt Engineering)已成为构建智能应用的核心技术。其原理在于通过自然语言指令引导模型完成特定任务,其技术价值在于极大地降低了AI应用开发的门槛,使非专业开发者也能快速实现想法。这一技术广泛应用于自动化办公、智能客服、内容生成等场景。本文聚焦于字节跳动旗下的Coze平台,该平台通过可视化工作流编排和预置技能,将上述技术封装为易用的工具。文章以“根据产品需求文档(PRD)自动生成测试用例”这一具体场景为例,详细拆解了从需求分析、工

weixin_30617695的博客 337

Power Network

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 23147   Accepted: 12120 Description A power network consists of nodes (power stations, consumer

お晨晓微风☼倾半世阳光 465

解放双手!利用海康VM全局脚本+通讯管理打造自动化视觉控制系统

本文深入探讨了如何利用海康VisionMaster(VM)的全局脚本和通讯管理功能,打造高效自动化视觉控制系统。通过全局变量实现数据共享与动态调整,结合多协议通讯管理,提升系统响应速度与智能化水平,适用于工业4.0环境下的复杂视觉检测与决策场景。

weixin_28720573的博客 424

网络流的一些基础入门题

hdu 3549 入门 #include<bits/stdc++.h> #define FIN freopen("input.txt","r",stdin) #define ll long long #define mod 1000000007 #define lson l,m,rt<<1 #define rson m+1,r,rt<&...

diangan7073的博客 207

【立创EDA-PCB设计基础】6.布线铺铜实战及细节详解

本文进行布线铺铜实战及详解布线铺铜的细节

嵌入式阿齐Archie 4万+

YOLO26优化:特征融合 | 一种新颖的多尺度特征融合iAFF,适配小目标检测

在YOLO26中如何使用:iAFF加入Neck替代Concat;

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 270

Power Network poj1549

题目大意:有n个点,其中np个发电厂负责发电,每个电厂有自己的发电上限,nc个用户接受电,有自己的接受上限,有m根电线,有各自的流量上限,问所有用户得到的电力和的最大值是多少。思路:发电厂的发电上限和用户的接收上限可以看做容量,那么就可以创建一个超级源点,连接所有发电厂,创建一个超级汇点,连接所有用户,这样便成为了一个最大流问题,用dinic求解。

ashbringer233的博客 169

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

EK算法,Dinic算法,ISAP算法

一蓑烟雨 1249

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

网络流之最大流EK --- poj 1459

题目链接 本篇博客延续上篇博客(最大流Dinic算法)的内容,此次使用EK算法解决最大流问题。 EK算法思想:在图中搜索一条从源点到汇点的扩展路,需要记录这条路径,将这条路径的最大可行流量 liu 增加到结果ans中,然后反向从汇点到源点更新这条路径上的每条边的权值(减去此次的liu),同时反向边的权值也需要更新(加上此次的liu)。然后再搜索新的扩展路……,循环,直到找不到新的扩展路,...

weixin_30502965的博客 83

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

poj_1459 Power Network(多源多汇最大网络流)(EK / dinic / ISAP)

Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 27462   Accepted: 14275 Description A power network consists of nodes (power stations, consumer

凳子 1091

poj 1459 Power Network(最大流)

http://poj.org/problem?id=1459 题意略去,没什么坑。只是多源多汇,加上超级源点和汇点建图就行。 Dinic 235ms #include #include #include #include using namespace std; const int INF = 0x3f3f3f3f; int n,m,np,nc; int map[110]

1703

poj 1459 Power Network

A的第一道网络流,嘿嘿,相比较来说蛮合适的。题目不难,套模板,注意加超级源、超级汇。难就难在能把题看完,题目很纠结啊。。 题目大意就是说,输电网络中:p是发电站,c是用户。由Sample Input:输入边、p、c ,求最大流。 多源多汇!?加个源点连接所有p,所有c 连接汇点,又是单源单汇了。 ek 1 #include<stdio....

aizhengcuo9261的博客 140

POJ1459[Power Network]--最大流

poj1459

Greninja的博客 778

POJ训练计划1459_Power Network(网络流最大流/Dinic)

解题报告 这题建模实在是好建。,,好贱。。, 给前向星给跪了,纯dinic的前向星居然TLE,sad。,,回头看看优化,。。 矩阵跑过了。2A,sad,,, /************************************************************************* > File Name: PowerN.cpp &g...

aofan9566的博客 103

网络流入门-POJ1459PowerNetwork-最大流模板

(我有什么错误或者你有什么意见,欢迎留言或私聊!谢谢!) (Ps:以前听说过网络流,想着以后再学,这次中南多校赛也碰到有关网络流的题目,想着这两天试着学学这个吧~~ 这是本人网络流入门第二题,不知道怎么解释, 给大家推荐几个博客方便大家入门网络流: 网络流入门博客1 网络流入门博客2 看之前大家可以去百度一下网络流入门术语,这对新手入门网络流会有一些帮助) ...

ako2654的博客 163

POJ 1459-Power Network(网络流_最大流)

Power Network Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1459 Description A power network consists of nodes (power stat

Inite的博客 531

【网络流专题1】多源多汇

Ahead 11.1.2018 例题 Poj 1459 洛谷 3376 属于网络流的模板 EK算法 dinic算法 sap,isap 算法 hlpp算法 代码 (dinic) #include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include ...

weixin_30546933的博客 310

网络流之最大流入门-POJ1459PowerNetwork-Dinic模板

(我有什么错误或者你有什么意见,欢迎留言或私聊!谢谢!) (Ps:以前听说过网络流,想着以后再学,这次中南多校赛也碰到有关网络流的题目,想着这两天试着学学这个吧~~ 这是本人网络流入门第二题,不知道怎么解释,给大家推荐几个博客方便大家入门网络流: 网络流入门博客1 网络流入门博客2 看之前大家可以去百度一下网络流入门术语,这对新手入门网络流会有一些帮助) 题意: 用人话解释: 1...

打过ICPC/CCPC,学习ACM算法,Linux内核,Openharmony,加油努力开心珍惜 540
上一篇: hdu 4920 矩阵循环
下一篇: poj 2796 st算法+二分 / 单调栈
HuanTongH
博客等级 码龄12年 7粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值