算法题:地铁站建站最小花费

There are total N stations in a Metro Line.Those stations can be divided into three types: U(ground),D(underground) and C(composite).The construction cost of each station differs throughout the Metro Line. It should be noted that the adjacent station type can not be repeated. Given the construction costs of  three types for each station,you are required to find out the minimum cost of constructing this Metro Line.For example,

 UDC
Station1199
Station2919
Station3991
The minimum cost is:1+1+1=3.


#include <iostream>
#include <math.h>
using namespace std;
#define N 3
int minCost(int i,int j,int *p)
{
  int result=*(p+i*3+j);
  if(i<N-1)
  {
    result+=min(minCost(i+1,(j+1)%3,p),
                minCost(i+1,(j+2)%3,p));
  }
  return result;
}

int main()
{
  int stationCost[N*3]={1,9,9,9,1,9,9,9,1};
  int a=minCost(0,0,stationCost);
  int b=minCost(0,1,stationCost);
  int c=minCost(0,2,stationCost);
  int result=min(min(a,b),c);
  cout<<result<<endl;
  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值