Skip to content

Instantly share code, notes, and snippets.

@hakomo
Created September 1, 2018 01:58
Show Gist options
  • Save hakomo/57766450271e0fbd1a42d781a979a9c9 to your computer and use it in GitHub Desktop.
Save hakomo/57766450271e0fbd1a42d781a979a9c9 to your computer and use it in GitHub Desktop.
import problem.Point;
import problem.Type;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.Scanner;
import javax.imageio.*;
public class Output {
public Input input;
public double cost, time;
public Output(Input input, String output) {
try (Scanner sc = new Scanner(output)) {
int n = sc.nextInt();
double[][] addJunction = new double[n][2];
for (int i = 0; i < n; ++i) {
addJunction[i][0] = sc.nextDouble();
addJunction[i][1] = sc.nextDouble();
}
n = sc.nextInt();
int[][] roads = new int[n][3];
for (int i = 0; i < n; ++i) {
roads[i][0] = sc.nextInt();
roads[i][1] = sc.nextInt();
roads[i][2] = sc.nextInt();
}
int[][] paths = new int[input.N][];
for (int i = 0; i < input.N; ++i) {
n = sc.nextInt();
paths[i] = new int[n];
for (int j = 0; j < n; ++j) {
paths[i][j] = sc.nextInt();
}
}
parse(input, addJunction, roads, paths);
} catch (Exception e) {
throw new VerifyException(e);
}
}
private void parse(Input input, double[][] addJunction, int[][] roads, int[][] paths) {
BufferedImage bi = new BufferedImage(606, 606, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 606, 606);
this.input = input;
int N = input.N + addJunction.length;
cost = addJunction.length * input.junctionCost;
time = 0;
Point[] points = new Point[N];
for (int i = 0; i < input.N; ++i) {
points[i] = input.points[i];
}
for (int i = 0; i < N - input.N; ++i) {
points[i + input.N] = new Point(addJunction[i][0], addJunction[i][1]);
}
Type[][] types = new Type[N][N];
for (int i = 0; i < roads.length; ++i) {
if (roads[i].length != 3)
throw new VerifyException();
int a = roads[i][0];
int b = roads[i][1];
int c = roads[i][2];
if (a < 0 || N <= a)
throw new VerifyException();
if (b < 0 || N <= b)
throw new VerifyException();
if (a == b)
throw new VerifyException();
Type t = Type.values()[c];
types[a][b] = types[b][a] = t;
cost += (t == Type.road ? input.roadCost : input.railCost) * dist(points[a], points[b]);
g.setStroke(new BasicStroke(2));
g.setColor(t == Type.road ? Color.BLUE : Color.GRAY);
g.drawLine(((int)points[a].x + 1) * 6, ((int)points[a].y + 1) * 6, ((int)points[b].x + 1) * 6, ((int)points[b].y + 1) * 6);
}
int count[][] = new int[N][N];
for (int i = 0; i < input.N; ++i) {
int p = i;
for (int j = 0; j < paths[i].length; ++j) {
int n = paths[i][j];
Type t = types[p][n];
if (t == null)
throw new VerifyException();
++count[p][n];
++count[n][p];
p = n;
}
if (p != input.toPoint[i])
throw new VerifyException();
}
for (int i = 0; i < input.N; ++i) {
int p = i;
double d = 0;
boolean car = true;
for (int j = 0; j < paths[i].length; ++j) {
int n = paths[i][j];
Type t = types[p][n];
if (t == Type.road) {
d += dist(points[p], points[n]) * Math.pow(1 + 0.02 * count[p][n], 2);
} else {
car = false;
d += dist(points[p], points[n]) / 2;
}
p = n;
}
if (car)
d /= 2;
time += d * d;
}
time = Math.sqrt(time);
g.setColor(Color.BLACK);
for (int i = 0; i < input.N; ++i) {
g.fillRect(((int)points[i].x + 1) * 6 - 2, ((int)points[i].y + 1) * 6 - 2, 4, 4);
}
g.setColor(Color.GREEN);
for (int i = input.N; i < N; ++i) {
g.fillRect(((int)points[i].x + 1) * 6 - 2, ((int)points[i].y + 1) * 6 - 2, 4, 4);
}
try {
ImageIO.write(bi, "png", new File("vis.png"));
} catch (Exception e) { e.printStackTrace(); }
}
public double getScore() {
return cost * time / 1000000;
}
private double dist(Point a, Point b) {
return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment