UVA 662 Fast Food

Source: Internet
Author: User

Original question:
The Fastfood chain Mcburger owns several restaurants along a highway. Recently, they has decided to build several depots along the highway, each one located at a restaurent and supplying Seve Ral of the restaurants with the needed ingredients. Naturally, these depots should be placed so, the average distance between a restaurant and its assigned depot is Minim Ized. You is to write a program that computes the optimal positions and assignments of the depots. To do this more precise, the management of Mcburger have issued the following specification:you would be given the Positi ONS of n restaurants along the highway as n integers d 1 < D 2 < < D N (these is the distances measured from the company's headquarter, which happens to BES at the same
Highway). Furthermore, a number K (k≤n) would be given, and the number of depots to be built. The k depots is built at the locations of K different restaurants. Each restaurant'll is assigned to the closest depot and from which it'll then receive its supplies. To minimize shipping costs, the total distance sum, defined as N∑i=1 | D i− (position of depot serving restaurant i) | Must be as small as possible. Write A program this computes the positions of the K depots, such the total distance sum is minimized.
Input
The input file contains several descriptions of fastfood chains. Each description starts with a line containing the integers n and K. N and K would satisfy 1≤n≤200, 1≤k≤30, k≤ N. Following this would n lines containing one integer each, giving the positions D I of the restaurants, ordered Increasi ngly. The input file would end with a case starting with n = k = 0. This case is should not being processed.
Output
For each chain, first output the number of the chain. Then output an optimal placement of the depots as Follows:for, depot output a line containing its position and the RA Nge of Restaurants it serves. If there is more than one optimal solution, and output any of them. After the depot descriptions output a line containing the total distance sum, as defined in the problem text. Output a blank line after each test case.
Sample Input
6 3
5
6
12
19
20
27
0 0
Sample Output
Chain 1
Depot 1 at Restaurant 2 serves restaurants 1 to 3
Depot 2 at Restaurant 4 serves restaurants 4 to 5
Depot 3 at Restaurant 6 serves restaurant 6
Total Distance sum = 8

English:
give you two numbers n and K, respectively, that there are n stores, to be in the K store inventory. And then give you the number of n, indicating the coordinates of the n stores on a one-dimensional axis. Now ask you in this n store choose which K shop when warehouse can make this K store to the surrounding shops to transport goods in the shortest distance. Distance is expressed in absolute value.

#include <bits/stdc++.h> using namespace std;
int mark[201][201];
int dp[50][210];
int pos[210];
int x[201][201],y[201][201];

int n,k;
    int Get_mid_dis (int a[],int s,int e) {if (s==e) return 0;
    int mid= (e+s+1)/2;
    int tot=0;
    for (int i=s;i<=e;i++) tot+=abs (A[mid]-a[i]);
return tot;
    } void Ini () {memset (dp,0,sizeof (DP));
    memset (Mark,0,sizeof (Mark));
    memset (x,0,sizeof (x));
    memset (y,0,sizeof (y));
    for (int. i=1;i<=n;i++) {for (int j=i+1;j<=n;j++) Mark[i][j]=get_mid_dis (POS,I,J);
    }} int main () {Ios::sync_with_stdio (false);
    int t=1;

        while (Cin>>n>>k,n+k) {for (int i=1;i<=n;i++) cin>>pos[i];
        INI ();

        for (int i=2;i<=n;i++) dp[1][i]=mark[1][i];
                for (int j=2;j<=k;j++) {for (int i=j;i<=n;i++) {int tmp=int_max;
for (int t=j-1;t<i;t++)                {if (dp[j-1][t]+mark[t+1][i]<tmp) {tmp
                        =dp[j-1][t]+mark[t+1][i];
                        x[j][i]=t+1;
                    Y[j][i]=i;
            }} dp[j][i]=tmp;
        }} int i=n,j=k;
        Vector<int> Rx,ry;
                while (true) {if (j==1) {rx.push_back (j);
                Ry.push_back (i);
            Break
            } rx.push_back (X[j][i]);
            Ry.push_back (Y[j][i]);
            I=x[j][i]-1;
        j--;
        } cout<< "Chain" <<T++<<endl;
        I=1; for (auto Ritx=rx.rbegin (), Rity=ry.rbegin (); Ritx!=rx.rend (); ritx++,rity++,i++) if (*ritx!=*rity) cout&lt ;< "Depot" <<i<< "at Restaurant" << (*ritx+*rity)/2<< "serves restaurants" <<*ritx< < "to" <<*rity<&Lt;endl; else cout<< "Depot" <<i<< "at Restaurant" << (*ritx+*rity)/2<< "serves restaurant
        "<<*ritx<<endl;
    cout<< "Total distance sum =" <<dp[k][n]<<endl<<endl;
} return 0;
 }

Ideas:

A very good dynamic planning problem, better think. The state transition equation is
Dp[j][i]=min (Dp[j-1][k]+dis (K+1,J))

wherein Dis (I,J) represents the first store to the J store to set a warehouse supply distance, warehouse location is the median, and then accumulate I to J store coordinates to the sum of the median.
Dp[j][i] Indicates the minimum distance to set J warehouses in the first I store
Then the significance of the transfer equation is the first I store set J warehouse of the distance is equal to the former K store set J-1 Warehouse, and then add the new addition of the first J warehouse after the shortest distance.

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.