187. Gas Station

题目链接:https://www.lintcode.com/problem/gas-station/description

在一条环路上有 N 个加油站,其中第 i 个加油站有汽油gas[i],并且从第_i_个加油站前往第_i_+1个加油站需要消耗汽油cost[i]。
你有一辆油箱容量无限大的汽车,现在要从某一个加油站出发绕环路一周,一开始油箱为空。
求可环绕环路一周时出发的加油站的编号,若不存在环绕一周的方案,则返回-1。

Example
样例 1:

输入:gas[i]=[1,1,3,1],cost[i]=[2,2,1,1]
输出:2
样例 2:

输入:gas[i]=[1,1,3,1],cost[i]=[2,2,10,1]
输出:-1
Challenge
O(n)时间和O(1)额外空间

Notice
数据保证答案唯一。

 

思路1:两个循环遍历,外循环gas,内循环从当前编号绕一周。

思路2: 题目说数据保证答案唯一,也就是要么有且仅有一个这样的加油站,要么没有,先看没有的情况,没有的话则说明,走一圈下来,加油站能加的油比消耗的油要少,这种情况下就没有答案。
那么遍历两个数组,先假设这个加油站是0号,然后随着遍历计算累计值,一旦累计值小于0,说明这之前的加油站都是不满足条件的,把开始的设置为遍历的下一个,然后继续这个过程,直到遍历结束。同时计算所有的gas和cost的和,如果sum(gas)<sum(cost),则不可能存在这样的一个加油站。 时间复杂度为O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值