P3959 treasure-like pressure DP

Source: Internet
Author: User

Before writing a copy of this question about simulated annealing, now to add a pressure DP method.

In fact, directly in the DFS-like pressure, and the implementation is very simple, but online some people say this method is wrong ... Do not know which is wrong, but do not write, found a positive solution.

The difference between the positive solutions is the state, (what does the tree height mean), and each time it is transferred from a subset of the current state. Here is a quick enumeration of the subset of operations, it is worth writing about.

The problem is dry:

description of the participation in the archaeological excavation Xiao Ming got a copy of the treasure map, the treasure map marked the nnn buried in the deep underground treasures house, also gave the NNN Treasure House between the development of M mm road and their length. Xiao Ming is determined to go to excavate the treasures in all the Treasure house. However, each treasure house is far from the ground, that is to say, it is difficult to get a path from the ground to a treasure house, while the road between the Treasure house is relatively easy to develop. Xiao Ming's determination touched the sponsor of the archaeological excavation, the sponsor decided to free the sponsorship he opened a passage from the ground to a treasure house, to which treasure house is decided by xiaoming. On this basis, Xiaoming also need to consider how to carve the road between the Treasure house. The road that has been dug out can be arbitrarily used without cost. Every time a new road is dug, Xiaoming will work with the archaeological team to dig out the treasures of the Treasure House that the road can reach. In addition, Xiao Ming does not want to develop useless roads, that is, two have been excavated the road between the Treasure House no longer need to develop. The cost of developing a new road is: Lxk\mathrm{l} \times \mathrm{k}lxkl represents the length of the road, and K represents the Treasure house that the sponsor helped you through to the Treasure house at the beginning of the road. The number of treasure houses (including the Treasure House that the sponsor helped you through and the Treasure house at the beginning of the road). Please write a program for xiaoming to select the Treasure House opened by the sponsors and the subsequent excavation of the road, so that the total cost of the project is minimal, and output this minimum value. Input and output format input format: The first line two a positive integer separated by a space n,mn,mn,m, representing the number of treasure house and the number of roads. The next MMM line, each line of three positive integers separated by a space, is the number of two treasure houses connected by a road (numbered1−n1-n1−n), and the length of the road VVV. Output format: A positive integer representing the minimum total cost. Input and Output sample input example #1: Copy4 5 1 2 1 1 3 3 1 4 1 2 3 4 3 4 1Output Example #1: Copy4Input Sample #2: Copy4 5 1 2 1 1 3 3 1 4 1 2 3 4 3 4 2Output Example #2: Copy5description "Sample explanation 1" Xiao Ming selected to let the sponsor through 1 OneTreasure House. Xiao Ming developed the road1→ +\to +→2, Digging up222Treasure of the number. Developed the road1→ A\to A→4, Digging up444Treasure of the number. Also developed the road4→ the\to the→3, digging up 33Treasure No. 3rd. The total cost of the project is:1X1+1X1+1X2= A\times1+1\times1+1\times2=4 1X1+1X1+1X2=4"Sample explanation 2" Xiao Ming selected to let the sponsor through 1 OneTreasure House. Xiao Ming developed the road1→ +\to +→2, Digging up222Treasure of the number. Developed the road1→ to\to to→3, Digging up333Treasure of the number. Also developed the road1→ A\to A→4, digging up 44Treasure No. 4th. The total cost of the project is:1X1+3X1+1X1=Wuyi\times1+3\times1+1\times1=WuyiX1+3X1+1X1=5"Data size and Convention"% -\% -% of data: guaranteed input is a tree,1≤n≤Bayi\le N \leBayi≤n≤8, v≤5000v \le 5000v≤ theand all of the VV v are equal. For +% +\% +% of data:1≤n≤Bayi\le N \leBayi≤n≤8,0≤m≤10000\le m \le10000≤m≤ +, v≤5000v \le 5000v≤ theand all v v v are equal. For% -\% -% of data:1≤n≤Bayi\le N \leBayi≤n≤8,0≤m≤10000\le m \le10000≤m≤ +, v≤5000v \le 5000v≤ thefor% -\% -% of data:1≤n≤121\le N \le121≤n≤ A,0≤m≤10000\le m \le10000≤m≤ +, v≤500000v \le 500000v≤500000

Code:

#include <iostream>#include<cstdio>#include<cmath>#include<ctime>#include<queue>#include<algorithm>#include<cstring>using namespacestd;#defineDuke (I,a,n) for (int i = a;i <= n;i++)#defineLV (i,a,n) for (int i = a;i >= n;i--)#defineClean (a) memset (A,0,sizeof (a))Const intINF =0x3f3f3f3f;Const intMoD = 1e9 +7; typedefLong LongLl;typedefDoubledb;template<classT>voidRead (T &x) {    CharC; BOOLOP =0;  while(c = GetChar (), C <'0'|| C >'9')        if(c = ='-') op =1; X= C-'0';  while(c = GetChar (), C >='0'&& C <='9') x= x *Ten+ C-'0'; if(OP) x =-x;} Template<classT>voidwrite (T x) {if(X <0) Putchar ('-'), x =-x; if(x >=Ten) Write (X/Ten); Putchar ('0'+ x%Ten);}Const intMAXN = the;Const intMAXM =1010;Const intMaxt =1<<MAXN;intn,m,a,b,c,ans=INF;intFROG[MAXT][MAXN],GORF[MAXT],DIS[MAXN][MAXN];intMain () {read (n);    Read (m); memset (DIS,0x3f,sizeof(DIS)); Duke (I,1, M) {        intx, Y, Z        Read (x); Read (y); read (z); X--;y--; Dis[x][y]= Dis[y][x] =min (dis[x][y],z); } memset (Frog,0x3f,sizeof(frog)); Duke (I,1,(1<< N)-1) {Duke (J,0N1)        {            if(((1<< j) | i) = =i) {dis[j][j]=0; Duke (K,0N1)                {                    if(Dis[j][k]! =INF) {Gorf[i]|= (1<<k); }}}}} duke (I,0N1) frog[1<< i][0] =0; Duke (I,2,(1<< N)-1)    {         for(intS0 = i-1; S0; S0 = (S0-1) &i) {if((Gorf[s0] | i) = =Gorf[s0]) {                intsum =0; intSS = S0 ^i; Duke (K,0N1)                {                    if((1<< k) &SS) {                        inttemp =INF; Duke (H,0N1)                        {                            if((1<< h) &s0) Temp=min (temp,dis[h][k]); } Sum+=temp; }} Duke (J,1N1)                if(Frog[s0][j-1] !=INF) {Frog[i][j]= Min (Frog[i][j],frog[s0][j-1] + sum *j); }            }        }    }    intAns =INF; Duke (I,0N1) {ans= Min (ans,frog[(1<< N)-1][i]); } printf ("%d\n", ans); return 0;}

P3959 treasure-like pressure DP

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.