/**
题目:度度熊的交易计划
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6118
题意:度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题:
喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区。
由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但是最多生产b[i]个。
同样的,由于每个片区的购买能力的区别,第i个片区也能够以c[i]的价格出售最多d[i]个物品。
由于这些因素,度度熊觉得只有合理的调动物品,才能获得最大的利益。
据测算,每一个商品运输1公里,将会花费1元。
那么喵哈哈村最多能够实现多少盈利呢? 思路:最小费用最大流。 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL;
const int mod = 1e9+;
const int INF = 0x3f3f3f3f;
const int maxn = 5e2+;
int a[maxn], b[maxn], c[maxn], d[maxn];
int f[maxn][maxn];
int n, m;
struct Edge
{
int from, to, cap, flow, cost;
};
struct MCMF
{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n)
{
this->n = n;
for(int i = ; i < n; i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,,cost});
edges.push_back((Edge){to,from,,,-cost});
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BellmanFord(int s,int t,int &flow,int &cost){
for(int i = ; i < n; i++) d[i] = INF;
memset(inq, , sizeof inq);
d[s] = ; inq[s] = ; p[s] = ; a[s] = INF;
queue<int> Q;
Q.push(s);
while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = ;
for(int i = ; i < (int)G[u].size(); 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] = ;}
}
}
}
if(d[t] >= ) return false;///由于求最大费用.
flow += a[t];
cost += d[t]*a[t];
int u = t;
while(u!=s){
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
u = edges[p[u]].from; }
return true;
}
int Mincost(int s,int t)
{
int flow = , cost = ;
while(BellmanFord(s,t,flow,cost)){
}
return cost;
}
};
struct edge{int to, cost;};
typedef pair<int,int> P;
int V;
vector<edge> G[maxn];
int dis[maxn][maxn];
void dijkstra(int s)
{
priority_queue<P, vector<P>, greater<P> > que;///按照first,小的先出。
fill(dis[s],dis[s]+V,INF);
dis[s][s] = ;
que.push(P(,s));
while(!que.empty()){
P p = que.top(); que.pop();
int v = p.second;
if(dis[s][v]<p.first) continue;
for(int i = ; i < (int)G[v].size(); i++){
edge e = G[v][i];
if(dis[s][e.to]>dis[s][v]+e.cost){
dis[s][e.to] = dis[s][v]+e.cost;
que.push(P(dis[s][e.to],e.to));
}
}
}
}
void floyd()
{
for(int k = ; k < n; k++){
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
f[i][j] = min(f[i][j],f[i][k]+f[k][j]);
}
}
}
}
int main()
{
MCMF mcmf;
while(scanf("%d%d",&n,&m)==)
{
int s = n, t = n+;
mcmf.init(t+);
for(int i = ; i<n; i++){
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
mcmf.AddEdge(s,i,b[i],a[i]);///求最大费用。
mcmf.AddEdge(i,t,d[i],-c[i]);
}
int u, v, k;
for(int i = ; i < n; i++) G[i].clear();
for(int i = ; i < m; i++){
scanf("%d%d%d",&u,&v,&k);
if(u==v) continue;
u--, v--;
G[u].push_back((edge){v,k});
G[v].push_back((edge){u,k});
}
V = n;
for(int i = ; i < V; i++){
dijkstra(i);
}
//floyd();///可改成求n次dijkstra,复杂度为N*lg(E);
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(i==j) continue;
if(dis[i][j]!=INF&&c[j]-dis[i][j]-a[i]>){
mcmf.AddEdge(i,j,INF,dis[i][j]);
}
}
}
printf("%d\n",-mcmf.Mincost(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. http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

    http://blog.csdn.net/xingfuzhijianxia/article/details/6433918

  2. TestNG 七 annotation

    TestNG中用到的annotation的快速预览及其属性. @BeforeSuite:   被注释的方法将在所有测试运行前运行 @AfterSuite:  被注释的方法将在所有测试运行后运行 @Be ...

  3. C#运行原理——我的柔情你永远不懂

    记得歌手陈琳曾经在1993年发行了第一张专辑<你的柔情我永远不懂>,创造了150万张的销售纪录,里边的主打歌——我的柔情你永远不懂,多年以后才发现是写给C#运行原理的,因为原理总是伤不起~ ...

  4. python fabric使用 http://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html

    fab -u username -p password  -H hostname -P -- cmd   或root@'hostname'  -H多个主机是引号用逗号隔开 -P异步

  5. ios 程序发布使用xcode工具Application Loader 正在通过ITUNES STORE进行鉴定错误

    ios 程序发布使用xcode工具Application Loader 正在通过ITUNES STORE进行鉴定错误 一:此错误会导致上传程序,一直停留在验证阶段,而没有一点上传进度:结果会苦等半天, ...

  6. 如何为iTunes Connect准备应用

    原地址:http://blog.sina.com.cn/s/blog_947c4a9f0101dded.html 如果你已经成功注册了iOS开发者,那么现在就可以登陆iTunes Connect来管理 ...

  7. python——关于Python Profilers性能分析器

    1. 介绍性能分析器 profiler是一个程序,用来描述运行时的程序性能,并且从不同方面提供统计数据加以表述.Python中含有3个模块提供这样的功能,分别是cProfile, profile和ps ...

  8. CPU利用率与Load Average的区别?

    CPU利用率,是对一个时间段内CPU使用状况的统计,通过这个指标可以看出在某一个时间段内CPU被占用的情况,如果CPU被占用时间很高,那么就需要考虑CPU是否已经处于超负荷运作,长期超负荷运作对于机器 ...

  9. Cacti监控MySQL实现过程中碰到的问题解汇总

    前言:cacti监控mysql服务器的大概50张graphs都弄出来了,也出图了,当中遇到一些问题,印象比較深刻的记录例如以下: (一):加入io监控 点击Create Graphs for this ...

  10. 转发:【PHP】转义和过滤html单、双引号及HTML标签

    一.单引号和双引号转义在PHP的数据存储过程中用得比较多,即往数据库里面存储数据时候需要注意转义单.双引号: 先说几个PHP函数: 1.addslashes — 使用反斜线引用(转义)字符串: 返回字 ...