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. 被我误解的max_connect_errors【转】

    实为吾之愚见,望诸君酌之!闻过则喜,与君共勉 第一节  什么是max_connect_errors 一开始接触这个参数的时候,感觉他和max_connections的含义差不多,字面意思简单明了,这个 ...

  2. oracle11g字符集问题之一

    select * from T_WORK_EXPERIENCE t where ROLE=N'被雇佣者' 因为ROLE为NVARCHAR2(30),所以要加N.pl/sql developer 中可以 ...

  3. 大数据系列之数据仓库Hive中分区Partition如何使用

    Hive系列博文,持续更新~~~ 大数据系列之数据仓库Hive原理 大数据系列之数据仓库Hive安装 大数据系列之数据仓库Hive中分区Partition如何使用 大数据系列之数据仓库Hive命令使用 ...

  4. Mac下破解intellij IDEA 2018

    一.在进入下面网站下载破解补丁 http://idea.lanyus.com/ 二.在“应用程序”中找到已经安装的IntelliJ IDEA,在app上右键,选择“显示包内容”,如下图: 将下载的破解 ...

  5. vue总结05 过渡--状态过渡

    状态过渡 Vue 的过渡系统提供了非常多简单的方法设置进入.离开和列表的动效.那么对于数据元素本身的动效呢,比如: 数字和运算 颜色的显示 SVG 节点的位置 元素的大小和其他的属性 所有的原始数字都 ...

  6. TStringList 与 泛型字典TDictionary 的 哈希功能效率PK

    结论: 做HashMap 映射 功能的时候 ,字典TDictionary 功能更强大,且效率更高,比如不仅仅可以存String,还可以存结构和类. TDictionary类是一个name,value容 ...

  7. CF312B 【Archer】

    容易算出这人第一次胜利的概率,第二次的,第三次的…… 好像可以无限乘下去 但是这题精度卡到1e-6 不妨设一个eps,当这次胜率小于eps时,就break掉,反正它已经不影响答案了 我设的是eps=1 ...

  8. ROS新动态获取网址汇总

    ROS新动态获取网址汇总 1 planet ROS http://planet.ros.org/ 2 ROS news http://www.ros.org/news/ 3 ROS-Industria ...

  9. Oracle与Sqlserver数据共享

    需求:在一个集成平台中有一个主系统使用的是Oralce数据库,子系统使用的SqlServer 数据库,如何让子系统的数据库与主系统的人员同步呢? 思路:通过服务WebService 公开接口 1.与主 ...

  10. day6 xml文件格式的处理

        XML处理模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公 ...