SoJ 1015 Jill's Tour Paths problem solving report

Source: Internet
Author: User

Title Description:

1015 Jill ' s tour Paths

Constraints

Time limit:1 secs, Memory limit:32 MB

Description

Every year, Jill takes a bicycle tour between the villages. There is different routes she can take between these villages, but she does has an upper limit on the distance that she Wants to travel. Given a map of the region indicating the cities and the roads between them (and their distances), Jill would like to has A list of the various routes between the selected cities that would meet her distance requirements. Your task is to write a program that would produce a list of these routes, in increasing order of distance.

We make the following assumptions.

    • At the most one road connects any pair of villages, and this road are two-way and has a non-zero positive distance.
    • There is no roads that leads directly from a village back to the same village.
    • Jill is only concerned on a one-way trip. That's is, she's not a concerned about returning to the village from which she starts her tour.
    • Jill won't visit any village to than once during the tour.

The farthest Jill would ever travel is 9999 units

Input

The input would contain several possible cases, each including a route map, identification of the start and destination Vil Lages, and the maximum distance Jill is willing-to-travel.

Each case appears with the input as a set of integers separated by blanks and/or ends of lines. The order and interpretation of these integers in each are as follows:

    • NV , haven number of villages in the route map. This number would be no larger than 20.  
    • NR , haven number Of roads that appear in the route map. Each road connects a distinct pair of villages.  
    • NR Triples, one for each road, containing C1 , C2 , and DIST C1 and C2 identify Villages connected by a road, and DIST gives the distance between these villages on that road.  
    • SV, DV , haven numbers associated with the start and destination Villag Es The villages is numbered 1 to NV .   
    • MAXDIST , Haven maximum distance Jill is willing-to-travel (one-to-one).   

The data for the last case would be followed by a single integer with the value–1.

Output

For each case, display the case number (1, 2, ...) on the first line of output. Then, each on a separate additional line, list the routes, which Jill might take preceded by the length of the route. Order the routes first by length and from shortest to longest. Within routes has the same length, order them in increasing lexicographic order. The sample input and output provide suitable examples, and the formatting shown there should is followed closely (each vil Lage number should is separated by a single space). Separate the output for consecutive cases is a single blank line. If There is no route, print out "no acceptable TOURS" (notice there is one space at the front).

Sample Input

4 5

1 2 2

1 3 3

1 4 1

2 3 2

3 4 4

1 3

4

4 5

1 2 2

1 3 3

1 4 1

2 3 2

3 4 4

1 4

10

5 7

1 2 2

1 4 5

2 3 1

2 4 2

2 5 3

3 4 3

3 5 2

1 3

8

5 7

1 2 2

1 4 5

2 3 1

2 4 2

2 5 3

3 4 3

3 5 2

1 3

1

-1

Sample Output

Case 1:

 3:1 3

 4:1 2 3

Case 2:

 1:1 4

 7:1 3 4

 8:1 2 3 4

Case 3:

 3:1 2 3

 7:1 2 4 3

 7:1 2 5 3

 8:1 4 2 3

 8:1 4 3

Case 4:

 no acceptable TOURS

Topic Analysis:

A relatively simple problem, ask for two points less than the maximum length of all paths, with deep search and wide search can be resolved, to pay attention to deep search in the back of the time must remember to restore the data to the state before the last search. The following is a deep search implementation.

Code implementation:

#include <iostream>#include<algorithm>#include<vector>#include<string.h>using namespacestd;structSol {sol () {}introute[ -]; intsize; intDista;}; Vector<sol>sols;BOOLvisited[ +];intadj[ +][ +];intpath[ +][ +];intadjs[ +];intr[ -];intJ, SRC, Dist, Len, MAXR;//InitvoidCalP () {inti;    Sol one; One.size=J; One.dista=Len;  for(i =0; i < one.size; i++) {One.route[i]=R[i]; } sols.push_back (one);}voidDfsintNow ) {    if(now = =Dist)        {CalP (); return; }    intI, l; L=Adjs[now];  for(i =0; I < L; i++) {        if(!Visited[adj[now][i]]) {R[j++] =Adj[now][i]; Visited[adj[now][i]]=true; Len+=Path[now][adj[now][i]]; if(Len <=MAXR)            {DFS (adj[now][i]); } Len-=Path[now][adj[now][i]]; Visited[adj[now][i]]=false; J--; }    }}BOOLcmpConstsol& A,Constsol&b) {if(A.dista >B.dista) {return false; }    if(A.dista = =B.dista) {intMinl =a.size; inti; if(Minl >b.size) {minl=b.size; }         for(i =0; i < minl; i++) {            if(A.route[i] >B.route[i])return false; if(A.route[i] <B.route[i])return true; }        returnA.size <b.size; }    return true;}intMain () {intNV, NR, I, F, T, D, ncase =0, K;  while(CIN >> NV && NV! =-1) {cin>>nr;        Sols.clear (); memset (Path,0,sizeof(path)); memset (adj,0,sizeof(adj)); memset (Adjs,0,sizeof(Adjs)); Memset (visited,0,sizeof(visited));  for(i =0; I < nr; i++) {cin>> F >> t >>D; Adj[t][adjs[t]++] =F; ADJ[F][ADJS[F]++] =T; PATH[T][F]= Path[f][t] =D; } CIN>> src >>Dist; CIN>>MaxR; J= Len =0; VISITED[SRC]=true; R[j++] =src;        DFS (SRC);        Sort (Sols.begin (), Sols.end (), CMP); if(Ncase! =0) {cout<<Endl; } cout<<" Case"<< ++ncase <<": \ n";  for(i =0; I < sols.size (); i++) {cout<<" "<< Sols[i].dista <<":";  for(k =0; K < sols[i].size; k++) {cout<<" "<<Sols[i].route[k]; } cout<<Endl; }        if(sols.size () = =0) {cout<<"NO acceptable TOURS"<<Endl; }    }}

Complexity of:

The number of nodes in the graph is V, the number of sides is e, and the number of solutions is N.

Time: Initialize (E) + deep Search (e*v) + sort (N*LG (n))

Space: v^2

Label:

Deep Search

SoJ 1015 Jill's Tour Paths problem solving report

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.