Pku1160postoffice post office distribution issue report

Source: Internet
Author: User

Classic question. The most important thing is to use the distribution of each post office for dynamic planning, that is, to find the minimum value of each post office in all towns, and then calculate the second and third... Finally, get the answer ..

The question shows M villages and their distances, and n post offices. How can we create n post offices to minimize the cost. Idea: Use OPT [I] [J] to record the optimal solution for building the previous post office in the previous J villages, use cost [I] [J] to record the minimum cost of building a post office in villages I to J. Obviously, the post office should be set to the midpoint (very important ). Let the previous post office cover the first J villages. The I + 1 post office covers the J + 1 to J + k villages (J + k <= N ), the state transition equation is
OPT [I + 1] [J + k] = min {OPT [I] [J] + cost [J + 1] [J + k];} (K + j <= N)

The minimum cost for storing the cost array from I to J is a post office. Obviously, the post office should be placed in the middle. The code and result for constructing the cost are as follows:

For (I = 1; I <= m; I ++)

For (j = I; j <= m; j ++)

{

Cost [I] [J] = 0;

Mid = (I + J)/2;

For (k = I; k <= J; k ++)

Cost [I] [J] + = (distance [Mid]-distance [k])> = 0?

Distance [Mid]-distance [k]: distance [k]-distance [Mid];

}

OPT [I] [J] indicates the minimum cost for the previous post office to cover the previous J villages. for I = 1, OPT [I] [J] = cost [I] [J]. Let the first two post offices cover the first J villages, that is, I = 2, it may be the optimal solution of the following situation: the first post office covers the first village, the second village covers 2-J villages, or the first post office covers the 1-2 villages, the second village covers 3-J villages, the first post office covers 1-3 villages, and the second village covers 4-J villages. The code for this part is as follows:

For (I = 0; I <= N; I ++)

For (j = 0; j <= m; j ++)

If (OPT [I] [J] <3000000)

{

For (k = 1; j + k <= m; k ++)

{

If (OPT [I + 1] [J + k]> OPT [I] [J] + cost [J + 1] [J + k])

{

OPT [I + 1] [J + k] = OPT [I] [J] + cost [J + 1] [J + k]; // calculate the minimum value of each post office on J + 1 to J + K...

}

}

}

 

 

The final OPT [m] [N] is the result...

The code is
:

# Include <stdio. h>
# Define Maxi 30000000
Void main (){
Int I, n, J, M, K, mid;
Int OPT [35] [310], cost [310] [310], t [301];
Scanf ("% d", & N, & M );
For (I = 1; I <= N; I ++)
Scanf ("% d", & T [I]);
For (I = 1; I <= N; I ++)
For (j = I; j <= N; j ++ ){
Cost [I] [J] = 0;
Mid = (I + J)/2;
For (k = I; k <= mid; k ++)
Cost [I] [J] + = T [Mid]-T [k];
For (k = Mid + 1; k <= J; k ++)
Cost [I] [J] + = (T [k]-T [Mid]);
}
For (I = 0; I <= m; I ++)
For (j = 0; j <= N; j ++)
OPT [I] [J] = Maxi;
OPT [0] [0] = 0;
For (I = 0; I <= m; I ++)
For (j = 0; j <= N; j ++)
If (OPT [I] [J] <Maxi ){
For (k = 1; k + j <= N; k ++)
If (OPT [I + 1] [J + k]> OPT [I] [J] + cost [J + 1] [J + k])
OPT [I + 1] [J + k] = OPT [I] [J] + cost [J + 1] [J + k];
}
Printf ("% d/N", OPT [m] [N]);
} Original problem report link: http://blog.csdn.net/china8848/archive/2008/01/12/2039670.aspx

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.