度度熊的交易计划

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1417    Accepted Submission(s): 519

Problem Description
度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:

喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。

由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。

同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。

由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。

据测算,每一个商品运输1公里,将会花费1元。

那么喵哈哈村最多能够实现多少盈利呢?

 
Input
本题包含若干组测试数据。
每组测试数据包含:
第一行两个整数n,m表示喵哈哈村由n个片区、m条街道。
接下来n行,每行四个整数a[i],b[i],c[i],d[i]表示的第i个地区,能够以a[i]的价格生产,最多生产b[i]个,以c[i]的价格出售,最多出售d[i]个。
接下来m行,每行三个整数,u[i],v[i],k[i],表示该条公路连接u[i],v[i]两个片区,距离为k[i]

可能存在重边,也可能存在自环。

满足:
1<=n<=500,
1<=m<=1000,
1<=a[i],b[i],c[i],d[i],k[i]<=1000,
1<=u[i],v[i]<=n

 
Output
输出最多能赚多少钱。
 
Sample Input
2 1
5 5 6 1
3 5 7 7
1 2 1
 
Sample Output
23
 
Source
 
Recommend
liuyiding
 

Statistic | Submit | Discuss | Note

析:最小费用流,建立一个超级源点 s 和汇点 t,然后从s 向每个地区边一条容量是 b,费用是a,从每个地区从 t 连一条容量为 d,费用为 -c的边,注意是 -c,然后每个地区有路的就直接连上就好,然后在增广的时候,增广的时候到正数的时候就停止。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int maxm = 1e5 + 10;
const int mod = 30007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int from, to, cap, flow, cost;
}; struct Mcmf{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n){
this-> n = n;
for(int i = 0; i < n; ++i) G[i].cl;
edges.cl;
} void addEdge(int from, int to, int cap, int cost){
edges.pb((Edge){from, to, cap, 0, cost});
edges.pb((Edge){to, from, 0, 0, -cost});
m = edges.sz;
G[from].pb(m-2);
G[to].pb(m-1);
} bool bellman(int &flow, int &cost){
ms(inq, 0); for(int i = 0; i <= n; ++i) d[i] = -INF;
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> q;
q.push(s);
while(!q.empty()){
int u = q.front(); q.pop();
inq[u] = 0;
for(int i = 0; i < G[u].sz; ++i){
Edge &e = edges[G[u][i]];
if(e.cap > e.flow && d[e.to] < d[u] + e.cost){
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if(!inq[e.to]){ q.push(e.to); inq[e.to] = 1; }
}
}
}
// printf("%d %d %d\n", cost, d[t], a[t]);
if(d[t] < 0) return false;
int u = t;
while(u != s){
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
cost += d[t] * a[t];
flow += a[t];
return true;
} int mincost(int s, int t){
int flow = 0, cost = 0;
this->s = s; this-> t = t;
while(bellman(flow, cost));
return cost;
}
}; Mcmf mcmf; int main(){
while(scanf("%d %d", &n, &m) == 2){
int s = 0, t = n + 1;
mcmf.init(t + 2);
for(int i = 1; i <= n; ++i){
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
mcmf.addEdge(s, i, b, -a);
mcmf.addEdge(i, t, d, c);
}
while(m--){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
if(u == v) continue;
mcmf.addEdge(u, v, INF, -c);
mcmf.addEdge(v, u, INF, -c);
}
printf("%d\n", mcmf.mincost(s, t));
}
return 0;
}

  

HDU 6118 度度熊的交易计划 (最小费用流)的更多相关文章

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

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

  2. hdu 6118 度度熊的交易计划

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

  3. hdu 6118度度熊的交易计划(费用流)

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

  4. HDU 6118 度度熊的交易计划(最小费用最大流)

    Problem Description度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个 ...

  5. HDU 6118 度度熊的交易计划(网络流-最小费用最大流)

    度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...

  6. HDU 6118 度度熊的交易计划(费用流)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6118 [题目大意] 给出一张无向边权图,每个点最多可以生产b[i]商品,每件代价为a[i], 每个 ...

  7. HDU 6118 度度熊的交易计划 最大费用可行流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6118 题意:中文题 分析: 最小费用最大流,首先建立源点 s ,与超级汇点 t .因为生产一个商品需要 ...

  8. HDU 6118 2017百度之星初赛B 度度熊的交易计划(费用流)

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

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

    /** 题目:度度熊的交易计划 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6118 题意:度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题 ...

随机推荐

  1. 用ASP.Net(C#)连接Oracle数据库的方法及实例

    今天看了一下asp.net连接oracle数据库的方法,得到了如下代码.这段代码打开了MyTable表,并把操作员的名字列出.字段类型是OracleString.读取的时候用的是字段编号,我不知道怎么 ...

  2. linux下nginx安装、配置实战

    1什么是Nginx Nginx("enginex")是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器,在高连接并发的情况下Nginx是Apac ...

  3. 黄聪:WordPress 多站点建站教程(三):主站如何调用子站的文章内容、SQL语句如何写?

    1.如果懂得编程的朋友可以SQL语句,然后加上PHP函数等操作就可以通过直接调用网站的数据库信息来实现想要达到的目的. 既然要用到SQL语句首先得对WordPress多站点数据库有一个了解,多站点激活 ...

  4. 【转】Java 字节流与字符流的区别

    字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢?实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作 ...

  5. Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找

    ylbtech-Java-Runoob-高级教程-实例-字符串:06. Java 实例 - 字符串查找 1.返回顶部 1. Java 实例 - 字符串搜索  Java 实例 以下实例使用了 Strin ...

  6. EqualsBuilder 类的使用

    apache的commons-lang包提供了一个EqualsBuilder类,提供了基本属性.数组.对象等比较操作的封装,可以辅助实现对象的equals(Object)方法. 这个类的使用比较简单, ...

  7. 第11课 Qt中的字符串类

    1. 历史遗留问题和解决方案 (1)历史遗留问题 ①C语言不支持真正意义上的字符串 ②C语言用字符数组和一组函数实现字符串操作 ③C语言不支持自定义类型,因此无法获得字符串类型 (2)解决方案 ①从C ...

  8. UCI一位搞theory的教授列的一些数学方面的比较趣味性的网页

    http://www.ics.uci.edu/~eppstein/recmath.html 据说是recreational的,没具体看,先收藏下来.

  9. wampserver中php版本的升级

    以php5.3.10到5.4.31版本为例: 1.  停止WAMP服务器. 2.  去网站windows.php.net 下载php-5.4.31-Win32-VC9-x86.zip. 不要下载THE ...

  10. Spring Boot自动配置

    Spring Boot自动配置原理 Spring Boot的自动配置注解是@EnableAutoConfiguration, 从上面的@Import的类可以找到下面自动加载自动配置的映射. org.s ...