我正在尝试解决http://www.spoj.com/problems/ISLHOP/
我尝试过的解决方案适用于样本输入/输出,但未能通过判断过程得出错误的答案。
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
class Main {
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int group = 0;
while(in.hasNextLine()) {
int numberOfIsland = in.nextInt();
if(numberOfIsland == 0)
{
break;
}
group++;
in.nextLine();
ArrayList<Island> isl = new ArrayList<Island>();
for(int i = 0; i < numberOfIsland; i++){
String s = in.nextLine();
String[] inputList = s.split("\\s+");
Island newIsland = new Island(Integer.parseInt(inputList[0]),Integer.parseInt(inputList[1]),Integer.parseInt(inputList[2]),-1);
isl.add(newIsland);
}
double result = solve(isl);
System.out.print("Island Group: "+group+" Average ");
System.out.println(String.format("%.2f", result));
}
}
public static double solve(ArrayList<Island> islands) {
TreeSet<Island> islandSet = new TreeSet<Island>();
double time = 0;
islandSet.add(islands.get(0));
Island current;
while(!islandSet.isEmpty()){
current = islandSet.first();
current.queued = true;
islandSet.remove(current);
Island parent = current.parent;
if(parent != null)
{
if(current.parent.curmax < current.curdis)
{
current.curmax = current.curdis;
}
else{
current.curmax = current.parent.curmax;
}
time += current.num * current.curmax;
}
for(int i=0; i< islands.size(); i++)
{
Island next = islands.get(i);
if(!next.queued){
double dis = Math.sqrt(
Math.pow(current.x-next.x,2)
+Math.pow(current.y-next.y,2));
if(islandSet.contains(next))
{
if(next.curdis > dis){
islandSet.remove(next);
next.parent = current;
next.curdis = dis;
islandSet.add(next);
}
}
else{
next.parent = current;
next.curdis = dis;
islandSet.add(next);
}
}
}
}
double total = 0;
for(int i = 0; i < islands.size(); i++)
{
total += islands.get(i).num;
}
return time/total;
}
}
class Island implements Comparable<Island>{
public int x, y, num;
public Island parent;
public double curdis, curmax;
public boolean queued;
public Island(int x, int y, int num, double dis){
this.x = x;
this.y = y;
this.num = num;
this.queued = false;
curdis = 0;
curmax = 0;
}
@Override
public int compareTo(Island o) {
if(curdis > o.curdis)
{
return 1;
}
if(curdis == o.curdis)
{
return 0;
}
return -1;
}
}
class FasterScanner{
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FasterScanner() {
this(System.in);
}
public FasterScanner(InputStream is) {
mIs = is;
}
public boolean hasNext(){
try{
if( read() < 0 )
{
return false;
}
}
catch(InputMismatchException e)
{
return false;
}
return true;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
我尝试使用Scanner
而不是FasterScanner
来实现它,因为我无法弄清楚如何为hasNext()
编写FasterScanner
。
我无法弄清楚我的代码有什么问题,因为它适用于给定的示例输入。
有没有人知道这个工作解决方案或这个解决方案有什么问题?