Poj_1041 John's trip

Source: Internet
Author: User

John's trip
Time Limit: 1000 MS
Memory Limit: 65536 K
Total Submissions: 5455
Accepted: 1774
Special Judge
 
 
 
 
Description
Little Johnny has got a new car. he decided to drivearound the town to visit his friends. johnny wanted to visit all his friends, but there was quota of them. in each street he had one friend. he startedthinking how to make his trip as short as possible. very soon he realized thatthe best way to do it was to travel through each street of town only once. naturally, he wanted to finish his trip at the same place he started, at hisparents 'house.

The streets in Johnny's town were named by integer numbers from 1 to n, n <1995. the junctions were independently named by integer numbers from 1 to m, m <= 44. no junction connects more than 44 streets. all junctions in the townhad different numbers. each street was connecting exactly two junctions. no twostreets in the town had the same number. he immediately started to plan hisround trip. if there was more than one such round trip, he wocould have chosenthe one which, when written down as a sequence of street numbers islexicographically the smallest. but Johnny was not able to find even one suchround trip.

Help Johnny and write a program which finds the desired shortest round trip. ifthe round trip does not exist the program shocould write a message. assume thatJohnny lives at the junction ending the street appears first in the input withsmaller number. all streets in the town are two way. there exists a way fromeach street to another street in the town. the streets in the town are verynarrow and there is no possibility to turn back the car once he is in thestreet
Input
Input file consists of several blocks. each blockdescribes one town. each line in the block contains three integers x; y; z, where x> 0 and y> 0 are the numbers of junctions which are connected bythe street number z. the end of the block is marked by the line containing x = y = 0. at the end of the input file there is an empty block, x = y = 0.
Output
Output one line of each block contains the sequence ofstreet numbers (single members of the sequence are separated by space) describing Johnny's round trip. if the round trip cannot be found thecorresponding output block contains the message "Round trip does notexist."
Sample Input
1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0
Sample Output
1 2 3 5 4 6
Round trip does not exist.
Source
Central Europe 1995
Question:
X, y, and z indicate the start point, end point, and number of the edge. Starting from the starting point of the graph, ask if you can access all the edges and return to the start point, the same access edge is accessed according to the dictionary sequence.
Solution:
This topic can be solved using Euler's loop. First, create a graph based on the input, and save the number, start point, and end point of each edge in the graph, and record the number of edges and points, use the Euler loop to determine whether the graph has an Euler loop. If it exists, recursively traverse each edge until all edges are traversed. The start point of the traversal is the end point of the previous edge.
Code:
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Define MAX 50
Using namespace std;
 
Struct edge
{// Save edge
Int start, end;
};
Edge e [2000]; // Number of Edges
Int g [MAX] [MAX];
Int indegree [MAX];
Int outdegree [MAX];
Int temp [MAX];
Int visited [2000]; // records whether a node has been accessed
Int t;
Int v, es; // number of points and edges
 
// Traverse each edge from the starting point s
Void euler (int s)
{
For (int I = 1; I <= es; I ++)
{
// This edge has not been accessed, and its start point or end point is the point s to be accessed.
If (visited [I] = 0 & (e [I]. start = s | e [I]. end = s ))
{
Visited [I] = 1; // access this edge
Euler (e [I]. start + e [I]. end-s); // access the next edge from the start point to the end point.
Temp [t] = I;
T ++;
}
}
}
 
Int main ()
{
Int x, y, z;
Int I, j;
Int flag = 0;
While (true)
{
Int start;
Memset (g, 0, sizeof (g ));
Memset (visited, 0, sizeof (visited ));
Memset (indegree, 0, sizeof (indegree ));
Memset (outdegree, 0, sizeof (outdegree ));
V = 0;
Es = 0;
T = 0;
While (true)
{
Scanf ("% d", & x, & y );
If (x = 0 & y = 0)
{
Flag ++;
If (flag = 2)
Return 0;
Break;
}
// Obtain the minimum value from the start point, x, and y.
Start = x;
If (start> y)
Start = y;
Flag = 0;
Scanf ("% d", & z );
G [x] [y] = 1;
G [y] [x] = 1;
E [z]. start = x; // connected graph of the graph. You do not need to determine whether the graph is connected.
E [z]. end = y;
// Calculate the number of edges and points
If (es <z)
Es = z;
If (v <x)
V = x;
If (v <y)
V = y;
// Record the output and inbound degrees to determine the Euler's path
Indegree [x] ++;
Outdegree [y] ++;
}
 
// Determine whether it is an Euler Loop
For (I = 1; I <= v; I ++)
{
For (j = 1; j <= v; j ++)
{
If (g [I] [j] = 1 & (outdegree [j] + indegree [j]) % 2! = 0)
{
Printf ("Round trip does not exist. \ n ");
Break;
}
}
If (j <= v)
Break;
}
If (I <= v)
Continue;
Euler (start );
For (I = ES-1; I> = 1; I --)
{
Printf ("% d", temp [I]);
}
Printf ("% d \ n", temp [0]);
}
 
Return 0;
}
Author: CSDN515

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.