Gas Station

问题描述:
有N个油气站构成环形路径,第i个油气站有gas[i]个单位的油,一辆容量无限的油罐车从第i个油气站到第i+1个油气站需要消耗cost[i]个单位油. 如果这辆车能够从任意一站出发依靠油气站补给能够回到原来的地方.求这个起点, 如果不存在输出-1.
.图文无关
图片网址

分析:

假设从站start出发,到第n个油气站,记 sum=ni=startgas[i]cost[i] , 当第一次sum < 0 的时候,这意味着以start到i所有站为起点都不会通过第i个站,因为这只会让到达此处的和更小
这意味着可以一下排除 i - start +1 个节点不可行, 那么更新 start = i + 1, 继续从新的start排除节点
如果从start 出发能够回到start, 那么这个站就是要求的了.
如果排除所有站,说明不存在这样的站点.

这个思路过程类比序列连续最大和问题

代码:

/* 循环里面i 与 j 同步, i负责探测路径, j负责检测是否已经排除所有节点 */
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
       int  sum  = 0 ;
       int start = 0;
       int j =0 ;
       for(int  i =0 ;start < gas.size() ; i =(i+1)%gas.size(), ++j)
       {
           sum += gas[i] - cost[i];
           if(ss < 0)
           {
              sum= 0 ;
              start = j+1;
           }else
             if((i+1)%gas.size()== start)return start;
       }
       return -1;
    }
};
  • 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、付费专栏及课程。

余额充值