HDU 3001 pressure DP

Source: Internet
Author: User

N cities and M roads, each of which has its own price. Each city can reach a maximum of two times, and the minimum cost and the starting point of any city can be reached.

Triplicate pressure stores the number of times each city passes in each State.


Transfer equation: DP [I + B [k] [k] = min (DP [I + B [k] [K], DP [I] [J] + dis [J] [k]);


# Include "stdio. H "# include" string. H "const int INF = 0x3f3f3f; int B [15], Mark [60010] [15], DP [60010] [15], DIS [15] [15]; int min (int A, int B) {if (a <B) return a; else return B;} int main () {int I, j, n, m, K, temp, ANS, flag; B [0] = 1; for (I = 1; I <= 10; I ++) B [I] = B [I-1] * 3; for (I = 0; I <B [10]; I ++) // records the passing status of each city in each State. {Temp = I; for (j = 0; j <10; j ++) {mark [I] [J] = TEMP % 3; temp/= 3 ;}} while (scanf ("% d", & N, & M )! = EOF) {memset (DIS, INF, sizeof (DIS); memset (DP, INF, sizeof (DP); While (M --) {scanf ("% d", & I, & J, & K); I --; j --; If (DIS [I] [J]> K) dis [I] [J] = dis [J] [I] = K;} for (I = 0; I <n; I ++) DP [B [I] [I] = 0; ans = inf; for (I = 0; I <B [N]; I ++) {flag = 1; for (j = 0; j <n; j ++) {If (MARK [I] [J] = 0) Flag = 0; if (DP [I] [J] = inf) continue; For (k = 0; k <n; k ++) if (K! = J & Mark [I] [k] <2 & dis [J] [k]! = Inf) DP [I + B [k] [k] = min (DP [I + B [k] [K], DP [I] [J] + dis [J] [k]);} If (flag = 1) For (j = 0; j <n; j ++) ans = min (ANS, DP [I] [J]);} If (ANS = inf) printf ("-1 \ n "); else printf ("% d \ n", ANS);} return 0 ;}


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