Hi, all ,what is the problem in my code?

B

Baby Lion

I am solving a travelling saleman problem ,
and now I am using the test data named " oliver30"
but I can not get the right path length .
I was supposed to get about 423.74 as the minimum path length ...

import java.applet.Applet;
import java.math.*;
import java.util.*;
import java.awt.Graphics;
import java.io.*;
public class tspPrint extends Applet{
public void paint(Graphics g)
{
try{
Scanner cin = new Scanner( new FileInputStream("input1.txt"));
int n = cin.nextInt();

BufferedReader f2 =
new BufferedReader(
new InputStreamReader(new
FileInputStream("input2.txt")));
int [] a = new int [n];
double []x = new double[n];
double [] y = new double[n];
int temp = 0;
for( int i = 0 ;i< n ;i++)
{
temp = cin.nextInt();
x =50* cin.nextDouble();

y =50 * cin.nextDouble();
temp = cin.nextInt();
}
String Line = f2.readLine();
StringTokenizer st = new StringTokenizer(Line);
for(int i = 0 ;i< n ;i++ )
{
a = Integer.parseInt(st.nextToken());
}
for(int i = 0 ;i< n-1; i++)
{
g.drawLine((int)x[a],(int)y[a],(int)x[a[i+1]],(int)y[a[i
+1]]);
}
g.drawLine((int)x[a[0]],(int)y[a[0]],(int)x[a[n-1]],(int)y[a[n-1]]);


// problem here
// s should be about
423.74 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//s: get the path
Length !!!!!
//y?
double s = 0 ;
double dx = x[a[0]] -x[a[n-1]];
double dy = y[a[0]] - y [a[n-1]];
s += Math.sqrt(dx*dx + dy* dy);
for( int i = 0 ;i< n -1 ;i++ )
{
dx = x[a] - x[a[i+1]];
dy = y[a] - y[a[i+1]];
s+= Math.sqrt( dx*dx + dy*dy);
}


g.drawString(Double.toString(s),5,35);

}catch(Exception e)
{ System.out.println(e.toString());}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top