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

2023-05-16

解题报告

这题建模实在是好建。,,好贱。。,

给前向星给跪了,纯dinic的前向星居然TLE,sad。,,回头看看优化,。。

矩阵跑过了。2A,sad,,,

/*************************************************************************
	> File Name:	PowerN.cpp
	> Author:		_nplus
	> Mail:	    jun18753370216@gmail.com
	> Time:		2014年07月19日 星期六 09时30分23秒
 ************************************************************************/

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
#define inf 99999999
#define N 510
#define M N*N
using namespace std;
int edge[N][N],l[N],n,m,nc,np;
int bfs()
{
    queue<int >Q;
    memset(l,-1,sizeof(l));
    while(!Q.empty())
        Q.pop();
    l[n]=0;
    Q.push(n);
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        for(int i=0; i<=n+1; i++)
        {
            if(edge[u][i]&&l[i]==-1)
            {
                l[i]=l[u]+1;
                Q.push(i);
            }
        }
    }
    if(l[n+1]>0)return 1;
    else return 0;
}
int dfs(int x,int f)
{
    if(x==n+1)return f;
    int a;
    for(int i=0; i<=n+1; i++)
    {
        if(edge[x][i]&&(l[i]==l[x]+1)&&(a=dfs(i,min(edge[x][i],f))))
        {
            edge[x][i]-=a;
            edge[i][x]+=a;
            return a;
        }
    }l[x]=-1;//加上时间优化了15倍,,。sad,。,
    return 0;
}
int main()
{
    int i,j,u,v,w;
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
    {
        memset(edge,0,sizeof(edge));
        for(i=0; i<m; i++)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&w);
            edge[u][v]=w;
        }
        for(i=0; i<np; i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&v,&w);
            edge[n][v]=w;
        }
        for(i=0; i<nc; i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&w);
            edge[u][n+1]=w;
        }
        int a,flow=0;
        while(bfs())
        {
            while(a=dfs(n,inf))
            {
                flow+=a;
            }
        }
        printf("%d\n",flow);
    }
}

写写EK算法。。。居然比我写的Dinic快,。。看来我的模板问题不少,,。sad。。,
/*************************************************************************
	> File Name:	PowerN.cpp
	> Author:		_nplus
	> Mail:	    jun18753370216@gmail.com
	> Time:		2014年07月19日 星期六 09时30分23秒
 ************************************************************************/

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
#define inf 99999999
#define N 510
#define M N*N
using namespace std;
int edge[N][N],pre[N],a[N],n,m,nc,np,flow;
void ek()
{
    while(1)
    {
        queue<int >Q;
        Q.push(n);
        memset(pre,-1,sizeof(pre));
        memset(a,0,sizeof(a));
        a[n]=inf;
        pre[n]=n;
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            for(int v=0;v<=n+1;v++)
            {
                if(edge[u][v]&&!a[v])
                {
                    pre[v]=u;
                    a[v]=min(a[u],edge[u][v]);
                    Q.push(v);
                }
            }
            if(a[n+1])break;
        }
        if(!a[n+1])break;
        for(int u=n+1;u!=n;u=pre[u])
        {
            edge[pre[u]][u]-=a[n+1];
            edge[u][pre[u]]+=a[n+1];
        }
        flow+=a[n+1];
    }
}
int main()
{
    int i,j,u,v,w;
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
    {
        memset(edge,0,sizeof(edge));
        for(i=0; i<m; i++)
        {
            while(getchar()!='(');
            scanf("%d,%d)%d",&u,&v,&w);
            edge[u][v]=w;
        }
        for(i=0; i<np; i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&v,&w);
            edge[n][v]=w;
        }
        for(i=0; i<nc; i++)
        {
            while(getchar()!='(');
            scanf("%d)%d",&u,&w);
            edge[u][n+1]=w;
        }
        int a;
        flow=0;
        ek();
        printf("%d\n",flow);
    }
}


Power Network
Time Limit: 2000MS Memory Limit: 32768K
Total Submissions: 22571 Accepted: 11819

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



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ训练计划1459_Power Network(网络流最大流/Dinic) 的相关文章

  • ca-bundle.crt文件,用于php发起外部https请求

    2019独角兽企业重金招聘Python工程师标准 gt gt gt Bundle of CA Root Certificates Certificate data from Mozilla downloaded on Wed Aug 13
  • 执行truffle unbox react报错,出现Error: connect ECONNREFUSED 0.0.0.0:443问题的解决办法

    前提 xff1a 我是用的是MAC系统 xff0c 不知道使用windows系统是否也可以 react box 项目构建 localhost ReactDapp liyuechun truffle unbox react box Start
  • 如何合并PDF文件?教你几种超简单的方法

    如何合并PDF文件呢 xff1f 我们在工作中会遇到很多难以处理的文件 xff0c 比如PDF文件就是一种 xff0c 尤其是将多个PDF文件合并成一个PDF文件 xff0c xff0c 其实大多数人都不知道将其合并 xff0c 盲目的在网
  • not a valid identifier解决

    not a valid identifier不是有效的标识符 因为在 usr的 多加了一个空格 xff0c 导致JAVA Home 无法识别 转载于 https www cnblogs com wxd136 p 10332040 html
  • asp链接数据库[转]

    1 ASP连接Access数据库语句 Set Conn 61 Server CreateObject 34 ADODB Connection 34 Connstr 61 34 DBQ 61 34 43 server mappath 34 w
  • OpenGL纹理映射

    GLfloat xrot X 旋转量 GLfloat yrot Y 旋转量 GLfloat zrot Z 旋转量 GLuint texture 1 存储一个纹理 AUX RGBImageRec LoadBMP char Filename 载
  • 【转】设置Qt应用程序图标及应用程序名

    一直以来很纠结给qt应用程序添加图标问题 xff0c 在网上收过一次 xff0c 但是感觉不够完整 xff0c 现将自己的实现过程记录下 xff0c 以便以后查看 xff1a 通过网上的例子知道qt助手中有相关说明 xff1a Settin
  • studioone机架效果模板_贾爽的分享-贾爽:带你认识StudioOne机架自带的两个混响效果器!...

    作者姓名 xff1a 贾爽 xff0c 现居河南省南阳市 xff0c 音视频软硬件产品的意见领袖 xff0c 网络主播培训指导讲师 xff0c 爽哥KX驱动工具 制作者 xff0c 南阳标题网络技术有限公司创始人 xff0c 河南省流行音乐
  • HTTP认证用户名密码 php

    header 39 HTTP 1 1 401 Authorization Required 39 header 39 WWW Authenticate Basic realm 61 34 PHP Secured 34 39 用户名和口令列表
  • C++ 存储类

    C 43 43 存储类 存储类定义 C 43 43 程序中变量 函数的范围 xff08 可见性 xff09 和生命周期 这些说明符放置在它们所修饰的类型之前 下面列出 C 43 43 程序中可用的存储类 xff1a auto registe
  • 怎么在VS监视DataSet类型的数据

    旧版本 先监视DataSet xff0c 打开dataset dataset下面有一个tables Tables打开有一个非公共成员 xff0c 然后下面有一个List xff0c List中存储了每一张表的信息 下图所示的List下面的
  • Python网络爬虫5 - 爬取QQ空间相册

    自毕业后 xff0c 就再也没有用过QQ xff0c QQ空间里记录的是些并不精彩的青葱岁月 xff0c 但好歹也是份回忆 xff0c 近日想着学以致用 xff0c 用Python把QQ空间相册的所有照片爬取下来 xff0c 以作备份 分析
  • C++学习笔记 简单部分

    C 43 43 数据类型 使用变量来存储各种信息 xff0c 变量保留的是它所存储的值的内存位置 这意味着 xff0c 当创建一个变量时 xff0c 就会在内存中保留一些空间 这段内存空间可以用于存储各种数据类型 xff08 比如字符型 宽
  • springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)

    最近在项目中想试一下使用 Hikari 连接池 xff0c 以前用的是阿里的 Druid xff0c 框架是 Spring MVC xff0c xml配置文件方式注入的 Bean xff0c 现在换成 Spring Boot 之后 xff0
  • 完美解决 开机无法启动 提示0xc000000e 问题

    完美解决 开机无法启动 提示0xc000000e 问题 原文链接 xff1a http bbs ruanmei com thread 186874 1 1 html 摘要 xff1a 本文提供0xc000000e问题的解决方法和原理解释 x
  • 使用Jmeter输出错误响应结果到日志

    性能测试过程中 xff0c 我们经常需要知道高并发性能测试情况下 xff0c 系统报错 xff0c 返回的结果是什么 xff0c 帮助开发具体定位问题 一 操作步骤 xff1a 正确响应结果 我们可以自定义断言语句 xff0c 自动判断断言
  • 控制台报错 index:0,size:0

    源代码 xff1a service实现类 xff1a String select sql 61 34 select cguid case isrz when 1 then 39 PASS 39 when 0 then 39 FAIL 39
  • Ubuntu11.04上tftp服务的配置

    Ubuntu11 04 上tftp 服务的配置 2011 06 17 15 01 以前ubuntu 版本上的tftp 已经配置很多遍了 xff0c 详情可以参见 xff1a www mcuos com thread 646 1 2 html
  • 九套常规报表模型(转)

    九套常规报表模型 我们可以通过九大报表模型的组合 xff0c 快速完成大多数报表的设计 这九大模型分别为 xff1a 列表 分组 主从 嵌套 交叉 图形 套打 分栏 填报 本文将重点对这九大模型的特征及适用范围进行阐述 1 列表 列表是报表
  • 利用for循环打印实心棱形和空心棱形

    一 要求 xff1a 提示用户输入棱形的行数 xff0c 比如输入5时 xff0c 打印如下实心棱形和空心棱形 xff08 由于排版问题 xff0c 可能显示会有变形 xff09 xff1a 二 分析 xff1a A 图形是上下对称的 B

随机推荐

  • Faiss教程:GPU

    Fassi通过CUDA支持GPU xff0c 要求3 5以上算力 xff0c float16要求CUDA7 5 43 通过index gpu to cpu可以将索引从GPU复制到CPU xff0c index cpu to gpu 和 in
  • 浏览器的强缓存与弱缓存

    在浏览器众多缓存中的HTTP缓存可能很多人对这个的概念并没有很清晰 xff0c 每个人都知道进入一次网页之后再刷新一次页面 xff0c 加载速度会比首次加载快非常多 xff0c 每个人都知道这是浏览器缓存的magic xff0c 但是对此背
  • 密钥协商(密钥交换)机制的讲解

    国标文件涉及密钥协商算法的函数 生成密钥协商参数并输出计算会话密钥产生协商数据并且计算会话密钥 密钥协商 xff08 交换 xff09 算法及其原理 密钥交换 协商目的 密钥协商机制 是 xff1a xff08 在身份认证 的前提下 xff
  • 基于牛客网Js v8引擎提供的读/写方法做的调试页面

    项目地址 正直秋招季 xff0c 对找工作的人来说 xff0c 牛客网肯定不陌生 xff0c 现在很多大型互联网公司的在线笔试都是在牛客网上面进行的 xff08 好像有打广告的嫌疑 xff09 Js有那么多的操作数据结构的api xff0c
  • 本地调试spark报org.apache.hadoop.io.nativeio.NativeIO$Windows.createFile...

    本地调试spark xff0c saveAsText 报错 org apache hadoop io nativeio NativeIO Windows createFileWithMode0 Ljava lang String xff1b
  • Element表格分页数据选择+全选所有完善批量操作

    后台管理系统中的列表页面 xff0c 一般都会有对列表数据进行批量操作的功能 xff0c 例如 xff1a 批量删除 批量删除等 之前项目中只是简单的用到Element框架中常规的属性 事件 在一次机缘巧合下 xff0c 了解到一个公司内部
  • idea本地调试web报“There is no configured/running web-servers found! Please, run any web-config”...

    为什么80 的码农都做不了架构师 xff1f gt gt gt 本文永久更新地址 xff1a https my oschina net bysu blog 3051091 1 按照网上的各种配置 如下图 还是不行 2 网上还说要先run才行
  • Visual Studio Code语言设置为中文

    1 Visual Studio Code下载安装 https code visualstudio com 2 语言设置 2 1 快捷键 Windows Linux 快捷键是 xff1a ctrl 43 shift 43 p macOS 快捷
  • 查看zookeeper注册中心是否有注册服务

    为什么80 的码农都做不了架构师 xff1f gt gt gt 查看zookeeper注册中心是否有注册服务可以在服务器上看 xff0c 也可以在dubboadmin看哦 1 在服务器上看 xff1a 1 xff09 查找zookeeper
  • matplotlib 设置图形大小时 figsize 与 dpi 的关系

    matplotlib 中设置图形大小的语句如下 xff1a fig 61 plt figure figsize 61 a b dpi 61 dpi 其中 xff1a figsize 设置图形的大小 xff0c a 为图形的宽 xff0c b
  • iOS- SQLite3的基本使用

    iOS 简单说说iOS移动客户端SQLite3的基本使用 1 为什么要使用SQLite3 xff1f 大量数据需要存储 管理数据 xff0c 存储数据 SQLite是一种关系型数据库 xff08 也是目前移动客户端的主流数据库 xff09
  • Daily Scrum: 2012/12/8

    成员角色今天工作明天计划王安然PM Dev已请假 xff0c 开会 继续开会 黄杨PM Dev已收拾skynet的小问题并且通过测试 xff08 312 xff09 xff0c 编写武器项cracker xff08 313 xff09 完成
  • 【Python】控制鼠标点击

    from pymouse import PyMouse m 61 PyMouse a 61 m position 获取当前坐标的位置 print a m move 50 500 鼠标移动到 x y 位置 a 61 m position pr
  • C++ 标准程序库std::string 详解

    现在一般不再使用传统的char 而选用C 43 43 标准程序库中的string类 xff0c 是因为string标准程序和char 比较起来 xff0c 不必担心内存是否足够 字符串长度 等等 xff0c 而且作为一个类出现 xff0c
  • Lighttpd 搭建 Web 服务器

    背景 xff1a 公司项目用到了lighttpd xff0c 由于自己没有接触过 xff0c 所以做下记录 简介 xff1a Lighttpd 是一个德国人领导的开源Web服务器软件 xff0c 其根本的目的是提供一个专门针对高性能网站 x
  • lua中.和:的区别

    2019独角兽企业重金招聘Python工程师标准 gt gt gt lua中 和 都可以用于方法的声明和调用 和table配合使用 和 最大的不同点 xff0c 就是 xff1a 会把调用者自身 xff0c 传入到函数中 如下代码 xff1
  • 一个很有意思的玩意:FlightGear,开源飞机模拟器

    你一定很想知道开F22战机是什么感觉 xff0c 甚至梦想有一天自己也能驾驭着飞机在空中飞翔 现实生活中 xff0c 做飞行员可不是一件简单的事 xff0c 既然如此 xff0c 我们就别想那么多 xff0c 但有了FlightGear这个
  • 第二学期无人机操作师结业复习测试

    无人机操作师结业复习测试 姓名 xff1a 学号 xff1a 得分 xff1a xff08 本套试卷考试时间为90分钟 xff0c 共分选择题 判断题 填空题 问答题四大部分 xff0c 总分100分 xff09 一 选择题 xff08 共
  • 误删linux文件恢复

    Linux下文件误删除 xff0c 使用extundelete恢复测试过程 extundelete下载官网地址 xff1a https pkgs org download extundelete 给虚拟主机添加一块磁盘 xff0c 磁盘为
  • POJ训练计划1459_Power Network(网络流最大流/Dinic)

    解题报告 这题建模实在是好建 xff0c xff0c 好贱 xff0c 给前向星给跪了 xff0c 纯dinic的前向星居然TLE xff0c sad xff0c xff0c 回头看看优化 xff0c 矩阵跑过了 2A xff0c sad