n个村庄m条带权路,权值为花费,村庄可以造东西卖东西,造完东西可以换地方卖,给出每个村庄造东西花费a和最多个数b、卖东西价值c和最多个数d,求最大收益。

裸的费用流。然而还WA了一发。很好。

建源向每个村庄连边(b,a),(b,a)表示容量b费用a,每个村庄向汇点连边(d,-c),村庄间有路就互相连边(inf,v),v为边权,然后就是最小费用流。

不是最小费用最大流!!把费用最大流SPFA中最后一句判断改成<0即可,因为>=0时的费用可以不要他。

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std; int n,m,s,t;
#define maxn 10011
#define maxm 50011
struct Edge{int from,to,next,cap,flow,cost;};
const int inf=0x3f3f3f3f;
struct Graph
{
Edge edge[maxm];
int n,first[maxn],d[maxn],cur[maxn],le,cost,s,t;
bool inq[maxn],mark[maxn];
void clear() {memset(first,,sizeof(first));le=;cost=;}
void in(int x,int y,int cap,int cost)
{
Edge& e=edge[le];
e.from=x;e.to=y;e.cap=cap;e.flow=;
e.cost=cost;e.next=first[x];first[x]=le++;
}
void insert(int x,int y,int cap,int cost)
{
in(x,y,cap,cost);
in(y,x,,-cost);
}
bool SPFA()
{
memset(d,0x3f,*(n+));
int que[],head=,tail=;
que[]=s;inq[s]=;d[s]=;
while (head<tail)
{
const int now=que[head++];
if (head==) head=;
inq[now]=;
for (int i=first[now];i;i=edge[i].next)
{
Edge& e=edge[i];
if (e.cap>e.flow && d[e.to]>d[now]+e.cost)
{
d[e.to]=d[now]+e.cost;
if (!inq[e.to])
{
que[tail++]=e.to;
inq[e.to]=;
if (tail==) tail=;
}
}
}
}
return d[t]<;
}
int dfs(int x,int a)
{
if (x==t || !a) {cost+=a*d[t];return a;}
mark[x]=;
int flow=,f;
for (int& i=cur[x];i;i=edge[i].next)
{
Edge& e=edge[i];
if (!mark[e.to] && d[e.to]==d[x]+e.cost && (f=dfs(e.to,min(a,e.cap-e.flow)))>)
{
flow+=f;
e.flow+=f;
edge[i^].flow-=f;
a-=f;
if (!a) break;
}
}
// mark[x]=0;
return flow;
}
int MCMF(int s,int t)
{
this->s=s;this->t=t;
int flow=;cost=;
memset(mark,,sizeof(mark));
memset(inq,,sizeof(inq));
while (SPFA())
{
memset(mark,,n+);
for (int i=;i<=n;i++) cur[i]=first[i];
flow+=dfs(s,inf);
}
return cost;
}
}g;
int a,b,c,d;
int main()
{
while (scanf("%d%d",&n,&m)==)
{
g.clear();g.n=n+;s=n+;t=s+;
for (int i=;i<=n;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
g.insert(s,i,b,a);
g.insert(i,t,d,-c);
}
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if (a==b) continue;
g.insert(a,b,inf,c);
g.insert(b,a,inf,c);
}
printf("%d\n",-g.MCMF(s,t));
}
return ;
}

2017"百度之星"程序设计大赛 - 初赛(B)度度熊的交易计划的更多相关文章

  1. 2017"百度之星"程序设计大赛 - 资格赛 1002 度度熊的王国战略

    全局最小割 Stoer-Wagner (SW算法)优化 优化吃藕了,感谢放宽时限,感谢平板电视 (pb_ds) #include <iostream> #include <cstdi ...

  2. HDU 6118 度度熊的交易计划 【最小费用最大流】 (2017"百度之星"程序设计大赛 - 初赛(B))

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 6119 小小粉丝度度熊 【预处理+尺取法】(2017"百度之星"程序设计大赛 - 初赛(B))

    小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. HDU 6114 Chess 【组合数】(2017"百度之星"程序设计大赛 - 初赛(B))

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. HDU 6109 数据分割 【并查集+set】 (2017"百度之星"程序设计大赛 - 初赛(A))

    数据分割 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. HDU 6108 小C的倍数问题 【数学】 (2017"百度之星"程序设计大赛 - 初赛(A))

    小C的倍数问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. HDU 6122 今夕何夕 【数学公式】 (2017"百度之星"程序设计大赛 - 初赛(A))

    今夕何夕 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. 2017"百度之星"程序设计大赛 - 初赛(A) [ hdu 6108 小C的倍数问题 ] [ hdu 6109 数据分割 ] [ hdu 6110 路径交 ] [ hdu 6112 今夕何夕 ] [ hdu 6113 度度熊的01世界 ]

    这套题体验极差. PROBLEM 1001 - 小C的倍数问题 题 OvO http://acm.hdu.edu.cn/showproblem.php?pid=6108 (2017"百度之星 ...

  10. [SinGuLaRiTy] 2017 百度之星程序设计大赛 初赛B

    [SinGuLaRiTy-1037] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Chess Time Limit: 2000/1000 ...

随机推荐

  1. nodejs中相互引用(循环引用)的模块分析

    话不多少,直接上源码吧: modA.js: module.exports.test = 'A'; const modB = require('./05_modB'); console.log( 'mo ...

  2. 01认识Python和基础知识

     1.了解Python Python的发展历史,作者Guido, 荷兰人 Python的优缺点 Python在网站的开发,如YouTube,科学计算,数据分析,在游戏后台开发等方面广泛使用  2.编写 ...

  3. Performance engineering introduction

    1.Performance Software performance is one of software quality attributes,  it  equal to efficiency w ...

  4. COGS 2274. [HEOI 2016] tree

    ★☆   输入文件:heoi2016_tree.in   输出文件:heoi2016_tree.out   简单对比时间限制:1 s   内存限制:128 MB 这道题数据弱到炸了 . 第一次做用树刨 ...

  5. 螺旋数字的python实现

    螺旋数字的算法简单实现. 示例 5 01 02 03 04 05 16 17 18 19 06 15 24 25 20 07 14 23 22 21 08 13 12 11 10 09 通过观察,外部 ...

  6. swift中的as?和as!

    as操作符用来把某个实例转型为另外的类型,由于实例转型可能失败,因此Swift为as操作符提供了两种形式:选项形式as?和强制形式as 选项形式(as?)的操作执行转换并返回期望类型的一个选项值,如果 ...

  7. 记录xerces使用(VS2017 C++)

    1.编译xerces,获得dll文件和lib文件 2.将dll文件和lib文件拷贝到使用xerces的工程目录里面去 3.配置VS2017 C/C++  ->  All Options --&g ...

  8. Hibernate-04

    HQL查询语法 查询:public class Dome{ Session session = HibernaeUitls.openSession(); Transaction tx = sessio ...

  9. Spring中RestTemplate的使用方法

    一.REST 在互联网中,我们会通过请求url来对网络上的资源做增删改查等动作,这里的请求包含两部分:动词,主要包括增.删.改.查:名词,就是网络中的各种资源.传统的非REST风格的请求方式是把动词和 ...

  10. 性能优化 java 24 次阅读 · 读完需要 15 分钟 0

    摘要: 技术传播的价值,不仅仅体现在通过商业化产品和开源项目来缩短我们构建应用的路径,加速业务的上线速率,也会体现在优秀程序员在工作效率提升.产品性能优化和用户体验改善等小技巧方面的分享,以提高我们的 ...