Dakar Rally


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Description

The Dakar Rally is an annual Dakar Series rally raid type of off-road race, organized by the Amaury Sport Organization. The off-road endurance race consists of a series of routes. In different routes, the competitors cross dunes, mud, camel grass, rocks, erg and so on.

Because of the various circumstances, the mileages consume of the car and the prices of gas vary from each other. Please help the competitors to minimize their payment on gas.

Assume that the car begins with an empty tank and each gas station has infinite gas. The racers need to finish all the routes in order as the test case descripts.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 50) indicating the number of test cases. Then T test cases follow.

The first line of each case contains two integers: n -- amount of routes in the race; capacity -- the capacity of the tank.

The following n lines contain three integers each: mileagei -- the mileage of the ith route; consumei -- the mileage consume of the car in the ith route , which means the number of gas unit the car consumes in 1 mile; pricei -- the price of unit gas in the gas station which locates at the beginning of the ith route.

All integers are positive and no more than 105.

Output

For each test case, print the minimal cost to finish all of the n routes. If it's impossible, print "Impossible" (without the quotes).

Sample Input

2
2 30
5 6 9
4 7 10
2 30
5 6 9
4 8 10

Sample Output

550
Impossible

Author: OUYANG, Jialin
Contest: The 13th Zhejiang University Programming Contest

用一个双端队列维护,使容量为C的油箱中,便宜的油的比例最大,并优先使用便宜的油。。。。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <deque>
#include <cmath> using namespace std; typedef long long int LL; struct ROAD
{
int m,c,p,cpm;
}road[]; struct OIL
{
int P,L;
bool operator<(OIL t) const
{
return P>t.P;
}
}; int N,C,t; LL solve()
{
LL ret=;
int vol=;///zhong gong C L de you
deque<OIL> dq;
for(int i=;i<=N;i++)
{
while(vol&&!dq.empty()&&dq.back().P>road[i].p)
{
vol-=dq.back().L;
dq.pop_back();
}
OIL t; t.L=C-vol; t.P=road[i].p;
vol=C-road[i].cpm;
dq.push_back(t);
int youliang=road[i].cpm;
while(youliang>)
{
OIL& cur=dq.front();
int Minn=min(cur.L,youliang);
ret+=(LL)Minn*cur.P;
youliang-=Minn; cur.L-=Minn;
if(!cur.L) dq.pop_front();
}
}
return ret;
} int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&C);
bool flag=true;
for(int i=;i<=N;i++)
{
scanf("%d%d%d",&road[i].m,&road[i].c,&road[i].p);
road[i].cpm=road[i].c*road[i].m;
if(road[i].cpm>C) flag=false;
}
if(flag==false) puts("Impossible");
else printf("%lld\n",solve());
}
return ;
}

ZOJ 3699 Dakar Rally的更多相关文章

  1. ZOJ 3699 Dakar Rally(贪心)

    这是一道贪心题,他的贪心思想很容易想明白,我们保证油箱里的油始终是最便宜的我们最后的花费就能是最少的.实现方法就是:比如现在在i点,我们看邮箱满载能最远到达哪里,不妨设最远到达j,(j >= i ...

  2. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  3. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  4. ZOJ Problem Set - 1394 Polar Explorer

    这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...

  5. ZOJ Problem Set - 1392 The Hardest Problem Ever

    放了一个长长的暑假,可能是这辈子最后一个这么长的暑假了吧,呵呵...今天来实验室了,先找了zoj上面简单的题目练练手直接贴代码了,不解释,就是一道简单的密文转换问题: #include <std ...

  6. ZOJ Problem Set - 1049 I Think I Need a Houseboat

    这道题目说白了是一道平面几何的数学问题,重在理解题目的意思: 题目说,弗雷德想买地盖房养老,但是土地每年会被密西西比河淹掉一部分,而且经调查是以半圆形的方式淹没的,每年淹没50平方英里,以初始水岸线为 ...

  7. ZOJ Problem Set - 1006 Do the Untwist

    今天在ZOJ上做了道很简单的题目是关于加密解密问题的,此题的关键点就在于求余的逆运算: 比如假设都是正整数 A=(B-C)%D 则 B - C = D*n + A 其中 A < D 移项 B = ...

  8. ZOJ Problem Set - 1001 A + B Problem

    ZOJ ACM题集,编译环境VC6.0 #include <stdio.h> int main() { int a,b; while(scanf("%d%d",& ...

  9. zoj 1788 Quad Trees

    zoj 1788 先输入初始化MAP ,然后要根据MAP 建立一个四分树,自下而上建立,先建立完整的一棵树,然后根据四个相邻的格 值相同则进行合并,(这又是递归的伟大),逐次向上递归 四分树建立完后, ...

随机推荐

  1. MVC MemeberShip vs. Asp.net Identity

    参考 从Membership 到 .NET4.5 之 ASP.NET Identity Extending Identity Accounts and Implementing Role-Based ...

  2. 帆软报表FineReport2016年1月份产品更新一览

    .条件属性可使用页码参数插件 由于报表计算逻辑关系,条件属性中取不到页码公式.但是有些场景下又是需要在条件属性中取到页码的,比如标题只要偶数页显示,比如奇数页标题标红等等. 插件安装完成后,条件属性里 ...

  3. (转)java字节流和字符流的区别

    转载: http://www.cnblogs.com/dolphin0520/category/361055.html 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同 ...

  4. Android(Linux)线路规程的使用

        一般来说,车载导航主机都需要外接若干个UART的外设,如支持HFP的蓝牙模块.与原车通信的CAN解码盒模块.u-blox的GPS模块和DVD机芯等.早年使用Telechips TCC8902+ ...

  5. 科学计算器(JAVA实现)

    前记: 大二学 Java 的时候写的,现在贴上来,只为留念. 再翻代码,自己看着都头疼.一重重的 if 嵌套,当时写得费劲,现在看着更费劲. 代码思想: 代码的大致思想是这样: 首先定义一个算式字符串 ...

  6. 第3章 Linux常用命令(5)_网络命令和挂载命令

    7. 网络命令 7.1 给用户发信息,以ctr+D保存结束 (1)write命令 命令名称 write 命令所在路径 /user/bin/write 执行权限 所有用户 语法 write <用户 ...

  7. 便捷的方式在手机上查看Unity3D的Console Log(调试信息)

    Logs Viewer 功能描述 Using this tool you can easily check your editor console logs inside the game itsel ...

  8. 常用Git代码托管服务分享

    Git Repository代码托管服务越来越流行,目前有很多商业公司和个人团队逐渐切换项目到 Git平台进行代码托管.本文分享一些常用的Git代码托管服务,其中一些提供私有项目保护服务,特别有利于远 ...

  9. ajax给全局变量赋值问题解决示例

    今天在做项目时,遇到了一个问题.我用的是ajax,要在 ajax里面给一个全局变量赋值,结果死活赋值不上,纠结了好半天,后来上网查了查,才 知道,ajax默认是异步请求,(当要赋值时,此时的值没有拿到 ...

  10. AutoHotKey实现将站点添加到IE的Intranet本地站点

    最近在内部推行CRM系统,其中的CPQ组件要求必须将站点加入到"本地Intranet”才可以正常使用,但是由于使用用户比较多(超过几千人),并且每个用户的计算机水平都不一样,所以让用户手工去 ...