java 欧拉_算法笔记_142:无向图的欧拉回路求解(Java)

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

2 解决方案

具体代码如下:

packagecom.liuzhen.practice;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.Scanner;public classMain {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 result = new ArrayList();class MyComparator implements Comparator{public intcompare(edge o1, edge o2) {if(o1.street >o2.street)return 1;else if(o1.street

return 0;

}

}static classedge {public int a; //边的起点

public int b; //边的终点

public int street; //街道

public edge(int a, int b, intstreet) {this.a =a;this.b =b;this.street =street;

}

}//寻找顶点a的根节点

public int find(int[] id, inta) {int root =a;while(id[root] >= 0) {

root=id[root];

}inti;int k =a;while(k !=root) {

i=id[k];

id[k]=root;

k=i;

}returnroot;

}//合并顶点a和顶点b所在的树

public void union(int[] id, int a, intb) {int rootA =find(id, a);int rootB =find(id, b);int rootNum = id[rootA] +id[rootB];if(id[rootA]

id[rootB]=rootA;

id[rootA]=rootNum;

}else{

id[rootA]=rootB;

id[rootB]=rootNum;

}return;

}public voidinit() {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[] map) {int root =find(id, start);for(int i = 0;i < MAX;i++) {if(map[i].size() == 0)continue;

Collections.sort(map[i],newMyComparator());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[] map, intstart) {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 voidmain(String[] args) {

Main test= newMain();

Scanner in= newScanner(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[] map = newArrayList[MAX];for(int i = 0;i < MAX;i++) {

map[i]= new ArrayList();

}

map[a1].add(newedge(a1, b1, c1));

map[b1].add(newedge(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(newedge(a, b, c));

map[b].add(newedge(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));

}

}

运行结果:

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

1 2 3 5 4 6Round trip does not exist.

参考资料:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值