Hdu4848 calculates the Shortest total time to reach each vertex (sum [d [I]).

Source: Internet
Author: User

At the beginning, it was a brute-force DFS + pruning operation. Later I referred to others' ideas:

First, find the shortest short circuit between each vertex (so that the pre-processed search can be used to determine whether to return a duplicate), and the key is the amputation: 1. Optimum pruning (as best as possible: the Current Status + the expected minimum time> min returns !), 2: feasibility of amputation: if the current status + predicted status is no longer feasible, return. (Consider whether it is continue or return !). And the location! The effect on the exit is generally better (not inside the next loop) (reason: if the status is followed by the status, the front will be DFS to a very deep level, so put it at the top, it is generally reasonable to return if it cannot be determined together .)

# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int N; int da [35]; int d [35]; int A [35] [35]; int maxd = 0; const int INF = 0x3f3f3f; int Minn = inf; int bit [31]; void DFS (int x, int lev, int sum, int Allstate) {If (sum + d [x] * (N-lev)> = Minn | D [x]> maxd) {return ;} if (Allstate = (bit [N]-1) {Minn = sum; return;} For (INT I = 2; I <= N; I ++) {If (Allstate & bit [I-1]) = 0 & D [x] + A [x] [I]> da [I]) return;} For (int I = 2; I <= N; I ++) {If (Allstate & bit [I-1]) = 0) {int F = d [I]; d [I] = d [x] + A [x] [I]; DFS (I, column + 1, sum + d [I], allstate | bit [I-1]); D [I] = f ;}return ;}void Init () {int TD = 0; DA [1] = 0x3f3f3f-1; for (INT I = 1; I <= N; I ++) d [I] = inf; d [1] = 0; maxd = 0; Minn = inf; for (INT I = 1; I <= N; I ++) // makes a mistake again! Enumerate excessive points first! For (Int J = 1; j <= N; j ++) for (int K = 1; k <= N; k ++) if (A [J] [I] + A [I] [k] <A [J] [k]) A [J] [k] = A [J] [I] + A [I] [k];} int main () {for (INT I = 0; I <31; I ++) bit [I] = 1 <I; while (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) scanf ("% d", & A [I] [J]); Init (); For (INT I = 2; I <= N; I ++) {scanf ("% d", & Da [I]); If (DA [I]> maxd) maxd = da [I];} DFS ); if (Minn! = Inf) printf ("% d \ n", Minn); else printf ("-1 \ n") ;}return 0 ;}


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.