Gas Station

There are N gas stations along a circular route, where the amount of gas at station i is gas[i].

You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.

Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.

Note:

The solution is guaranteed to be unique.

看到题目第一反应是贪心算法;所以想也没想就直接枚举每一种情况,从0开始,看是不是可以走完,这样发现其实根本就是暴力枚举,没有任何的算法可言。然后仔细分析,发现其实很多地方是可以优化的,定义diff[i] = gas[i] - cost[i];如果diff数组里面的每个元素和小于0的话,那么必然是没有可以到达的方式,直接返回-1;然后是从0开始,依次计算是否可能,如果发现到达j位置走不下去了,那么可能的出发点start的范围应该是 j <= start <= gas.size(),所以下次需找的点应该是从j继续往后走,这样可以将算法马上从0(N^2)的复杂度降低到0(N)的复杂度,只要遍历一遍diff数据,就可以找到可能的起始出发位置。所以看到题目还是得注意多分析,尽可能的优化时间复杂度。

int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int lenG = gas.size();
        int lenC = cost.size();
        if(lenG == 0 || lenC == 0 || (lenG != lenC))
        {
            return -1;
        }
        
        int sum = 0;
        vector<int> diff;
        int tmp = 0;
        for(int i = 0; i < lenG; ++i)
        {
            tmp = gas[i] - cost[i];
            diff.push_back(tmp);
            sum += tmp;
        }
        
        if(sum < 0)
            return -1;
        int index = 0;
        for(int i = 0; i < lenG; ++i)
        {
            int tmpSum = diff[i];
            if(tmpSum < 0)
                continue;
            int flag = 1;
            for(int j = i + 1; j < lenG; ++j)
            {
                tmpSum += diff[j];
                if(tmpSum < 0)
                {
                    flag = 0;
                    i = j;
                    break;
                }
            }
            
            if(flag)
            {
                index = i;
                break;
            }
        }
        
        return index;
    }


  • 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、付费专栏及课程。

余额充值