vijos-p1456 minimum Total cost (state compression DP + memory Search)

Source: Internet
Author: User

P1456Minimum total cost accepted Tags: [show tags]<textarea id="code" class="textbox" style=""></textarea>Describe

n the individual is doing the passing item of the game, numbered 1-n.

The rules of the game are this: at the beginning the item can be on any person, and he can pass it on to any of the others; the next person can pass it on to anyone who has not received the item.

That is, items can only pass through the same person once, and each transfer process has a cost; there is no link between the value of different people passing to different people;
What is the total cost of the whole process when the item passes through all n individuals?

Format input Format

The first act n, which represents a total of n individuals (16>=n>=2);
The following is a matrix of n*n, section i+1, column J indicates the cost of passing the item from the person numbered I to the person numbered J, especially if the i+1 line, the I column 1 (because the item cannot be passed to itself), and the other data are positive integers (<=10000).

(for 50% of data, n<=11).

Output format

A number, which is the sum of the minimum costs.

Example 1 sample input 1[copy]
2-1 97942724–1
Sample output 1[Copy]
2724
Limit

All data time limit is 1s

Source

Jszx

This problem can be directly with the memory of the search to be more convenient, here is a temporary state compression DP;

Dp[0][i][j][k] for the selection of several people

DP[1][I][J][K] for the minimum cost

So:

Dp[1][i][j][k] = min (Dp[1][i][j][k], dp[1][i-1][f][j] + v[j][k]) {k does not belong to s{already selected}}.


Package Ds;import java.util.*;import java.math.*;import java.io.*;p ublic class Main {static Scanner cin = Null;static Pr  Intstream cout = null;static final int maxn = 20;static int INF = 0x3f3f3f3f;static int[][] V = new int[maxn][maxn];static Int[][][][] dp = new int[2][maxn][maxn][maxn];static int bit_count (int val) {//Several people have passed int ret = 0;while (val > 0) {V Al &= (VAL-1); ret++;} return ret;} public static void Main (string[] agrs) throws IOException {//System.setin (New FileInputStream (New File ("D:" + File.separa Tor +//"Imput.txt")), cin = new Scanner (system.in), cout = new PrintStream (System.out), while (Cin.hasnext ()) {int n = cin. Nextint (); for (int i = 1, i <= N; i++) {for (int j = 1; J <= N; j + +) {V[i][j] = Cin.nextint ();}} for (int i = 0, i < 2; i++) {for (int f = 1, f <= N; f++) {for (int j = 1; J <= N; j + +) {for (int k = 1; k <= N k++) {if (i = = 1) dp[i][f][j][k] = 0;elsedp[i][f][j][k] = inf;if (i = = 0 && F = = 0) Dp[i][f][j][k] = 0;}}} int Min = Inf;for (int i = 1; i < n; i++) {for (int j = 1; J <= N; j + +) {for (int k = 1; k <= N; k++) {if (j = = k) continue;f or (int f = 1; f <= N; f++) {if (f = = j) continue;if ((Dp[1][i-1][f][j] & (1 << k) > 0) continue;if (dp[0 ][I][J][K] >= Dp[0][i-1][f][j] + v[j][k]) {dp[0][i][j][k] = Dp[0][i-1][f][j] + v[j][k];DP [1][i][j][k] = dp[1][i-1 ][F][J] | (1 << k); if (i = = n-1&& Bit_count (dp[1][i][j][k]) = = n-1) {min = math.min (min, dp[0][i][j][k]);}}}}} if (min = = INF) cout.println ("0"); elsecout.println (min);} Cout.flush ();}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vijos-p1456 minimum Total cost (state compression DP + memory Search)

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.