题意:

n行, a房间的气球,b房间的气球

i行需要的气球,与a房的距离,b房的距离

求最小距离

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <algorithm>
#include <stdlib.h> #define N 2000
#define M 10100
#define inf 107374182
#define ll int using namespace std;
inline ll Min(ll a,ll b){return a>b?b:a;}
inline ll Max(ll a,ll b){return a>b?a:b;} int n;
//双向边,注意RE 注意这个模版是 相同起末点的边 合并而不是去重
struct Edge{
int from, to, flow, cap, nex, cost;
}edge[M*2]; int head[N], edgenum;//2个要初始化-1和0
void addedge(int u,int v,int cap,int cost){//网络流要加反向弧
Edge E={u, v, 0, cap, head[u], cost};
edge[edgenum]=E;
head[u]=edgenum++; Edge E2={v, u, 0, 0, head[v], -cost}; //这里的cap若是单向边要为0
edge[edgenum]=E2;
head[v]=edgenum++;
}
int D[N], P[N], A[N];
bool inq[N];
bool BellmanFord(int s, int t, int &flow, int &cost){
for(int i=0;i<=n+4;i++) D[i]= inf; memset(inq, 0, sizeof(inq));
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=head[u]; i!=-1; i=edge[i].nex){
Edge &E = edge[i];
if(E.cap > E.flow && D[E.to] > D[u] +E.cost){
D[E.to] = D[u] + E.cost ;
P[E.to] = i;
A[E.to] = Min(A[u], E.cap - E.flow);
if(!inq[E.to]) Q.push(E.to) , inq[E.to] = 1;
}
}
}
if(D[t] == inf) return false;
flow += A[t];
cost += D[t] * A[t];
int u = t;
while(u != s){
edge[P[u]].flow += A[t];
edge[P[u]^1].flow -= A[t];
u = edge[P[u]].from;
}
return true;
} int Mincost(int s,int t){
int flow = 0, cost = 0;
while(BellmanFord(0, n+3, flow, cost));
return cost;
}
int main(){
int i,disa,disb,flow,maxa,maxb;
while( scanf("%d %d %d",&n, &maxa, &maxb), n+maxb+maxa){
memset(head,-1,sizeof(head)); edgenum=0; addedge(n+1, n+3, maxa, 0);
addedge(n+2, n+3, maxb, 0); for(i=1;i<=n;i++){
scanf("%d %d %d",&flow,&disa,&disb);
addedge(0, i, flow, 0);
addedge(i, n+1, flow, disa);
addedge(i, n+2, flow, disb);
} printf("%d\n",Mincost(0,n+3) ); }
return 0;
}
/*
3 15 35
10 20 10
10 10 30
10 40 10 5 5 0
1 10 1000
1 10 1000
1 10 1000
0 10 1000
1 10 1000 0 0 0 ans:
300
40 */

CFGYM 2013-2014 CT S01E03 D题 费用流模版题的更多相关文章

  1. 【思维题 费用流 技巧】bzoj5403: marshland

    主要还是网络流拆点建图一类技巧吧 Description JudgeOnline/upload/201806/1(4).pdf 题目分析 第一眼看到这题时候只会把每个点拆成4个方向:再强制定向连边防止 ...

  2. HDU 3376 &amp;&amp; 2686 方格取数 最大和 费用流裸题

    题意: 1.一个人从[1,1] ->[n,n] ->[1,1] 2.仅仅能走最短路 3.走过的点不能再走 问最大和. 对每一个点拆点限流为1就可以满足3. 费用流流量为2满足1 最大费用流 ...

  3. Lunch Time(费用流变型题,以时间为费用)

    Lunch Time http://acm.hdu.edu.cn/showproblem.php?pid=4807 Time Limit: 4000/2000 MS (Java/Others)     ...

  4. Coding Contest(费用流变形题,double)

    Coding Contest http://acm.hdu.edu.cn/showproblem.php?pid=5988 Time Limit: 2000/1000 MS (Java/Others) ...

  5. 最大流&最小割&费用流模版

    好久都没有搞博客了.想认真写又要准备文化课期末了. ISAP 流程: 原理就是dfs找增广路. 最基础的建反向边以便反悔就不说了. 但是记录一个dep(dis)表示层数,一开始BFS(从t开始,dis ...

  6. hdu4862 2014多校B题/ 费用流(最优情况下用不大于K条路径覆盖)(不同的解法)

    题意: 一个数字矩阵,可以出发K次,每次可以从右边或者下面走,要求(在收益最大情况下)覆盖全图,不能则输出-1.(规则:每次跳一步的时候若格子数字相等则获得该数字的能量,每跳一步消耗距离的能量).每个 ...

  7. POJ 3686 The Windy's(思维+费用流好题)

    The Windy's Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5362   Accepted: 2249 Descr ...

  8. bzoj3442: 学习小组(费用流好题)

    3442: 学习小组 题目:传送门 题解: 超级好题啊大佬们的神题!建图肥肠灵性!感觉自己是星际玩家... 首先呢st直接向每个人连边,容量为min(k,喜欢的小组个数),费用为0 然后每个人再向ed ...

  9. Going Home POJ - 2195 费用流板子题

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

随机推荐

  1. 使用CXF+spring创建一个web的接口项目

    一.web project整合spring 1.1.打开Myeclipse,建立web project(eclipse为dynamic web project),使用J2EE5.0. 1.2.加入Sr ...

  2. Java开发者易犯错误Top10

    本文总结了Java开发者经常会犯的前十种错误列表. Top1. 数组转换为数组列表 将数组转换为数组列表,开发者经常会这样做: List<String> list = Arrays.asL ...

  3. js 判断微信浏览器(转)

    最近做很多HTML5的项目,很多页面会通过微信微博等SNS分享出去.在分享页面上提供公司APP的下载.但是在很多应用的浏览器中,点击下载链接无法下载应用.那么针对这些浏览器我们需要给用户提示从safa ...

  4. jqueryMobile中select样式自定义

    要去掉引入的jqueryMobile给下拉框组件的样式,有两种办法. 第一种:全局的去掉所有的下拉框样式: <link rel="stylesheet" href=" ...

  5. (转)chm格式的电子书打开是空白的解决办法

    当我们从网络上下载chm格式的电子书到本地后,打开就发现chm格式的电子书右边的目录是正常的,但是chm格式的电子书内容部分却是空白的情况,很多时候我们都以为是中毒了,但是用杀毒软件却杀不出来,其实很 ...

  6. 多线程、多任务管理 简单demo

    需求:假设多个任务需要执行,每个任务不是一时半会能完成(需要能看到中间执行状况): 多个任务 根据条件不同 可能需要不同的处理 分析: 多线程并发执行多任务: 对任务进行管理,追踪中间执行状态: 运用 ...

  7. C#多态联系之虚方法

    class Class1 { static void Main(string[] args) { YuanGong yg = new YuanGong(); JingLi jl = new JingL ...

  8. uva 759 - The Return of the Roman Empire

    #include <cstdio> #include <string> #include <map> using namespace std; ; , , , , ...

  9. Mysql创建函数出错

    目前在项目中,执行创建mysql的函数出错, mysql 创建函数出错信息如下: Error Code: 1227. Access denied; you need (at least one of) ...

  10. java学习笔记 (7) —— 实现简单的上传处理

    1.下载apache 的 commons-fileupload.jar 包及 commons-io.jar 2.引入到lib 文件夹下 3.新建uploadApache.jsp <%@ page ...