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 贪心/费用流的更多相关文章
-
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 ...
随机推荐
-
JAVA基础,字符串
字符串String(一个字符数组,常量,不可变): 1. 创建并初始化字符串: 1). 使用字符串常量直接初始化 String s="hello!"; 2). 使用构造方法创建并初 ...
-
选择本地照片之后即显示在Img中(客户体验)
最近转战MVC项目,然后又再次遇到照片上传的实现,之前都是使用ASP.NET,虽然也有照片上传,而且出于客户体验考虑, 也实现了选择本地照片之后即时显示在IMG中,在这里就简单介绍其实现(ASP.NE ...
-
【C语言】指针
错误一: 一种错误的写法: * sizeof(int)); * sizeof(int)); y = x; 没有必要为y开辟内存,因为y在开辟内存时 y内存储的地址时开辟的内存的位置, 但是后面又把x的 ...
-
使用pip安装python插件的时候出现Microsoft Visual C++ 9.0缺失错误
使用pip安装python插件的时候出现Microsoft Visual C++ 9.0缺失错误 使用pip安装python插件的时候出现Microsoft Visual C++ 9.0缺失错误 : ...
-
【推荐】开放静态文件 CDN服务staticfile.org
虽然国内外有很多类似的服务器,比如最初的google ajax api,还有后来的sae,百度等都有提供,但是也都有不同的弊端,比如国内访问速度慢.提供的静态文件不全等...staticfile有望解 ...
-
背包问题matlab程序
clearclca=0.95k=[5;10;13;4;3;11;13;10;8;16;7;4];k=-k;d=[2;5;18;3;2;5;10;4;11;7;14;6];restriction=46; ...
-
Linux实战教学笔记21:Rsync数据同步工具
第二十一节 Rsync数据同步工具 标签(空格分隔): Linux实战教学笔记-陈思齐 ---本教学笔记是本人学习和工作生涯中的摘记整理而成,此为初稿(尚有诸多不完善之处),为原创作品,允许转载,转载 ...
-
SSM之框架整合
前言 SSM框架,即Spring + Spring MVC + MyBatis的整合框架集,是继SSH后比较主流的Java EE企业级框架,采用标准的MVC模式,项目结构与微软的ASP.NET MVC ...
-
AP与CP介绍【转】
本文转载子:https://blog.csdn.net/wqlinf/article/details/8663170 基带芯片加协处理器(CP,通常是多媒体加速器).这类产品以MTK方案为典型代表,M ...
-
unix 网络编程 第五章
个人对unix 网络编程中的代码进行了精简,保留了主要和关键部分. 1 tcpserve01 程序见 https://github.com/juniperdiego/Unix-network-prog ...