Summer Training 1 e question paid roads (poj 3411) solution report

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3411

N cities are connected by M. If a city is connected to cityia and cityb, if you have already passed cityc (may be equal to a), you can pay P. Otherwise, you must pay the r toll if you have not passed cityc before.

Note that a point can be repeated multiple times, that is, it may constitute a ring. Although the road is long, the road cost is low. This problem should be taken into account. There is also pruning: if the current toll is larger than the previous answer, it will be traced back.

Mincost is clearly a global variable, and it was declared again in main with multiple hands. After more than an hour, I found that = ....

1 # include <iostream> 2 # include <cstdio> 3 # include <cstdlib> 4 # include <cstring> 5 using namespace STD; 6 7 const int maxn = 1000 + 5; 8 int head [maxn]; 9 int point_vis [maxn]; 10 int n, m, mincost; 11 12 struct adjlist // neighbor list 13 {14 int B, C, p, r; 15 int next; 16} node [maxn]; 17 18 void DFS (INT next, int cost) 19 {20 if (point_vis [next]> N | cost> = mincost) // If a point is passed more than N, or if the current cost is greater than the minsum obtained previously, it will be traced back. 21 return; 22 if (next = N) 23 {24 mincost = cost; 25 return; 26} 27 for (INT I = head [next]; I! =-1; I = node [I]. next) 28 {29 int v = node [I]. b; 30 point_vis [node [I]. b] ++; 31 if (point_vis [node [I]. c]> = 1) // if you have accessed this intermediate point C, you can pay the fee of p32 DFS (node [I]. b, cost + node [I]. p); 33 else // previously this point has been taken over, and you can only pay the r fee 34 DFS (node [I]. b, cost + node [I]. r); 35 point_vis [node [I]. b] --; 36} 37} 38 39 int main () 40 {41 while (scanf ("% d", & N, & M )! = EOF) 42 {43 int T1, CNT = 0; 44 memset (Head,-1, sizeof (head); 45 for (INT I = 0; I <m; I ++) 46 {47 scanf ("% d", & T1); 48 node [CNT]. next = head [T1]; 49 scanf ("% d", & node [CNT]. b, & node [CNT]. c, & node [CNT]. p, & node [CNT]. r); 50 head [T1] = CNT ++; 51} 52 memset (point_vis, 0, sizeof (point_vis); 53 mincost = maxn; 54 point_vis [1] ++; 55 DFS (1, 0); 56 If (mincost = maxn) 57 printf ("impossible \ n "); 58 else59 printf ("% d \ n", mincost); 60} 61 Return 0; 62}

 

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.