-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFencePlanning.java
More file actions
94 lines (91 loc) · 2.47 KB
/
FencePlanning.java
File metadata and controls
94 lines (91 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.*;
import java.util.*;
public class FencePlanning {
static ArrayList<ArrayList<Integer>> AdjList;
static int[] sets;
static Point[] locations;
public static void main(String[] args) throws IOException
{
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader("fenceplan.in"));
String[] temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
int M = Integer.parseInt(temp[1]);
AdjList = new ArrayList<ArrayList<Integer>>();
locations = new Point[N];
sets = new int[N];
Arrays.fill(sets, -1);
for (int i = 0; i < N; i++)
{
temp = br.readLine().split(" ");
locations[i] = new FencePlanning().new Point(Integer.parseInt(temp[0])
, Integer.parseInt(temp[1]));
AdjList.add(new ArrayList<Integer>());
}
int node1, node2;
for (int i = 0; i < M; i++)
{
temp = br.readLine().split(" ");
node1 = Integer.parseInt(temp[0])-1;
node2 = Integer.parseInt(temp[1])-1;
AdjList.get(node1).add(node2);
AdjList.get(node2).add(node1);
}
int setnum = 0;
for (int i = 0; i < N; i++)
{
if (sets[i] != -1)
continue;
bfs(i, setnum++);
}
ArrayList<ArrayList<Integer>> groups = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < setnum; i++)
groups.add(new ArrayList());
for (int i = 0; i < sets.length; i++)
groups.get(sets[i]).add(i);
//get min perimeter
int minX, minY, maxX, maxY;
int minP = Integer.MAX_VALUE;
Point currloc;
for (ArrayList<Integer> group: groups)
{
minX = locations[group.get(0)].x;
maxX = minX;
minY = locations[group.get(0)].y;
maxY = minY;
for (int cow: group)
{
currloc = locations[cow];
minX = Math.min(currloc.x, minX);
maxX = Math.max(currloc.x, maxX);
minY = Math.min(currloc.y, minY);
maxY = Math.max(currloc.y, maxY);
}
if (2*(maxX - minX + maxY - minY) < minP)
minP = 2*(maxX - minX + maxY - minY);
}
//System.out.println(minP);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("fenceplan.out")));
pw.println(minP);
pw.close();
br.close();
}
public static void bfs(int node, int setnum)
{
if (sets[node] == setnum) // if we've already been here return
return;
sets[node] = setnum; //or else claim it in the set
for (int neighbor: AdjList.get(node))
bfs(neighbor, setnum);
}
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public Point() {}
}
}