ACM学习-动态规划-巡回演出问题

来源:互联网 发布:指纹考勤机数据修改 编辑:程序博客网 时间:2024/05/16 11:35
// ACM学习-动态规划-巡回演出问题.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include<iostream>
using namespace std;


int n=3;
int data[100][100] = {
{130,150},
{75,0,80},
{120,110,0,100,110,120,0},
{60,70,60,50},
{0,135,140},
{70,80},
};
int circle[100] = {
    2,3,7,4,3,2
};
int mincost[100][100][50] = { -1 };
int k=6;//要飞的航班数
int muti;
int sum;
int first=0;
int last=2;
//返回start---end 走steps步需要的最少花费
int get(int start,int end,int steps){
if (mincost[start][end][steps] > -1)return mincost[start][end][steps];
int result=10000;
//int count = 0;
if (steps == 1){
int temp;
if (end>start)
temp = data[start*muti + end - 1][steps-1];
else
temp = data[start*muti + end][steps - 1];
if (temp != 0){
//cout << "         temp=" << temp << endl;
return temp;
}
else{
return 10000;
}
}
for (int i = 0; i < n; i++){
if (i != end){
int temp;
if (end>i)
temp = data[i*muti + end - 1][steps-1];
else
temp = data[i*muti + end][steps - 1];
if (temp != 0){
if (steps == 2 && end != start&&i==first){
continue;
}
//count++;
int cost = get(start, i,steps-1) + temp;
if (cost < result&&cost<10000)
result = cost;
//cout << "cost=" << cost << endl;
}
}
}
if (result < 10000)mincost[start][end][steps] = result;
return result;
}
int _tmain(int argc, _TCHAR* argv[])
{
memset(mincost, 0xff, sizeof(mincost));
muti = n - 1;
    sum = n*(n - 1);
for (int i = 0; i < sum; i++){
for (int j = circle[i]; j < k; j++){
data[i][j] = data[i][j%circle[i]];
}
}
for (int i = 0; i < sum; i++){
for (int j = 0; j < k; j++){
cout << data[i][j] << ends;
}
cout << endl;
}
cout << get(0, 2, 6) << endl;
return 0;
}

0 0
原创粉丝点击