Multiple access nodes for DFS POJ 3411 Paid Roads

Source: Internet
Author: User

POJ 3411 Paid Roads
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6553 Accepted: 2430

Description

A Network of m roads connects N cities (numbered from 1 to N). There may is more than one road connecting one city with another. Some of the roads are paid. There is ways to pay for travel on a paid road I from city ai to city bi:

    • In advance, in-a city CI (which may or May is not being the same as Ai);
    • After the travel, in the city bi.

The payment is Pi in the first case and Ri in the second case.

Write a program to find a minimal-cost route from the City 1 to the city N.

Input

The first line of the input contains the values of N and M. Each of the following m lines describes one road by specifying the values of ai, bi, ci, Pi, Ri (1≤ i m). Adjacent values on the same line is separated by one or more spaces. All values is integers, 1≤ m, N ≤ 10, 0≤ Pi , ri ≤100, Pi≤ ri (1≤ i m).

Output

The first and only line of the that file must contain the minimal possible cost of a trip from the City 1 to the city n. If The trip isn't possible for any reason, the line must contain the word 'impossible'.

Sample Input

4 51 2 1 10 102 3 1 30 503 4 3 80 802 1 2 10 101 3 2 10 50

Sample Output

110

Approximate test instructions:

There are n cities and M (1<=n,m<=10) routes. Now it's going from City 1 to City N. Some roads are charged, from city A to City B, if you have been to C city before, then just pay P's money, if not have to pay r money. Ask for at least how much it will cost.

Note: The path is direction-aware.

The difficulty is that "there may be multiple paths between cities and cities":

1, the input data may appear multiple from city A to City B path information, but the cost varies;

2, for the same path from City A to City B, allow repeated walk.

One would ask, what's the point of repeating the same path? Simply increase the cost, why can not mark all paths, each road only allowed to walk once, so the cost is not less?

I started to get caught up in this kind of thinking, but the idea was "half right, half wrong".

First look at a set of data:

6 5

1 2 1) 10 10

2 3 4) 10 100

2 4 2) 15 15

4 1 1) 12 12

3 6 6) 10 10

If each road is allowed to walk only once, then there are only 1 scenarios:

1à2à3à6 Total 135 Yuan

But the correct answer to this set of data is $67. Why? The correct scenario is as follows:

1à2à4à1à2à3à6 Total 67 Yuan

Apparently 1à2 repeated the trip, the aim is to reach the City 4 first, so that the cost of 2à3 This section of the road from 100 to 10 yuan.

Seeing a lot of students here seems to be suddenly, but the problem is coming again. If the same road is allowed to repeat, then it can not be marked, but once not marked, lost the search constraints, DFS can not end, not into a dead loop?

I just said that the idea of "half, Wrong half", "yes" is the "repeat go will increase costs", "wrong" is wrong in "repeat the object is not a certain road, but a certain loop." A repeat of the same loop will only really add to the cost. But tag loops are cumbersome, so can you judge the way the current plan has come to pass, based on the number of times a road or a city repeats? The answer is yes.

The above example has been verified, the same path can be repeated, but not infinitely repeated, the number of repetitions is limited. So how many times should we repeat it to be reasonable? This is related to the M-value. The M-Value range of the topic is <=10, so when the number of times a city is reached is >3 (excluding 3), the scheme must appear in the loop (the students on the internet call it "gate number").

So just change the bool vist[] to int vist[] to mark, the subject can be solved.

/*The difficulty of this topic is that each point will be accessed multiple times, so that the DFS boundary condition is not good to write the problem I have encountered in this topic: 1. I want to enumerate the edges that he can handle when I find a point, and it is difficult to enumerate the conditions when there are multiple edges. The positive solution is when you go to the end of this side, test this side of CI has been traversed, and then compare the size of pi and RI. 2. Will not process a side multiple walk, heavy edge and a point multiple walk, at the beginning of the thought that each point only walk once, the results of boundary conditions can not be obtained the optimal value. The standard answer is to limit the maximum walk three times per point, because M "= 10, if not sure, change 3 to 10 can still*/#include<iostream>using namespacestd; #include<cstdio>#include<cstring>#defineN 11#include<vector>#defineINF (1<<31)-1structedge{intBi,ci,pi,ri;}; Vector<Edge>Edge[n];intAns,n,m,a,b,c,p,r,vis[n];inlinevoidinput () {scanf ("%d%d",&n,&m);  for(intI=1; i<=m;++i) {scanf ("%d%d%d%d%d",&a,&b,&c,&p,&R);    Edge[a].push_back ({b,c,p,r}); }}voidDfsintKintCost ) {Vis[k]++; if(Cost>=ans)return; if(k==N) {ans=min (ans,cost); return; }    intSize=edge[k].size ();  for(intI=0; i<size;++i) {if(vis[edge[k][i].bi]>3)Continue; intt=inf; if(vis[edge[k][i].ci]&&edge[k][i].pi<t) T=Edge[k][i].pi; if(edge[k][i].ri<t) T=Edge[k][i].ri; DFS (Edge[k][i].bi,cost+t); VIS[EDGE[K][I].BI]--; }}intMain () {input (); Ans=inf; DFS (1,0); if(Ans==inf) printf ("Impossible"); Elseprintf"%d", ans); return 0; } 

Multiple access nodes for DFS POJ 3411 Paid Roads

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.