HDU 3001 tri-hexadecimal DP

Source: Internet
Author: User
Travelling

Time Limit: 6000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 3789 accepted submission (s): 1182

Problem descriptionafter coding so many days, Mr acmer wants to have a good rest. So traveling is the best choice! He has decided to visit n cities (he insists on seeing all the cities! And he does not mind which city being his start station because Superman can bring him to any city at first but only once .), and of course there are m roads here, following a stored as usual. but Mr acmer gets bored so easily that he doesn't want to visit a city more than twice! And he is so mean that he wants to minimize the total amount! He is lazy you see. so he turns to you for help. inputthere are several test cases, the first line is two intergers N (1 <= n <= 10) and M, which means he needs to visit n cities and there are m roads he can choose, then M lines follow, each line will include three intergers A, B and C (1 <=, B <= N), means there is a road between A and B and the cost is of course C. input to the end of file. outputoutput the minimum limit that he shoshould pay, or-1 if he can't find such a route. sample input2 1 1 2 100 3 2 2 40 2 3 50 3 3 1 2 3 3 3 3 4 2 3 10 sample output100 90 7 Source 2009 multi-university training contest 11-host hrbeu recommendgaojie is transferred from: http://blog.csdn.net/azheng51714/article/details/7773862

Question:

Acmer wants to play in N cities, telling us the travel expenses between cities, and requiring each city to go twice at most! What is the minimum cost ?!

Ideas:

The only change to a typical TSP problem is two times!

Solution:

The number of times that edge vertex J appears in vertex set I is expressed in a triplicate System (triplicate system) as the total number of times that edge vertex J appears in vertex set I [I] [J].

 

 

1 # include <iostream> 2 # include <cstring> 3 # include <cstdlib> 4 # include <cstdio> 5 # include <algorithm> 6 # include <cmath> 7 # include <queue> 8 # include <map> 9 # include <vector> 10 # include <set> 11 12 # define n 1005 13 # define M 100000 14 # define INF 1000000007 15 # define mod 1000000007 16 # define mod2 100000000 17 # define ll long 18 # define maxi (, b) (a)> (B )? (A): (B) 19 # define mini (a, B) (a) <(B )? (A): (B) 20 21 using namespace STD; 22 23 int N; 24 int m; 25 int tri [12] = {243,729,218, 6561, 19683,59049}; 26 int dig [59050] [11]; // Dig [State] [k_dig] What is the K-bit of State 27 int DP [59050] [11]; 28 int d [15] [15]; 29 int ans; 30 int flag; 31 32 void INI1 () 33 {34 int o, J, T; 35 For (O = 0; O <59049; O ++) {36 T = O; 37 for (j = 0; j <10; j ++) {38 dig [O] [J] = T % 3; 39 t/= 3; 40} 41} 42} 43 44 void INI () 45 {46 int A, B; 47 int C; 48 int I; 49 ans =-1; 50 memset (DP,-1, sizeof (DP); 51 memset (D, -1, sizeof (d); 52 while (M --) {53 scanf ("% d", & A, & B, & C ); 54 if (d [a] [B] =-1) {55 d [a] [B] = C; 56 d [B] [a] = C; 57} 58 else {59 d [a] [B] = min (d [a] [B], C ); 60 d [B] [a] = min (d [B] [a], c); 61} 62} 63 64 for (I = 1; I <= N; I ++) {65 DP [TRI [I] [I-1] = 0; 66} 67} 68 69 void solve () 70 {71 int o, J, Te, k; 72 for (O = 1; O <tri [n + 1]; O ++) {7 3 flag = 1; 74 for (j = 0; j <n; j ++) {75 if (DIG [O] [J] = 0) {76 flag = 0; continue; 77} 78 TE = o-tri [J + 1]; 79 for (k = 0; k <n; k ++) {80 If (DIG [Te] [k] = 0) continue; 81 If (d [k + 1] [J + 1] =-1) continue; 82 If (DP [Te] [k] =-1) continue; 83 If (DP [O] [J] =-1) {84 DP [O] [J] = DP [Te] [k] + d [k + 1] [J + 1]; 85} 86 else {87 DP [O] [J] = min (DP [O] [J], DP [Te] [k] + d [k + 1] [J + 1]); 88} 89} 90} 91 92 // printf ("O = % d flag = % d \ n", O, flag); 93 If (flag = 0) continue; 94 for (j = 0; j <n; j ++) {95 If (DP [O] [J] =-1) continue; 96 If (ANS =-1) {97 ans = DP [O] [J]; 98} 99 else {100 Ans = min (ANS, DP [O] [J]); 101} 102} 103 104} 105 106 107 void out () 108 {109 // For (int o = 0; O <tri [n + 1]; O ++) {110 // For (Int J = 0; j <n; j ++) printf ("O = % d J = % d dp = % d \ n", O, J, DP [O] [J]); 111 //} 112 printf ("% d \ n", ANS); 113} 114 115 int main () 116 {117 INI1 (); 118 // freopen ("data. in "," r ", stdin); 119 // fr Eopen ("data. out "," W ", stdout); 120 // scanf (" % d ", & T); 121 // For (int cnt = 1; CNT <= T; CNT ++) 122 // while (t --) 123 while (scanf ("% d", & N, & M )! = EOF) 124 {125 INI (); 126 solve (); 127 out (); 128} 129 return 0; 130}

 

HDU 3001 tri-hexadecimal 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.