Commit 526a7bd9 authored by larsesselink's avatar larsesselink
Browse files

-final

parent f89bfb28
import java.io.File;
import java.io.FileNotFoundException;
//Student: Lars Esselink | s1058642
import java.util.*;
/*
X_k = x coord for k'th pillar
Y_k = y coord for k'th pillar
R_i = Radius of the i'th disk
C_i = Cost of the i'th disk
*/
public class Algorithm {
private final String fileName = "A:\\Documents\\Computing Science\\AD\\Project1\\testcases\\02-sample.in";
// private final String fileName = "/home/ldesselink/Documents/CS/AD/project1/rep/testcases/02-sample.in";
private final Vector<Vector<Integer>> input = new Vector<>();
private final Vector<Pillar> pillars = new Vector<>();
private final Vector<Disk> disks = new Vector<>();
private final HashSet<Edge> edges = new HashSet<>();
private final List<Pillar> pillars = new ArrayList<>();
private final List<Disk> disks = new ArrayList<>();
private int N;
private int M;
private int W;
......@@ -29,7 +13,7 @@ public class Algorithm {
private final PriorityQueue<Pillar> queue = new PriorityQueue<>();
private final LinkedHashSet<Pillar> visited = new LinkedHashSet<>();
private final HashMap<Pillar, Pair<Disk,Long>> distance = new HashMap<>();
private final List<Pair<Pillar, Disk>> pairs = new ArrayList<Pair<Pillar, Disk>>();
private final List<Pair<Pillar, Disk>> pairs = new ArrayList<>();
......@@ -37,35 +21,18 @@ public class Algorithm {
public void main(){
src.setCost(0L);
initiateVector();
readFromConsole();
makePairs();
// for(Pair<Pillar, Disk> pair : getAdj(src,new Disk(0,0))){
// Pillar pillar = pair.getKey();
// System.out.println(pillar.getID() + " : " + pair.getValue().getRadius());
// }
// for(Pillar pillar: pillars){
// System.out.print("Pillar " + pillar.getID() + " has neighbour: ");
// for(Pillar pillar2 : pillar.getAdj().keySet()){
// System.out.print(pillar2.getID() + " ");
// }
// System.out.print("\n");
// }
algorithm();
//
// visited = visited.stream().distinct().collect(Collectors.toList());
//
//// for(Pillar pillar: distance.keySet()){
//// System.out.println(pillar.getID() + " : " + pillar.getCost());
//// }
//
for(Pillar pillar: visited){
System.out.println(pillar.getID()+ " = " + distance.get(pillar).getValue());
}
Pillar lastPillar = new Pillar(0,0,0);
for(Pillar pillar: visited){
lastPillar = pillar;
}
System.out.println(distance.get(lastPillar).getValue());
}
......@@ -80,11 +47,12 @@ public class Algorithm {
distance.put(pair.getKey(),new Pair<>(pair.getValue(),Long.MAX_VALUE));
}
distance.put(target, new Pair<>(new Disk(0,0),Long.MAX_VALUE));
src.setDisk(new Disk(0,0));
while(!queue.isEmpty()){
// System.out.println(queue.size());
Pillar pillar1 = queue.remove();
Disk disk1 = pillar1.getDisk();
......@@ -94,22 +62,23 @@ public class Algorithm {
Pillar pillar2 = pair.getKey();
Disk disk2 = pair.getValue();
long tempCost = distance.get(pillar1).getKey().getCost() + disk2.getCost();
// System.out.println("From pillar " + pillar1.getID() + " = " + pillar2.getID() + " : " + distance.get(pillar2).getValue() + " > " + tempCost);
if((distance.get(pillar2).getValue() > tempCost)){
distance.put(pillar2, new Pair<>(disk2,tempCost+distance.get(pillar1).getValue()));
pillar1.setDisk(disk1);
long neighbourCost = distance.get(pillar2).getValue();
long currentCost = distance.get(pillar1).getValue();
long edgeWeight = disk2.getCost();
long newCost = currentCost + edgeWeight;
boolean shortestPath = neighbourCost > (currentCost + edgeWeight);
if(shortestPath && pillar2 != target){
distance.put(pillar2,new Pair<>(disk2,newCost));
pillar2.setDisk(disk2);
}
if(!visited.contains(pillar2))
{
if(!visited.contains(pillar2)){
queue.add(pillar2);
}
}
}
......@@ -122,9 +91,6 @@ public class Algorithm {
Pillar pillarPair = entry.getKey();
Disk diskPair = entry.getValue();
// System.out.println("Checking " + pillar.getID() + " With " + pillarPair.getID());
// System.out.println("Doing: " + distancePoints(pillar, pillarPair) + " < " + diskPair.getRadius() + " + " + disk.getRadius() + " and " + (pillar != pillarPair));
if(distancePoints(pillar, pillarPair) <= diskPair.getRadius() + disk.getRadius() && pillar != pillarPair){
temp.add(new Pair<>(pillarPair,diskPair));
}
......@@ -139,18 +105,9 @@ public class Algorithm {
pairs.add(new Pair<>(pillars.get(i), disk));
}
}
pairs.add(new Pair<>(target,new Disk(0,0)));
}
// public boolean distanceCheck(Pillar pillar1, Pillar pillar2){
// if(checkIfSoT(pillar1,pillar2)){
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistance;
// } else{
// return distancePoints(pillar1.getX(), pillar1.getY(), pillar2.getX(), pillar2.getY()) <= maxDistanceHalf;
// }
// }
public double distancePoints(Pillar pillar1,Pillar pillar2) {
double x1 = pillar1.getX();
double y1 = pillar1.getY();
......@@ -159,79 +116,28 @@ public class Algorithm {
return (long) Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
public Long distancePointToLine(Pillar pillar1, Pillar pillar2){
int x1 = pillar1.getX();
int y1 = pillar1.getY();
int x2 = pillar1.getX();
int y2;
if(pillar2.getID() == 0){
y2 = 0;
} else {
y2 = W;
}
return (long) Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
public void readFromConsole(){
Scanner sc = new Scanner(System.in);;
public Boolean checkIfSoT(Pillar pillar1, Pillar pillar2){
return pillar1.getID() != 0 && pillar2.getID() != 0 && pillar1.getID() != -1 && pillar2.getID() != -1 ;
}
public void initiateVector(){
readFromFile();
N = input.get(0).get(0);
M = input.get(0).get(1);
W = input.get(0).get(2);
target = new Pillar(0,W, 10000 );
Disk tempDisk = null;
for(int i = N + 1; i <= N + M; i++){
tempDisk = new Disk(input.get(i).get(0),input.get(i).get(1));
disks.add(tempDisk);
}
N = sc.nextInt();
M = sc.nextInt();
W = sc.nextInt();
pillars.add(src);
Pillar tempPillar = null;
Pillar tempPillar;
for(int i = 1; i < N + 1; i++){
tempPillar = new Pillar(input.get(i).get(0),input.get(i).get(1),i);
tempPillar = new Pillar(sc.nextInt(),sc.nextInt(),i);
pillars.add(tempPillar);
}
target = new Pillar(0,W, -1);
pillars.add(target);
}
public void readFromFile(){
File file = new File(fileName);
Scanner sc;
try {
sc = new Scanner(file);
while(sc.hasNextLine()){
// System.out.print(sc.nextLine() + "\n");
addToVector(sc.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void addToVector(String line){
String[] split = line.split("\\s+");
Vector<Integer> temp = new Vector<Integer>();
for(String strings : split){
temp.add(Integer.parseInt(strings));
Disk tempDisk;
for(int i = N + 1; i <= N + M; i++){
tempDisk = new Disk(sc.nextInt(),sc.nextInt());
disks.add(tempDisk);
}
input.add(temp);
}
}
import java.util.List;
public class Dijkstra {
private Long dist[];
List<List<Pillar>> adjacent;
public void dijkstraAlg(List<List<Pillar>> adjacent, long src){
this.adjacent = adjacent;
}
}
//Student: Lars Esselink | s1058642
public class Disk {
private final long Cost;
......
import java.util.Comparator;
public class Edge{
private final Pillar pillar1;
private final Pillar pillar2;
private final long cost;
public Edge(Pillar pillar1, Pillar pillar2, long cost){
this.pillar1 = pillar1;
this.pillar2 = pillar2;
this.cost = cost;
}
public Pillar getPillar1() {
return pillar1;
}
public Pillar getPillar2() {
return pillar2;
}
public long getCost() {
return cost;
}
public double distancePoints(double x1, double y1, double x2, double y2) {
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
}
@Override
public String toString(){
return ("(" + "(" + pillar1.getX() + "," + pillar1.getY() + ")" + "," + "(" + pillar2.getX() +"," + pillar2.getY() + ")" + ")");
}
}
//Student: Lars Esselink | s1058642
public class Pair<X, Y> {
private final X key;
private final Y value;
......
import java.util.*;
//Student: Lars Esselink | s1058642
public class Pillar implements Comparable<Pillar> {
private final int X;
......@@ -6,7 +6,6 @@ public class Pillar implements Comparable<Pillar> {
private Long Cost = Long.MAX_VALUE;
private final int ID;
private Disk disk = new Disk(0, Long.MAX_VALUE);
private final HashMap<Pillar, Long> adj = new HashMap<>();
public Pillar(int X, int Y, int ID){
......@@ -27,17 +26,6 @@ public class Pillar implements Comparable<Pillar> {
this.Cost = cost;
}
public void addAdj(Pillar pillar, Long weight){
if(this.adj.get(pillar) == null){
this.adj.put(pillar, weight);
} else if (this.adj.get(pillar) > weight){
this.adj.put(pillar, weight);
}
}
public HashMap<Pillar,Long> getAdj(){
return adj;
}
public Long getCost(){
return Cost;
}
......@@ -54,7 +42,6 @@ public class Pillar implements Comparable<Pillar> {
this.disk = disk;
}
@Override
public int compareTo(Pillar pillar) {
return Long.compare(this.getCost(), pillar.getCost());
......
//Student: Lars Esselink | s1058642
public class main {
static Algorithm alg = new Algorithm();
public static void main(String[] args){
double start = System.nanoTime();
alg.main();
double end = System.nanoTime();
double total = end - start;
System.out.println("runtime in seconds: " + (total/1000000000));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment