<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Maps TSP</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false" type="text/javascript"></script>
<script type="text/javascript">
function calculate() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.setMapType(G_NORMAL_MAP);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
var directions = new GDirections(map, null);
GEvent.addListener(directions, "error", function() {
var message = document.getElementById("message");
message.appendChild(document.createTextNode(directions.getStatus().code));
message.appendChild(document.createElement("br"));
});
directions.loadFromWaypoints(
toGLatLng(document.getElementById("input").value.split("\n")),
{ locale: "ja_JP" }
);
var message = document.getElementById("message");
message.innerHTML = "";
var input = document.getElementById("input").value.split("\n");
var results
= new Array(); for (var i = 0; i < input.length; i++) {
for (var j = 0; j < input.length; j++) {
results.push([input[i], input[j]]);
}
}
setInterval(function(){
if (results.length == 0 ) {
return;
}
var array = results.shift();
var directions = new GDirections(null, null);
var route = toPlaceNames(array);
GEvent.addListener(directions, "load", function() {
var message = document.getElementById("message");
message.appendChild(document.createTextNode(route.join(" ") + " " + directions.getDuration().seconds));
message.appendChild(document.createElement("br"));
});
GEvent.addListener(directions, "error", function() {
var message = document.getElementById("message");
message.appendChild(document.createTextNode(directions.getStatus().code));
message.appendChild(document.createElement("br"));
});
directions.loadFromWaypoints(
toGLatLng(array),
{ locale: "ja_JP" }
);
}, 500);
}
}
if (GBrowserIsCompatible()) {
GEvent.addDomListener(window, "unload", function() { GUnload(); });
}
function toPlaceNames(array) {
for (var index in array) {
var items = array[index].split(" ");
ret.push(items[0]);
}
return ret;
}
function toGLatLng(array) {
for (var index in array) {
var items = array[index].split(" ");
ret.push(new GLatLng(items[1], items[2]));
}
return ret;
}
</script>
</head>
<body>
<h1>Maps TSP</h1>
<h2>入力</h2>
<textarea id="input" style="width: 25em; height: 13em;">東京駅 35.681382 139.766084
首里城 26.216991 127.719362
三内丸山遺跡 40.811225 140.695649
坂本竜馬記念館 33.496428 133.571845
五稜郭 41.796868 140.756794
襟裳岬 41.926490 143.246642
国会正門前 35.676293 139.746927
京都大学 35.025280 135.780910
東京国際フォーラム 35.676651 139.763651
京都リサーチパーク 34.994707 135.740076</textarea>
<input type="button" value="計算する" onClick="calculate()"/>
<h2>出力</h2>
<div id="map" style="width: 400px; height: 300px; border: 1px solid gray; float: left;">ここに地図が表示されます。</div>
<div id="message" style="margin-left: 400px; padding-left: 1em;">ここにメッセージが表示されます。</div>
</body>
</html>
--------------------------------------------------------------------------------
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
public class TSP {
static long minDistance
= Long.
MAX_VALUE;
Set<String> places = new LinkedHashSet<String>();
Scanner scanner
= new Scanner
(System.
in); while (scanner.hasNextLine()) {
String line
= scanner.
nextLine(); String[] items
= line.
split(" "); if (items.length == 3) {
long distance
= Long.
parseLong(items
[2]); places.add(from);
distances.put(from + "-" + to, distance);
} else {
System.
out.
println("wrong format : " + line
); }
}
scanner.close();
enumurate(new ArrayList<String>(places), new Stack<String>(), distances);
}
static void enumurate
(List
<String
> in, Stack
<String
> out, Map
<String, Long
> distances
) { if (in.isEmpty()) {
out.push(out.get(0));
long distance = 0;
for (int i = 1; i < out.size(); i++) {
distance += distances.get(out.get(i - 1) + "-" + out.get(i));
}
if (distance < minDistance) {
minDistance = distance;
System.
out.
println(out.
toString().
replace(", ",
" ") + ", " + distance
); }
out.pop();
} else {
for (int i = 0; i < in.size(); i++) {
in.remove(i);
out.push(value);
enumurate(in, out, distances);
in.add(i, value);
out.pop();
}
}
}
}