Java实现John's trip(约翰的小汽车)
1 问题描述
John’s trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8998 Accepted: 3018 Special Judge
Description
Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the 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 his parents’ 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 town had different numbers. Each street was connecting exactly two junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest. But Johnny was not able to find even one such round trip.
Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the street appears first in the input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street
Input
Input file consists of several blocks. Each block describes 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 by the 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 of street numbers (single members of the sequence are separated by space) describing Johnny’s round trip. If the round trip cannot be found the corresponding output block contains the message “Round trip does not exist.”
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
中文翻译
约翰的旅行
时间限制:1000毫秒内存限制:65536K
提交总数:8998件接受人数:3018名特别法官
说明
小约翰尼有辆新车。他决定开车环城去拜访他的朋友。约翰尼想拜访他所有的朋友,但他们中有很多人。他在每条街上都有一个朋友。他开始考虑如何尽可能缩短行程。很快他就意识到,最好的办法是只穿过城镇的每一条街道一次。当然,他想在他开始的同一个地方完成他的旅行,在他父母的家里。
约翰尼镇上的街道是用1到n的整数命名的,n<1995。连接点由1到m的整数独立命名,m<=44。没有连接44条以上街道的交叉口。镇上所有的路口都有不同的号码。每条街道正好连接两个路口。镇上没有两条街道的号码相同。他立即开始计划他的往返旅行。如果有不止一次这样的往返旅行,他会选择一次,当写下街道号码的序列时,从字典上来说,是最小的。但约翰尼甚至找不到这样的往返旅行。
帮助约翰尼写一个程序,找出最短的往返行程。如果往返不存在,程序应编写一条消息。假设约翰尼住在街尾的交叉路口,在输入中以较小的数字出现在第一位。镇上所有的街道都是双向的。镇上有一条从每条街道到另一条街道的路。城里的街道很窄,一旦他在街上,就不可能把车掉头。
输入
输入文件由几个块组成。每个街区都有一个城镇。块中的每一行包含三个整数x;y;z,其中x>0和y>0是由街道编号z连接的连接数。块的结尾由包含x=y=0的行标记。在输入文件的末尾有一个空块,x=y=0。
输出
输出每个街区的一行包含描述约翰尼往返路线的街道编号序列(序列中的单个成员用空格分隔)。如果找不到往返行程,则相应的输出块包含消息“往返不存在”。
package com.liuzhen.practice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static int MAX = 100; //题意说明,最多44个路口
public static int start; // Johnny出发起点
public static int[] id = new int[MAX];
public static int[] degree = new int[MAX]; //用于计算给定图每个顶点的度
public static boolean[] used = new boolean[2000]; //用于判断图中相应边是否被遍历
public static int[] path = new int[MAX];
public static int count; //用于统计行走路径的街道数
public static ArrayList<String> result = new ArrayList<String>();
class MyComparator implements Comparator<edge> {
public int compare(edge o1, edge o2) {
if(o1.street > o2.street)
return 1;
else if(o1.street < o2.street)
return -1;
else
return 0;
}
}
static class edge {
public int a; //边的起点
public int b; //边的终点
public int street; //街道
public edge(int a, int b, int street) {
this.a = a;
this.b = b;
this.street = street;
}
}
//寻找顶点a的根节点
public int find(int[] id, int a) {
int root = a;
while(id[root] >= 0) {
root = id[root];
}
int i;
int k = a;
while(k != root) {
i = id[k];
id[k] = root;
k = i;
}
return root;
}
//合并顶点a和顶点b所在的树
public void union(int[] id, int a, int b) {
int rootA = find(id, a);
int rootB = find(id, b);
int rootNum = id[rootA] + id[rootB];
if(id[rootA] < id[rootB]) {
id[rootB] = rootA;
id[rootA] = rootNum;
} else{
id[rootA] = rootB;
id[rootB] = rootNum;
}
return;
}
public void init() {
for(int i = 0;i < 50;i++) {
id[i] = -1; //初始化所有顶点所在树的根节点编号为-1
degree[i] = 0;
path[i] = -1;
count = 0;
}
for(int i = 0;i < 2000;i++) {
used[i] = false;
}
return;
}
public boolean judge(ArrayList<edge>[] map) {
int root = find(id, start);
for(int i = 0;i < MAX;i++) {
if(map[i].size() == 0)
continue;
Collections.sort(map[i], new MyComparator());
for(int j = 0;j < map[i].size();j++) {
if(find(id, map[i].get(j).b) != root)
return false;
}
}
for(int i = 0;i < MAX;i++) {
if(degree[i] % 2 != 0)
return false;
}
return true;
}
public void dfs(ArrayList<edge>[] map, int start) {
for(int i = 0;i < map[start].size();i++) {
if(!used[map[start].get(i).street]) {
used[map[start].get(i).street] = true;
path[count++] = map[start].get(i).street;
dfs(map, map[start].get(i).b);
}
}
}
public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
while(true) {
int a1 = in.nextInt();
int b1 = in.nextInt();
if(a1 == 0 && b1 == 0)
break;
int c1 = in.nextInt();
start = Math.min(a1, b1); //Johnny出发起点
test.init(); //初始化输入顶点的度和所在树的根节点
@SuppressWarnings("unchecked")
ArrayList<edge>[] map = new ArrayList[MAX];
for(int i = 0;i < MAX;i++) {
map[i] = new ArrayList<edge>();
}
map[a1].add(new edge(a1, b1, c1));
map[b1].add(new edge(b1, a1, c1));
degree[a1]++;
degree[b1]++;
test.union(id, a1, b1); //合并顶点a1和顶点b1所在树
while(true) {
int a = in.nextInt();
int b = in.nextInt();
if(a == 0 && b == 0)
break;
int c = in.nextInt();
map[a].add(new edge(a, b, c));
map[b].add(new edge(b, a, c));
degree[a]++;
degree[b]++;
test.union(id, a, b);
}
String tempR = "";
if(test.judge(map)) {
test.dfs(map, start);
for(int i = 0;i < count;i++) {
tempR = tempR + path[i] + " ";
}
} else {
tempR = "Round trip does not exist.";
}
result.add(tempR);
}
for(int i = 0;i < result.size();i++)
System.out.println(result.get(i));
}
}
运行结果:
2 1
3 2
1 6
2 5
3 3
1 4
0
2 1
3 2
3 3
4 4
0
0
2 3 5 4 6
Round trip does not exist.
Java实现John's trip(约翰的小汽车)的更多相关文章
- UVA302 John's trip(欧拉回路)
UVA302 John's trip 欧拉回路 attention: 如果有多组解,按字典序输出. 起点为每组数据所给的第一条边的编号较小的路口 每次输出完额外换一行 保证连通性 每次输入数据结束后, ...
- POJ1041 John's trip
John's trip Language:Default John's trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
- poj 1041 John's trip 欧拉回路
题目链接 求给出的图是否存在欧拉回路并输出路径, 从1这个点开始, 输出时按边的升序输出. 将每个点的边排序一下就可以. #include <iostream> #include < ...
- POJ 1041 John's trip 无向图的【欧拉回路】路径输出
欧拉回路第一题TVT 本题的一个小技巧在于: [建立一个存放点与边关系的邻接矩阵] 1.先判断是否存在欧拉路径 无向图: 欧拉回路:连通 + 所有定点的度为偶数 欧拉路径:连通 + 除源点和终点外都为 ...
- John's trip POJ - 1041(这题数据有点水)
题意: 其实还是一个欧拉回路,但要按字典序走路: 解析: 我真是蠢啊emm... map[i][j]表示由顶点i经街道j会到达的顶点编号 然后枚举j就好了 用栈储存.. 虽然我不是这样写的 #incl ...
- John's trip(POJ1041+欧拉回路+打印路径)
题目链接:http://poj.org/problem?id=1041 题目: 题意:给你n条街道,m个路口,每次输入以0 0结束,给你的u v t分别表示路口u和v由t这条街道连接,要输出从起点出发 ...
- [POJ 1041] John's Trip
[题目链接] http://poj.org/problem?id=1041 [算法] 欧拉回路[代码] #include <algorithm> #include <bitset&g ...
- poj1041 John's trip——字典序欧拉回路
题目:http://poj.org/problem?id=1041 求字典序欧拉回路: 首先,如果图是欧拉图,就一定存在欧拉回路,直接 dfs 即可,不用 return 判断什么的,否则TLE... ...
随机推荐
- FastDFS安装(mac)|文件存储方案
目录 FastDFS安装(mac)|文件存储方案 1 FastDFS介绍 1.1 FastDFS架构 1.2 工作原理实例介绍 1.3 FastDFS上传和下载流程 1.4 FastDFS文件索引 2 ...
- Web_python_template_injection
0x01 pthon模板注入 判断是否为模板注入 paload http://124.126.19.106:34164/{{1+1}} //如果里面的值被执行了,那么存在模板注入 //调用os模块的p ...
- 省市县三级联动sql文件
截止于2018年,中国有34个省级, 地级行政区划单位334个 县级行政区划单位2851个 乡级行政区划单位39888个 例如 湖南省有多少个市级单位: 先拿到湖南省的code,再查city表: SE ...
- java -> 异常类与自定义异常
异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体 ...
- pytest——pycharm中右击运行(run)没有问题,在terminal中运行pytest报错:E ModuleNotFoundError: No module named
参考了这个解决办法:https://blog.csdn.net/qq_36829091/article/details/82180866 我的是Windows,linux的和Windows的解决办法有 ...
- Bash Shell之内建命令和保留字
转载自:http://blog.chinaunix.net/uid-25880122-id-2941630.html 命令 含义 ! 保留字,逻辑非 : 不做任何事,只做参数展开 . 读取文件并在sh ...
- HTML学习——day1
HTML是一种用于创建网页的标准标记语 注意:对于中文网页需要使用<meta charset=''utf-8''>声明编码,否则会出现乱码. HTML标签 <标签>内容< ...
- ql自动化测试之路-概述篇
前言:本节主要讲解自动化测试的基本概述,包括分层自动化测试.自动化测试中用到的工具.以及关于自动化测试的想法 一.分层自动化测试 上图是经典的测试金字塔.用它来形容目前测试投入的价值是比较适合的,同样 ...
- 旧版flexbox局部填坑
本来昨晚要写一篇react的小笔记,恰好同学小聚的时候附近有个ios维修,把我的4s拿去修好,早上用我还是ios5.1系统的4s打开自己的页面,发现flexbox布局的部分是乱的,眼前一黑. what ...
- 02 初级版web框架
02 初级版web框架 服务器server端python程序(初级版): import socket server=socket.socket() server.bind(("127.0.0 ...