There will be several test cases in the input. Each test case will begin with a line with three integers:

N A B

Where N is the number of teams (1N1, 000), and A and B are the number of balloons in rooms A and B, respectively (0A, B10, 000). On each of the next N lines there will be three integers, representing information for each team:

K DA DB

Where K is the total number of balloons that this team will need, DA is the distance of this team from room A, and DB is this team's distance from room B (0DA, DB1, 000). You may assume that there are enough balloons - that is,
(K's)A + B. The input will end with a line with three 0s.

Output

For each test case, output a single integer, representing the minimum
total distance that must be traveled to deliver all of the balloons.
Count only the outbound trip, from room A or room B to the team. Don't
count the distance that a runner must travel to return to room A or room
B. Print each integer on its own line with no spaces. Do not print any
blank lines between answers.

Sample Input

3 15 35
10 20 10
10 10 30
10 40 10
0 0 0

Sample Output

300

费用流代码:

#include <cstdio>
#include <cstring>
using namespace std; #define MAXN 2000
#define MAXM 4000
#define INF 0x3f3f3f3f
#define MIN(a,b) (a<b?a:b)
#define V(p) edge[(p)].v
#define F(p) edge[(p)].f
#define C(p) edge[(p)].c
#define Nx(p) edge[(p)].next int n,A,B,s,t,ans,ecnt;
int dis[MAXN],adj[MAXN];
bool vis[MAXN];
struct node
{
int v,f,c,next;
}edge[MAXM*]; void Addedge(int u,int v,int f,int c)
{
++ecnt;
V(ecnt)=v; F(ecnt)=f; C(ecnt)=c;
Nx(ecnt)=adj[u]; adj[u]=ecnt;
++ecnt;
V(ecnt)=u; F(ecnt)=; C(ecnt)=-c;
Nx(ecnt)=adj[v]; adj[v]=ecnt;
} int Aug(int u,int lim)
{
if(u==t){
ans+=lim*dis[s];
return lim;
}
vis[u]=true;
int p,v,f,c,delta,sum=;
for(p=adj[u];p;p=Nx(p)){
v=V(p); f=F(p); c=C(p);
if(vis[v] || !f || dis[v]+c!=dis[u]) continue;
delta=Aug(v,MIN(f,(lim-sum)));
F(p)-=delta; F(p^)+=delta;
sum+=delta;
if(sum==lim) break;
}
return sum;
}
bool Update()
{
int i,p,Min=INF;
for(i=s;i<=t;++i){
if(!vis[i]) continue;
for(p=adj[i];p;p=Nx(p)){
if(!F(p) || vis[V(p)]) continue;
Min=MIN(Min,(dis[V(p)]+C(p)-dis[i]));
}
}
if(Min==INF) return false;
for(i=s;i<=t;++i){
if(!vis[i]) continue;
dis[i]+=Min;
}
return true;
}
void ZKW()
{
do{
for(memset(vis,,sizeof(vis));Aug(s,INF);memset(vis,,sizeof(vis)));
}while(Update());
printf("%d\n",ans);
} void Init()
{
memset(adj,,sizeof(adj));
memset(dis,,sizeof(dis));
ans=; ecnt=; s=; t=n+;
int i,k,da,db;
Addedge(s,n+,A,);
Addedge(s,n+,B,);
for(i=;i<=n;++i){
scanf("%d%d%d",&k,&da,&db);
Addedge(n+,i,INF,da);
Addedge(n+,i,INF,db);
Addedge(i,t,k,);
}
}
int main()
{
while(true){
scanf("%d%d%d",&n,&A,&B);
if(!n && !A && !B) break;
Init();
ZKW();
}
return ;
}

贪心代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 10011
const int inf=0x7fffffff; //无限大
struct node
{
int total;
int da;
int db;
int dis;
};
bool cmp(node a,node b)
{
return a.dis>b.dis;
}
node team[maxn];
int main()
{
sspeed;
int n,a,b;
while(cin>>n>>a>>b)
{
if(n==&&a==&&b==)
break;
int sum=;
for(int i=;i<n;i++)
{
cin>>team[i].total>>team[i].da>>team[i].db;
team[i].dis=fabs(team[i].da-team[i].db);
}
sort(team,team+n,cmp);
for(int i=;i<n;i++)
{
if(team[i].da<team[i].db)
{
if(a>=team[i].total)
{
sum+=team[i].da*team[i].total;
a-=team[i].total;
}
else
{
sum+=team[i].da*a;
sum+=(team[i].total-a)*team[i].db;
b-=(team[i].total-a);
a=;
}
}
else
{
if(b>=team[i].total)
{
sum+=team[i].db*team[i].total;
b-=team[i].total;
}
else
{
sum+=team[i].db*b;
sum+=(team[i].total-b)*team[i].da;
a-=(team[i].total-b);
b=;
}
}
}
cout<<sum<<endl;
}
}

UVALive 4863 Balloons 贪心/费用流的更多相关文章

  1. luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流

    LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...

  2. UvaLive 4863 Balloons(贪心)

    题意: 给定n个队伍, 然后A房间有a个气球, B房间有b个气球, 然后给出每个队伍所需要的气球数量和到A B房间的距离, 求把气球全部送到每个队伍的最短距离. 分析: 在气球充足的情况下, 那么我们 ...

  3. CodeForces - 884F :Anti-Palindromize(贪心&费用流)

    A string a of length m is called antipalindromic iff m is even, and for each i (1 ≤ i ≤ m) ai ≠ am - ...

  4. 【bzoj2424】[HAOI2010]订货 费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...

  5. [SDOI2016]数字配对(费用流+贪心+trick)

    重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...

  6. UVALive - 6266 Admiral 费用流

    UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...

  7. UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)

    题目链接 http://uoj.ac/contest/47/problem/455 题解 模拟费用流,一个非常神奇的东西. 本题即为WC2019 laofu的讲课中的Problem 8,经典的老鼠进洞 ...

  8. 贪心(模拟费用流):NOIP2011 观光公交

    [问题描述] 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0 分钟出现在1号景点,随后依次前往2. ...

  9. 【 UVALive - 2197】Paint the Roads(上下界费用流)

    Description In a country there are n cities connected by m one way roads. You can paint any of these ...

随机推荐

  1. jQuery简单介绍

    一.jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交 ...

  2. jmter提交图片

    jmter提交图片 https://www.cnblogs.com/linglingyuese/p/4514808.html

  3. 详解MySQL大表优化方案

    单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型值为主的表在千万级以下,字符串为主的表在五百万以下是没有太大问题的.而事实上很多时 ...

  4. Fedora下Eclipse/MyEclipse崩溃的解决方案

    Fedora19下使用myeclipse2013时,打开不到一分钟就异常关闭. 默认在home目录下生成一个log文件,里面显示的错误信息,说是libsoup.so文件导致出错.第一个想法是删除这个文 ...

  5. 关于ARM指令那些你必须知道的东西

    1.32位ARM指令每一位都有其作用,具体如下: 低12为第二操作数, 12~15位为目的寄存器, 16~19位为第一操作数, 20~27就是操作码, 28~31就是条件域. 2.多寄存器load和s ...

  6. Phoenix的安装使用与SQL查询HBase

    一. Phoenix的简介 1. 什么是phoenix 现有hbase的查询工具有很多如:Hive,Tez,Impala,Shark/Spark,Phoenix等.今天主要说Phoenix.phoen ...

  7. Linux下光盘镜像生成和刻录

    mkiosfs命令如在/root/下有文件file1 file2 file3maiosfs -o img.ios file1 file2 file3该命令将file1 file2 file3放入到im ...

  8. Linq To Sql 使用初探

    最近有数据处理需要,就是那种从数据库中把数据取出来 ,对其中的部分字段做一些处理再吐回去的工作,从同事那里学习到了,这中活最适合使用 Linq to Sql 这种方式,不用搭建框架,不用自建实体,直接 ...

  9. 15:链表中倒数第K个节点

    /** * 面试题15:链表中倒数第K个节点 * 输入一个链表,输出该链表中倒数第k个结点. */ public class _15_linked_K { public static void mai ...

  10. js根据IP跳转

    <script language="javascript" type="text/javascript" src="http://int.dpo ...