UVALive 4863 Balloons 贪心/费用流
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 (1N
1, 000), and A and B are the number of balloons in rooms A and B, respectively (0
A, B
10, 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, DB
1, 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 贪心/费用流的更多相关文章
- luogu P5470 [NOI2019]序列 dp 贪心 费用流 模拟费用流
LINK:序列 考虑前20分 容易想到爆搜. 考虑dp 容易设\(f_{i,j,k,l}\)表示前i个位置 选了j对 且此时A选择了k个 B选择了l个的最大值.期望得分28. code //#incl ...
- UvaLive 4863 Balloons(贪心)
题意: 给定n个队伍, 然后A房间有a个气球, B房间有b个气球, 然后给出每个队伍所需要的气球数量和到A B房间的距离, 求把气球全部送到每个队伍的最短距离. 分析: 在气球充足的情况下, 那么我们 ...
- 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 - ...
- 【bzoj2424】[HAOI2010]订货 费用流
原文地址:http://www.cnblogs.com/GXZlegend/p/6825296.html 题目描述 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di, ...
- [SDOI2016]数字配对(费用流+贪心+trick)
重点是如何找到可以配对的\(a[i]\)和\(a[j]\). 把\(a[i]\)分解质因数.设\(a[i]\)分解出的质因数的数量为\(cnt[i]\). 设\(a[i]\geq a[j]\) 那么\ ...
- UVALive - 6266 Admiral 费用流
UVALive - 6266 Admiral 题意:找两条完全不相交不重复的路使得权值和最小. 思路:比赛的时候时间都卡在D题了,没有仔细的想这题,其实还是很简单的,将每个点拆开,连一条容量为1,费用 ...
- UOJ #455 [UER #8]雪灾与外卖 (贪心、模拟费用流)
题目链接 http://uoj.ac/contest/47/problem/455 题解 模拟费用流,一个非常神奇的东西. 本题即为WC2019 laofu的讲课中的Problem 8,经典的老鼠进洞 ...
- 贪心(模拟费用流):NOIP2011 观光公交
[问题描述] 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0 分钟出现在1号景点,随后依次前往2. ...
- 【 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 ...
随机推荐
- Owin WebApi版本控制
public class WebApiControllerSelector : IHttpControllerSelector { private const string NamespaceKey ...
- No.10 selenium学习之路之通过元素定位获取属性
1. implicitly_wait()隐形等待.等待页面加载完成,作用是全局的. 时间可以设置的长,短时间也没有影响.直到设置的时间耗完 时间耗完也不会报错 2.获取title值 driver.ti ...
- P2733 家的范围 Home on the Range
又是一校内模拟赛见的题 不知道为什么出题人怎么这么喜欢USACO的Farmer John的他的牛... 感觉这道题不是特别的难,但也不很水 同机房的神仙们都说这个题是一道二维前缀和的裸题,但我当时的确 ...
- 洛谷P3621风铃
传送门啦 分析: 这个题看起来像是个树形dp,嗯,就是看起来像. 所以我们就按树形dp的思路去分析就好了,这个题是一个树形dp的变形题. 和以前建树是一样的,我们用邻接表来进行储存.利用邻接表的特性, ...
- 开发者常用的 Sublime Text 3 插件
1.官网下载 Sublime Text 3 (已有安装包的,请忽略) Sublime Text 官网下载地址 : http://www.sublimetext.com/ 2.打开 Sublime Te ...
- AngularJs(SPA)单页面SEO以及百度统计应用(上)
只有两种人最具有吸引力,一种是无所不知的人,一种是一无所知的人 问:学生问追一个女孩总是追不上怎么办?回答:女孩不是追来的,是吸引来的,你追的过程是吸引女孩的过程,如果女孩没有看上你,再追都是没有用的 ...
- C#socket编程序(一)
在讲socket编程之前,我们先看一下最常用的一些类和方法,相信这些能让你事半功倍. 一.IP地址操作类 1.IPaddress类 a.在该类中有一个 parse()方法,能够把点分十进制IP地址 转 ...
- SQL中的注释语句
SQL中的注释分为单行注释和多行注释.顾名思义,单行注释就是对一行进行注释,多行注释就是同时对多行进行注释. 一.单行注释 SQL语句中的单行注释使用 -- create database datab ...
- USACO 4.3 Letter Game (字典树)
Letter GameIOI 1995 Figure 1: Each of the 26 lowercase letters and its value Letter games are popula ...
- Django实战(6):对比RoR和Django的模板系统
scaffold的生成物虽然用处不大,但是给我们带来一些最佳实践.其中就有模板的继承和分区. 如果你深入使用过rails的模板体系,那么恭喜你:你有超强的忍耐力!而且更重要的是,你只需要3分钟就可以理 ...