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

来源:互联网 发布:网络嗅探器绿色版 编辑:程序博客网 时间:2024/04/29 14:30

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,

 UDCStation1199Station2919Station3991The minimum cost is:1+1+1=3.


#include <iostream>#include <math.h>using namespace std;#define N 3int 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 0