HDU 3618 Good Plan(费用流)
s t v
means that someone want to rant a house from the day s to t paying v yuan totally (including the day s and t, 0 ≤ s ≤ t ≤ 400, 0 ≤ v ≤ 100,0000).
A house can be only rant to one person, and FJ should either accept an order totally or reject it.
此题用来测试了一下SPFA费用流的模版。建图:每个s到t连一条容量为1,代价为v的边,每一天与下一天连一条容量为2,代价为0的边,S与第0天连边,T与最后一天连边,容量为2,代价为0,最大费用最大流即为答案。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #define INF 0x7fffffff 6 using namespace std; 7 const int MAXN=500,MAXM=4000; 8 struct mcflow{ 9 int N,M,S,T; 10 int head[MAXN],next[MAXM],to[MAXM],cost[MAXM],f[MAXM],ecnt; 11 int pre[MAXN],d[MAXN]; 12 bool vis[MAXN]; 13 #define CL(x) memset(x, 0, sizeof(x)); 14 void init(){ 15 CL(head); CL(next); CL(to); CL(cost); 16 ecnt = 2; 17 T = S = N = M = 0; 18 } 19 void make_edge(int a, int b, int cc, int d){ 20 next[ecnt] = head[a]; 21 head[a] = ecnt; 22 to[ecnt] = b; 23 f[ecnt] = cc; 24 cost[ecnt++] = d; 25 } 26 void insert(int a, int b, int cc, int d){ 27 make_edge(a, b, cc, d); 28 make_edge(b, a, 0, -d); 29 } 30 bool spfa(){ 31 CL(vis); 32 for(int i = 0; i <= T; ++i) 33 d[i] = INF; 34 queue<int> Q; 35 Q.push(S); 36 vis[S]=1; 37 d[S] = 0; 38 while(!Q.empty()){ 39 int u = Q.front(); Q.pop(); 40 vis[u] = 0; 41 for(int p = head[u]; p; p = next[p]){ 42 if(f[p] > 0 && d[to[p]] > d[u] + cost[p]){ 43 d[to[p]] = d[u] + cost[p]; 44 pre[to[p]] = p; 45 if(!vis[to[p]]){ 46 vis[to[p]]=1; 47 Q.push(to[p]); 48 } 49 } 50 } 51 } 52 return d[T] < INF; 53 } 54 void min_cost_flow(int &Flow, int &fee){ 55 Flow = fee = 0; 56 while(spfa()){ 57 fee += d[T]; 58 int u = T, tmp = INF; 59 while(u != S){ 60 tmp=min(f[pre[u]], tmp); 61 u=to[pre[u]^1]; 62 } 63 u=T; 64 while(u != S){ 65 f[pre[u]] -= tmp; 66 f[pre[u]^1] += tmp; 67 u = to[pre[u]^1]; 68 } 69 Flow += tmp; 70 } 71 } 72 int mincost(){ 73 int ret, tmp; 74 min_cost_flow(tmp, ret); 75 return ret; 76 } 77 int maxflow(){ 78 int ret, tmp; 79 min_cost_flow(ret, tmp); 80 return ret; 81 } 82 }G; 83 84 int main() 85 { 86 int T,m,s,t,v,i; 87 scanf("%d",&T); 88 while(T--){ 89 G.init(); 90 scanf("%d",&m); 91 while(m--){ 92 scanf("%d%d%d",&s,&t,&v); 93 G.insert(s,t+1,1,-v); 94 } 95 G.S = 401; G.T = 402; 96 G.insert(G.S,0,2,0); G.insert(400,G.T,2,0); 97 for(i = 0; i < 400; ++i) G.insert(i,i+1,2,0); 98 printf("%d\n",-G.mincost()); 99 } 100 return 0; 101 }
View Code
更新(2015年11月7日):
上面的模板只在每次流量增量是1的时候才是对的。增量不是1就会出错。
转载于:https://www.cnblogs.com/oyking/archive/2013/06/04/3116663.html
HDU 3618 Good Plan(费用流)相关推荐
- HDU 5383 Yu-Gi-Oh!(费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5383 题意:游戏王同调召唤的方法,每只怪兽有等级和攻击力,两种不同类型的怪兽可以在一定限制下召唤出某一 ...
- POJ 2135 Farm Tour amp;amp; HDU 2686 Matrix amp;amp; HDU 3376 Matrix Again 费用流求来回最短路...
累了就要写题解,近期总是被虐到没脾气. 来回最短路问题貌似也能够用DP来搞.只是拿费用流还是非常方便的. 能够转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1.然后连 ...
- 【HDU】4411 Arrest 费用流
传送门:[HDU]4411 Arrest 题目分析:题目的意思一开始没看懂= =...题意大致为:派出至多K个警队遵守先灭小的再灭老的的原则将N个城市的帮派全端了(要灭编号大的必须要先灭编号小的).且 ...
- HDU 4411 Arrest(费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4411 题意:有n+1个城市,编号0到n.其中警察局在0号城市,1到n号城市中每个城市都有一个小偷.现在 ...
- 网络流 最大流 最小割 费用流
[腾讯文档]网络流初步 网络流初步 文章目录 网络流初步 一.网络流简介 1. 网络 2. 流 3. 再次理解网络流 二.常见题型(三种) 三.相关问题对应算法介绍 1.最大流 (1) FF算法 - ...
- hdu 2448 Mining Station on the Sea(最短路+费用流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2448 题意:给你一个由N个港口和M个海上油田构成的连通无向图(给出了图中所有的边和权值),现在给你N个 ...
- hdu 3395(费用流,二分图的最大权匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3395 解题思路: 这个构图很容易出错,最开始都容易想,把每个点拆开,分为攻击和被攻击的,建图如下: 源 ...
- HDU Problem - 1533 Going Home(费用流板子题)
题目链接 Problem Description On a grid map there are n little men and n houses. In each unit time, every ...
- HDU 4833 Best Financing 一脸费用流的dp
原题:http://acm.hdu.edu.cn/showproblem.php?pid=4833 题意:中文题不解释... 解题思路: 首先以interest_rates为费用建图跑费用流是比较容易 ...
最新文章
- JAVA 和.NET在安全功能的比较
- OpenStack icehouse系列之计算节点搭建
- 《JavaScript面向对象编程指南》——1.7 训练环境设置
- 掌握Rabbitmq几个重要概念,从一条消息说起
- IDE日志分析方法pt。 1个
- python如何仿写文章_python,python3.x_求助,用python仿写以下代码,python,python3.x,java - phpStudy...
- jggrid 设置了自适应宽度仍然有滚动条的问题
- linux系统关于mysql的命令_[操作系统]Linux 操作MySql命令
- Linux安装wireshark并配置权限
- 常用函数的连续傅里叶变换对
- 谷歌修复安卓System 组件中的多个 RCE 漏洞
- 有关语音识别技术的一些信息点
- LD_PRELOAD用法总结
- 深度学习之神经网络(二)
- 水经注万能地图下载器功能大全
- VUE+element-ui开发出的美观好看的登录注册模板组件 集成方便
- Spring Boot入门HelloWorld
- allegro 尺寸标注操作未到板边的处理
- Linux 磁盘清理
- python制作气温分布图_基于Python的多种形式气温分布图自动绘制