首页 > 代码库 > UVa 662 - Fast Food

UVa 662 - Fast Food

题目:一条街道上有n家餐馆,现在想建立k个仓库,储存代价是每个餐馆到最近的仓库的距离和;

            求最小的储存代价。

分析:dp,中位数,动态规划。

            状态:f(i,j)表示前j家餐馆建立i个仓库的最小储存代价; 

            状态转移:f(i,j)= min(f(i-1,k)+ cost(k+1,j)){ 其中 i-1 < k < j };

            其中cost(k+1,j)是在k+1,..,到j间建立一个仓库供这j-k个餐馆使用的最小单价;

            此时,必然建立在中位数位置;存储路径递归逆序输出即可。

说明:之前写的不是永中位数求的,代价太大。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

int dist[201];
int father[31][201];
int dp[31][201];
int cost[201][201] = {0};

void output(int k, int n)
{
	if (k) {
		int up = father[k][n]+1,mid = (up+n)/2;
		output(k-1, father[k][n]);
		if (up == n)
			printf("Depot %d at restaurant %d serves restaurant %d\n",k,mid,n);
		else
			printf("Depot %d at restaurant %d serves restaurants %d to %d\n",k,mid,up,n);
	}
}

int main()
{
	int N,K,mid,Count = 1;
	while (cin >> N >> K && N) {
		for (int i = 1 ; i <= N ; ++ i)
			cin >> dist[i];
		//初始化 
		for (int i = 1 ; i <= N ; ++ i)
		for (int j = i ; j <= N ; ++ j) {
			cost[i][j] = 0;
			mid = (j+i)/2;
			for (int k = i ; k <= j ; ++ k)
				cost[i][j] += abs(dist[k]-dist[mid]);
		}
		for (int i = 1 ; i <= N ; ++ i)
			dp[1][i] = cost[1][i];
		
		for (int i = 2 ; i <= K ; ++ i)
		for (int j = i ; j <= N ; ++ j) {
			dp[i][j] = 0xffffff;
			for (int k = i-1 ; k < j ; ++ k) {
				if (dp[i][j] > dp[i-1][k] + cost[k+1][j]) {
					dp[i][j] = dp[i-1][k] + cost[k+1][j];
					father[i][j] = k;
				}
			}
		}
		
		printf("Chain %d\n",Count ++);
		output(K, N);
		printf("Total distance sum = %d\n\n",dp[K][N]);
	}
	return 0;
}
测试数据:

200 10
41 53 153 288 292 467 491 778 900 1150
1655 1842 1869 1999 2082 2306 2421 2995 3035 3430
3548 3602 3728 3788 3902 4031 4041 4596 4639 4664
4734 4827 4833 4966 5021 5097 5436 5447 5537 5705
5829 6270 6334 6359 6422 6483 6617 6729 6868 6900
7376 7711 8281 8723 8909 8942 9040 9161 9374 9514
9741 9758 9894 9930 9961 10291 10383 11020 11323 11337
11478 11538 11840 11942 12052 12287 12316 12382 12623 12859
13030 13290 13931 13966 13977 14310 14604 14771 14893 14945
15006 15141 15350 15457 15573 15574 15724 15890 16118 16413
16512 16541 16827 16941 16944 17035 17410 17421 17673 17807
18007 18127 18467 18588 18636 18716 18756 18762 19072 19169
19264 19629 19668 19718 19895 19912 19954 20037 20537 21538
21548 21724 21726 22190 22355 22386 22483 22648 22704 22813
22929 23199 23281 23655 23805 23811 23986 24021 24084 24221
24350 24370 24393 24464 24484 24626 24648 24767 24946 25547
25667 26299 26308 26418 26500 26777 26924 26962 27348 27350
27446 27506 27529 27595 27624 27644 27753 27938 28145 28253
28703 28745 29168 29358 29658 30106 30191 30333 30836 31101
31107 31115 31322 31673 32209 32391 32439 32591 32662 32757

Chain 1
Depot 1 at restaurant 9 serves restaurants 1 to 17
Depot 2 at restaurant 34 serves restaurants 18 to 50
Depot 3 at restaurant 59 serves restaurants 51 to 67
Depot 4 at restaurant 75 serves restaurants 68 to 82
Depot 5 at restaurant 94 serves restaurants 83 to 106
Depot 6 at restaurant 118 serves restaurants 107 to 129
Depot 7 at restaurant 136 serves restaurants 130 to 143
Depot 8 at restaurant 152 serves restaurants 144 to 161
Depot 9 at restaurant 172 serves restaurants 162 to 183
Depot 10 at restaurant 192 serves restaurants 184 to 200
Total distance sum = 144490


200 20
41 53 153 288 292 467 491 778 900 1150
1655 1842 1869 1999 2082 2306 2421 2995 3035 3430
3548 3602 3728 3788 3902 4031 4041 4596 4639 4664
4734 4827 4833 4966 5021 5097 5436 5447 5537 5705
5829 6270 6334 6359 6422 6483 6617 6729 6868 6900
7376 7711 8281 8723 8909 8942 9040 9161 9374 9514
9741 9758 9894 9930 9961 10291 10383 11020 11323 11337
11478 11538 11840 11942 12052 12287 12316 12382 12623 12859
13030 13290 13931 13966 13977 14310 14604 14771 14893 14945
15006 15141 15350 15457 15573 15574 15724 15890 16118 16413
16512 16541 16827 16941 16944 17035 17410 17421 17673 17807
18007 18127 18467 18588 18636 18716 18756 18762 19072 19169
19264 19629 19668 19718 19895 19912 19954 20037 20537 21538
21548 21724 21726 22190 22355 22386 22483 22648 22704 22813
22929 23199 23281 23655 23805 23811 23986 24021 24084 24221
24350 24370 24393 24464 24484 24626 24648 24767 24946 25547
25667 26299 26308 26418 26500 26777 26924 26962 27348 27350
27446 27506 27529 27595 27624 27644 27753 27938 28145 28253
28703 28745 29168 29358 29658 30106 30191 30333 30836 31101
31107 31115 31322 31673 32209 32391 32439 32591 32662 32757

Chain 2
Depot 1 at restaurant 5 serves restaurants 1 to 9
Depot 2 at restaurant 13 serves restaurants 10 to 17
Depot 3 at restaurant 22 serves restaurants 18 to 27
Depot 4 at restaurant 34 serves restaurants 28 to 40
Depot 5 at restaurant 46 serves restaurants 41 to 52
Depot 6 at restaurant 56 serves restaurants 53 to 59
Depot 7 at restaurant 63 serves restaurants 60 to 67
Depot 8 at restaurant 74 serves restaurants 68 to 80
Depot 9 at restaurant 84 serves restaurants 81 to 87
Depot 10 at restaurant 93 serves restaurants 88 to 99
Depot 11 at restaurant 105 serves restaurants 100 to 110
Depot 12 at restaurant 116 serves restaurants 111 to 121
Depot 13 at restaurant 125 serves restaurants 122 to 129
Depot 14 at restaurant 136 serves restaurants 130 to 143
Depot 15 at restaurant 151 serves restaurants 144 to 159
Depot 16 at restaurant 164 serves restaurants 160 to 168
Depot 17 at restaurant 174 serves restaurants 169 to 180
Depot 18 at restaurant 184 serves restaurants 181 to 187
Depot 19 at restaurant 191 serves restaurants 188 to 194
Depot 20 at restaurant 197 serves restaurants 195 to 200
Total distance sum = 64170


200 30
41 53 153 288 292 467 491 778 900 1150
1655 1842 1869 1999 2082 2306 2421 2995 3035 3430
3548 3602 3728 3788 3902 4031 4041 4596 4639 4664
4734 4827 4833 4966 5021 5097 5436 5447 5537 5705
5829 6270 6334 6359 6422 6483 6617 6729 6868 6900
7376 7711 8281 8723 8909 8942 9040 9161 9374 9514
9741 9758 9894 9930 9961 10291 10383 11020 11323 11337
11478 11538 11840 11942 12052 12287 12316 12382 12623 12859
13030 13290 13931 13966 13977 14310 14604 14771 14893 14945
15006 15141 15350 15457 15573 15574 15724 15890 16118 16413
16512 16541 16827 16941 16944 17035 17410 17421 17673 17807
18007 18127 18467 18588 18636 18716 18756 18762 19072 19169
19264 19629 19668 19718 19895 19912 19954 20037 20537 21538
21548 21724 21726 22190 22355 22386 22483 22648 22704 22813
22929 23199 23281 23655 23805 23811 23986 24021 24084 24221
24350 24370 24393 24464 24484 24626 24648 24767 24946 25547
25667 26299 26308 26418 26500 26777 26924 26962 27348 27350
27446 27506 27529 27595 27624 27644 27753 27938 28145 28253
28703 28745 29168 29358 29658 30106 30191 30333 30836 31101
31107 31115 31322 31673 32209 32391 32439 32591 32662 32757

Chain 3
Depot 1 at restaurant 4 serves restaurants 1 to 7
Depot 2 at restaurant 9 serves restaurants 8 to 10
Depot 3 at restaurant 14 serves restaurants 11 to 17
Depot 4 at restaurant 22 serves restaurants 18 to 27
Depot 5 at restaurant 32 serves restaurants 28 to 36
Depot 6 at restaurant 39 serves restaurants 37 to 41
Depot 7 at restaurant 46 serves restaurants 42 to 50
Depot 8 at restaurant 52 serves restaurants 51 to 53
Depot 9 at restaurant 56 serves restaurants 54 to 59
Depot 10 at restaurant 63 serves restaurants 60 to 67
Depot 11 at restaurant 70 serves restaurants 68 to 72
Depot 12 at restaurant 77 serves restaurants 73 to 81
Depot 13 at restaurant 84 serves restaurants 82 to 86
Depot 14 at restaurant 89 serves restaurants 87 to 92
Depot 15 at restaurant 96 serves restaurants 93 to 99
Depot 16 at restaurant 103 serves restaurants 100 to 106
Depot 17 at restaurant 109 serves restaurants 107 to 112
Depot 18 at restaurant 117 serves restaurants 113 to 121
Depot 19 at restaurant 125 serves restaurants 122 to 129
Depot 20 at restaurant 131 serves restaurants 130 to 133
Depot 21 at restaurant 138 serves restaurants 134 to 142
Depot 22 at restaurant 146 serves restaurants 143 to 149
Depot 23 at restaurant 154 serves restaurants 150 to 159
Depot 24 at restaurant 160 serves restaurants 160 to 161
Depot 25 at restaurant 165 serves restaurants 162 to 168
Depot 26 at restaurant 174 serves restaurants 169 to 179
Depot 27 at restaurant 182 serves restaurants 180 to 184
Depot 28 at restaurant 186 serves restaurants 185 to 188
Depot 29 at restaurant 191 serves restaurants 189 to 194
Depot 30 at restaurant 197 serves restaurants 195 to 200
Total distance sum = 39563

UVa 662 - Fast Food