Ultraviolet A 662-fast food

Source: Internet
Author: User

Question: There are n restaurants in a street. Now I want to build K warehouses. The storage cost is the distance from each restaurant to the nearest warehouse;

Minimum storage cost.

Analysis: DP, median, and dynamic planning.

Status: f (I, j) indicates the minimum storage cost for the first J restaurants to create I warehouses;

State Transfer: f (I, j) = min (f (I-1, k) + cost (k + 1, J) {where I-1 <k <j };

Among them, cost (k + 1, J) is the minimum unit price to build a warehouse between k + 1,... and J for this J-K restaurant;

At this time, it must be set up at the median position. Store the recursive reverse output of the path.

Note: What I wrote earlier was not the permanent middle digit. The cost is too high.

# 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 ); elseprintf ("depot % d at restaurant % d serves serving ants % 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]; // initialize 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] = 0 xffffff; 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 ", DP [k] [N]);} return 0 ;}
Test data:

200 1041 53 153 288 292 467 491 778 900 11501655 1842 1869 1999 2082 2306 2421 2995 3035 34303548 3602 3728 3788 3902 4031 4041 4596 4639 46644734 4827 4833 4966 5021 5097 5436 5447 5537 57055829 6270 6334 6359 6422 6483 6617 6729 6868 69007376 7711 8281 8723 8909 8942 9040 9161 9374 95149741 9758 9894 9930 9961 10291 10383 11020 11323 1133711478 11538 11840 11942 12052 12287 12316 12382 12623 1285913030 13290 13931 13966 13977 14310 14604 14771 14893 1494515006 15141 15350 15457 15573 15574 15724 15890 16118 1641316512 16541 16827 16941 16944 17035 17410 17421 17673 1780718007 18127 18467 18588 18636 18716 18756 18762 19072 1916919264 19629 19668 19718 19895 19912 19954 20037 20537 2153821548 21724 21726 22190 22355 22386 22483 22648 22704 2281322929 23199 23281 23655 23805 23811 23986 24021 24084 2422124350 24370 24393 24464 24484 24626 24648 24767 24946 2554725667 26299 26308 26418 26500 26777 26924 26962 27348 2735027446 27506 27529 27595 27624 27644 27753 27938 28145 2825328703 28745 29168 29358 29658 30106 30191 30333 30836 3110131107 31115 31322 31673 32209 32391 32439 32591 32662 32757Chain 1Depot 1 at restaurant 9 serves restaurants 1 to 17Depot 2 at restaurant 34 serves restaurants 18 to 50Depot 3 at restaurant 59 serves restaurants 51 to 67Depot 4 at restaurant 75 serves restaurants 68 to 82Depot 5 at restaurant 94 serves restaurants 83 to 106Depot 6 at restaurant 118 serves restaurants 107 to 129Depot 7 at restaurant 136 serves restaurants 130 to 143Depot 8 at restaurant 152 serves restaurants 144 to 161Depot 9 at restaurant 172 serves restaurants 162 to 183Depot 10 at restaurant 192 serves restaurants 184 to 200Total distance sum = 144490200 2041 53 153 288 292 467 491 778 900 11501655 1842 1869 1999 2082 2306 2421 2995 3035 34303548 3602 3728 3788 3902 4031 4041 4596 4639 46644734 4827 4833 4966 5021 5097 5436 5447 5537 57055829 6270 6334 6359 6422 6483 6617 6729 6868 69007376 7711 8281 8723 8909 8942 9040 9161 9374 95149741 9758 9894 9930 9961 10291 10383 11020 11323 1133711478 11538 11840 11942 12052 12287 12316 12382 12623 1285913030 13290 13931 13966 13977 14310 14604 14771 14893 1494515006 15141 15350 15457 15573 15574 15724 15890 16118 1641316512 16541 16827 16941 16944 17035 17410 17421 17673 1780718007 18127 18467 18588 18636 18716 18756 18762 19072 1916919264 19629 19668 19718 19895 19912 19954 20037 20537 2153821548 21724 21726 22190 22355 22386 22483 22648 22704 2281322929 23199 23281 23655 23805 23811 23986 24021 24084 2422124350 24370 24393 24464 24484 24626 24648 24767 24946 2554725667 26299 26308 26418 26500 26777 26924 26962 27348 2735027446 27506 27529 27595 27624 27644 27753 27938 28145 2825328703 28745 29168 29358 29658 30106 30191 30333 30836 3110131107 31115 31322 31673 32209 32391 32439 32591 32662 32757Chain 2Depot 1 at restaurant 5 serves restaurants 1 to 9Depot 2 at restaurant 13 serves restaurants 10 to 17Depot 3 at restaurant 22 serves restaurants 18 to 27Depot 4 at restaurant 34 serves restaurants 28 to 40Depot 5 at restaurant 46 serves restaurants 41 to 52Depot 6 at restaurant 56 serves restaurants 53 to 59Depot 7 at restaurant 63 serves restaurants 60 to 67Depot 8 at restaurant 74 serves restaurants 68 to 80Depot 9 at restaurant 84 serves restaurants 81 to 87Depot 10 at restaurant 93 serves restaurants 88 to 99Depot 11 at restaurant 105 serves restaurants 100 to 110Depot 12 at restaurant 116 serves restaurants 111 to 121Depot 13 at restaurant 125 serves restaurants 122 to 129Depot 14 at restaurant 136 serves restaurants 130 to 143Depot 15 at restaurant 151 serves restaurants 144 to 159Depot 16 at restaurant 164 serves restaurants 160 to 168Depot 17 at restaurant 174 serves restaurants 169 to 180Depot 18 at restaurant 184 serves restaurants 181 to 187Depot 19 at restaurant 191 serves restaurants 188 to 194Depot 20 at restaurant 197 serves restaurants 195 to 200Total distance sum = 64170200 3041 53 153 288 292 467 491 778 900 11501655 1842 1869 1999 2082 2306 2421 2995 3035 34303548 3602 3728 3788 3902 4031 4041 4596 4639 46644734 4827 4833 4966 5021 5097 5436 5447 5537 57055829 6270 6334 6359 6422 6483 6617 6729 6868 69007376 7711 8281 8723 8909 8942 9040 9161 9374 95149741 9758 9894 9930 9961 10291 10383 11020 11323 1133711478 11538 11840 11942 12052 12287 12316 12382 12623 1285913030 13290 13931 13966 13977 14310 14604 14771 14893 1494515006 15141 15350 15457 15573 15574 15724 15890 16118 1641316512 16541 16827 16941 16944 17035 17410 17421 17673 1780718007 18127 18467 18588 18636 18716 18756 18762 19072 1916919264 19629 19668 19718 19895 19912 19954 20037 20537 2153821548 21724 21726 22190 22355 22386 22483 22648 22704 2281322929 23199 23281 23655 23805 23811 23986 24021 24084 2422124350 24370 24393 24464 24484 24626 24648 24767 24946 2554725667 26299 26308 26418 26500 26777 26924 26962 27348 2735027446 27506 27529 27595 27624 27644 27753 27938 28145 2825328703 28745 29168 29358 29658 30106 30191 30333 30836 3110131107 31115 31322 31673 32209 32391 32439 32591 32662 32757Chain 3Depot 1 at restaurant 4 serves restaurants 1 to 7Depot 2 at restaurant 9 serves restaurants 8 to 10Depot 3 at restaurant 14 serves restaurants 11 to 17Depot 4 at restaurant 22 serves restaurants 18 to 27Depot 5 at restaurant 32 serves restaurants 28 to 36Depot 6 at restaurant 39 serves restaurants 37 to 41Depot 7 at restaurant 46 serves restaurants 42 to 50Depot 8 at restaurant 52 serves restaurants 51 to 53Depot 9 at restaurant 56 serves restaurants 54 to 59Depot 10 at restaurant 63 serves restaurants 60 to 67Depot 11 at restaurant 70 serves restaurants 68 to 72Depot 12 at restaurant 77 serves restaurants 73 to 81Depot 13 at restaurant 84 serves restaurants 82 to 86Depot 14 at restaurant 89 serves restaurants 87 to 92Depot 15 at restaurant 96 serves restaurants 93 to 99Depot 16 at restaurant 103 serves restaurants 100 to 106Depot 17 at restaurant 109 serves restaurants 107 to 112Depot 18 at restaurant 117 serves restaurants 113 to 121Depot 19 at restaurant 125 serves restaurants 122 to 129Depot 20 at restaurant 131 serves restaurants 130 to 133Depot 21 at restaurant 138 serves restaurants 134 to 142Depot 22 at restaurant 146 serves restaurants 143 to 149Depot 23 at restaurant 154 serves restaurants 150 to 159Depot 24 at restaurant 160 serves restaurants 160 to 161Depot 25 at restaurant 165 serves restaurants 162 to 168Depot 26 at restaurant 174 serves restaurants 169 to 179Depot 27 at restaurant 182 serves restaurants 180 to 184Depot 28 at restaurant 186 serves restaurants 185 to 188Depot 29 at restaurant 191 serves restaurants 189 to 194Depot 30 at restaurant 197 serves restaurants 195 to 200Total distance sum = 39563

Ultraviolet A 662-fast food

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.