# 汽车加油行驶问题

7729人阅读 评论(0)

http://poj.org/problem?id=2431   Expedition

/*
n个加油站,,邮箱容量不限,,,,每个加油站可加的油量有限,,,求最少的加油次数

*/
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;

struct Node
{
int distance,fuel;  //定义一个优先队列
friend bool operator<(Node a, Node b)
{     //从小到大排序采用“>”号；如果要从大到小排序，则采用“<”号
return a.fuel < b.fuel;       //从大到小排序
}
}node[10001];

bool cmp(const Node &a,const Node &b)
{
return a.distance < b.distance;
}
void Solve(int n,int L,int P)
{
int i=0,j=P,ans=0;
priority_queue<Node>q;  //用优先队列来维护一个按照加油站油量从大到小来排序
Node temp;
while(node[i].distance<0)
i++;
while(i < n && j < L)
{
while(i < n && node[i].distance <= j)   //有油的时候能走多远就走多远
{
q.push(node[i]);
++i;
}
if(q.empty())
break;
temp = q.top();
q.pop();
j += temp.fuel;   //如果走不到，就必须要加一次油，途中会遇到很多加油站，一定要选择油最多的那个加油站
ans++;
}
if(j < L)
printf("-1\n");
else
printf("%d\n",ans);
}
int main(void)
{
int i,n,L,P;
while (scanf("%d",&n)!=EOF)
{
sort(node,node+n,cmp);
for (i = 0; i < n; ++i)
scanf("%d %d",&node[i].distance,&node[i].fuel);
scanf("%d %d",&L,&P);
for (i = 0; i < n; ++i)
node[i].distance = L - node[i].distance;   //当前位置跟加油站之间的距离
sort(node,node+n,cmp);
Solve(n,L,P);
}
return 0;
}

http://poj.org/problem?id=2465    Adventures in Moving - Part IV

/*

*/
//汽车油箱为200，初始油箱内只有100，当到达目的地时，油箱内的油>=100

#include<iostream>
#include<cstdio>
using namespace std;
#include<memory.h>

#define INF 1061109567

inline int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}

int dist[103],price[103];
int dp[103][203];  //dp[i][j]表示到达地i个加油站，还剩下j油量的最少代价

int main(void)
{
int i,j,k,n,len,d,p,m,ans;
scanf("%d",&len);
n=0;
while(scanf("%d %d",&d,&p) != EOF)
{
dist[n] = d;
price[n++] = p;
}
dist[n] = len;
price[n++] = INF;
memset(dp,0x3f3f3f,sizeof(dp));

dp[0][100-dist[0]] = 0;
for(i = 0 ; i < n - 1 ; ++i)
{
for(j = 0 ; j <= 200 ; ++j)
{
if(dp[i][j] == INF)
continue;
for(k = 0 ; k+j <= 200 ; ++k)
{
m = k + j - (dist[i+1] - dist[i]);
if(m < 0 )
continue;
dp[i+1][m] = min(dp[i+1][m],dp[i][j] + k*price[i] );
}
}
}
ans = INF;
for(i = 100 ; i <= 200 ; ++i)
{
if(dp[n-1][i] < ans)
ans = dp[n-1][i];
}
if(ans == INF)
puts("Impossible");
else
printf("%d\n",ans);
return 0;
}

http://pat.zju.edu.cn/contests/pat-practise/1033    To Fill or Not to Fill

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

struct Node
{
double price;
double length;
}node[502];
bool cmp(const Node &a,const Node &b)
{
return a.length<b.length;
}
double  capacity , dist ;
int unit_gas ,n;

int main(void)
{
int i,j,m,index;
double sum,len,cur_capacity,min_price;
bool flag;
while (scanf("%lf %lf %d %d",&capacity,&dist,&unit_gas,&n)!=EOF)
{
len = capacity*unit_gas;
for (i = 0; i < n; ++i)
scanf("%lf %lf",&node[i].price,&node[i].length);
sort(node,node+n,cmp);
node[n].price = 0;
node[n].length = dist;

if(node[0].length>0)
{
printf("The maximum travel distance = 0.00\n");
continue;
}
else
{
flag = false;
cur_capacity = 0;
sum = 0;
for( i = 0 ; i < n ;)
{
if(node[i+1].length - node[i].length > len)    //某两个油站之间的距离大于汽车油箱装满油量的最大行程
{
flag = true;
printf("The maximum travel distance = %.2lf\n",node[i].length + len);
break;
}
else
{
index = i;
min_price = node[i].price;
//找出当前油箱里的油能到达的所有加油站里，油价最便宜的那个
for(j = i + 1 ; node[j].length - node[i].length <= cur_capacity*unit_gas && j <= n ; j++)
{
if(node[j].price < min_price)
{
min_price = node[j].price;
index = j;
}
}
if(index != i)
{
cur_capacity -= (node[index].length - node[i].length)/unit_gas;
i = index;
continue;
}
//若找不到，找出最近的一个能到达的比当前油价便宜的站，加一些油，跑到那个站
index = i;
for(j = i + 1 ; node[j].length - node[i].length <= len && j <= n ; j++)
{
if(node[j].price < node[i].price)
{
index = j;
break;
}
}
if(index != i)
{
sum += ((node[index].length - node[i].length)/unit_gas - cur_capacity)*node[i].price;
cur_capacity = 0;
i = index;
continue;
}
//找不到比当前油站的价格还便宜的油站的时候
//在当前油站需要加满油，跑到能跑到的所有站里油价最小的那个油站
index = i;
min_price = 1e18;
for(j = i + 1 ; node[j].length - node[i].length <= len && j <= n ; j++)
{
if(node[j].price < min_price)
{
min_price = node[j].price;
index = j;
}
}
sum += (capacity-cur_capacity)*node[i].price;
cur_capacity = capacity - (node[index].length - node[i].length )/unit_gas;
i = index;
}//else
}//for
}//else
if(!flag)
printf("%.2lf\n",sum);
}
return 0;
}

6
1

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：4229485次
• 积分：34196
• 等级：
• 排名：第137名
• 原创：254篇
• 转载：8篇
• 译文：0篇
• 评论：2659条
博客专栏
 考研复试上机题 文章：14篇 阅读：39475
 IT公司笔试题集锦 文章：18篇 阅读：567194
最新评论