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. 使用ubifs格式的根文件系统

    配置内核,使其支持ubifs文件系统 1)Device Drivers  --->Memory Technology Device (MTD) support  --->UBI - Uns ...

  2. 2018ICPC南京网络赛

    2018ICPC南京网络赛 A. An Olympian Math Problem 题目描述:求\(\sum_{i=1}^{n} i\times i! \%n\) solution \[(n-1) \ ...

  3. React-Native 之 FlexBox介绍和使用

    # 前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会 ...

  4. linux文件管理 -> 系统文件属性

    -rw-------. 1 root root 4434 May 30 13:58 ks.cfg -rw-------. ①:文件类型与权限 ②:硬链接次数 root ③:所属用户 root ④:所属 ...

  5. day07作业

    import java.util.Scanner; class SsqGame { public static void main(String[] args) { int total = 0;//买 ...

  6. dpr 与 dproj 有什么区别

  7. sem_open中信号量命名

    问题: sem_open will failed with "No such file or directory"   解释1: 这是由于在Linux内核中,创建信号量的默认路径是 ...

  8. HBase 入门笔记-数据落地篇

    一.前言 关于数据落地方面,HBase官网也有相关介绍.本文主要介绍一下实际工作中涉及的数据存储方面的一些经验和技巧,主要涉及表rowkey设计.数据落地方案 二.表设计 相对于MySQL等关系型数据 ...

  9. C#socket编程序(一)

    在讲socket编程之前,我们先看一下最常用的一些类和方法,相信这些能让你事半功倍. 一.IP地址操作类 1.IPaddress类 a.在该类中有一个 parse()方法,能够把点分十进制IP地址 转 ...

  10. 拥抱 Android Studio 之一:从 ADT 到 Android Studio

    http://kvh.io/cn/embrace-android-studio-migration.html 1. 拥抱变化,拥抱新事物 Android Studio(IntelliJ IDEA)vs ...