Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 625e6bd

Browse files
Update Course 3
1 parent b8de30b commit 625e6bd

12 files changed

+1475
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import java.util.Scanner;
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.PriorityQueue;
5+
6+
public class DistPreprocessLarge {
7+
private static class Impl {
8+
// See the descriptions of these fields in the starter for friend_suggestion
9+
int n;
10+
ArrayList<Integer>[][] adj;
11+
ArrayList<Long>[][] cost;
12+
Long[][] distance;
13+
ArrayList<PriorityQueue<Entry>> queue;
14+
boolean[] visited;
15+
ArrayList<Integer> workset;
16+
final Long INFINITY = Long.MAX_VALUE / 4;
17+
18+
// Position of the node in the node ordering
19+
Integer[] rank;
20+
// Level of the node for level heuristic in the node ordering
21+
Long[] level;
22+
23+
Impl(int n) {
24+
this.n = n;
25+
visited = new boolean[n];
26+
Arrays.fill(visited, false);
27+
workset = new ArrayList<Integer>();
28+
rank = new Integer[n];
29+
level = new Long[n];
30+
distance = new Long[][] {new Long[n], new Long[n]};
31+
for (int i = 0; i < n; ++i) {
32+
distance[0][i] = distance[1][i] = INFINITY;
33+
level[i] = 0L;
34+
rank[i] = 0;
35+
}
36+
queue = new ArrayList<PriorityQueue<Entry>>();
37+
queue.add(new PriorityQueue<Entry>(n));
38+
queue.add(new PriorityQueue<Entry>(n));
39+
}
40+
41+
// Preprocess the graph
42+
void preprocess() {
43+
// This priority queue will contain pairs (importance, node) with the least important node in the head
44+
PriorityQueue<Entry> q = new PriorityQueue<Entry>(n);
45+
// Implement this method yourself
46+
}
47+
48+
void add_edge(int side, int u, int v, Long c) {
49+
for (int i = 0; i < adj[side][u].size(); ++i) {
50+
int w = adj[side][u].get(i);
51+
if (w == v) {
52+
Long cc = min(cost[side][u].get(i), c);
53+
cost[side][u].set(i, cc);
54+
return;
55+
}
56+
}
57+
adj[side][u].add(v);
58+
cost[side][u].add(c);
59+
}
60+
61+
void apply_shortcut(Shortcut sc) {
62+
add_edge(0, sc.u, sc.v, sc.cost);
63+
add_edge(1, sc.v, sc.u, sc.cost);
64+
}
65+
66+
void clear() {
67+
for (int v : workset) {
68+
distance[0][v] = distance[1][v] = INFINITY;
69+
visited[v] = false;
70+
}
71+
workset.clear();
72+
queue.get(0).clear();
73+
queue.get(1).clear();
74+
}
75+
76+
void mark_visited(int u) {
77+
visited[u] = true;
78+
workset.add(u);
79+
}
80+
81+
// See the description of this method in the starter for friend_suggestion
82+
boolean visit(int side, int v, Long dist) {
83+
// Implement this method yourself
84+
return false;
85+
}
86+
87+
// Add the shortcuts corresponding to contracting node v. Return v's importance.
88+
Long shortcut(int v) {
89+
// Implement this method yourself
90+
91+
// Compute the node importance in the end
92+
Long shortcuts = 0;
93+
Long vlevel = 0L;
94+
Long neighbors = 0L;
95+
Long shortcutCover = 0L;
96+
// Compute the correct values for the above heuristics before computing the node importance
97+
Long importance = (shortcuts - adj[0][v].size() - adj[1][v].size()) + neighbors + shortcutCover + vlevel;
98+
return importance;
99+
}
100+
101+
// Returns the distance from s to t in the graph
102+
Long query(int s, int t) {
103+
if (s == t) {
104+
return 0L;
105+
}
106+
visit(0, s, 0L);
107+
visit(1, t, 0L);
108+
Long estimate = INFINITY;
109+
// Implement the rest of the algorithm yourself
110+
return estimate == INFINITY ? -1 : estimate;
111+
}
112+
113+
class Entry implements Comparable<Entry>
114+
{
115+
Long cost;
116+
int node;
117+
118+
public Entry(Long cost, int node)
119+
{
120+
this.cost = cost;
121+
this.node = node;
122+
}
123+
124+
public int compareTo(Entry other)
125+
{
126+
if (cost == other.cost) {
127+
return node < other.node ? -1 : node > other.node ? 1: 0;
128+
}
129+
return cost < other.cost ? -1 : cost > other.cost ? 1 : 0;
130+
}
131+
}
132+
133+
class Shortcut
134+
{
135+
int u;
136+
int v;
137+
Long cost;
138+
139+
public Shortcut(int u, int v, Long c)
140+
{
141+
this.u = u;
142+
this.v = v;
143+
cost = c;
144+
}
145+
}
146+
}
147+
148+
public static void main(String args[]) {
149+
Scanner in = new Scanner(System.in);
150+
int n = in.nextInt();
151+
int m = in.nextInt();
152+
Impl ch = new Impl(n);
153+
@SuppressWarnings("unchecked")
154+
ArrayList<Integer>[][] tmp1 = (ArrayList<Integer>[][])new ArrayList[2][];
155+
ch.adj = tmp1;
156+
@SuppressWarnings("unchecked")
157+
ArrayList<Long>[][] tmp2 = (ArrayList<Long>[][])new ArrayList[2][];
158+
ch.cost = tmp2;
159+
for (int side = 0; side < 2; ++side) {
160+
@SuppressWarnings("unchecked")
161+
ArrayList<Integer>[] tmp3 = (ArrayList<Integer>[])new ArrayList[n];
162+
ch.adj[side] = tmp3;
163+
@SuppressWarnings("unchecked")
164+
ArrayList<Long>[] tmp4 = (ArrayList<Long>[])new ArrayList[n];
165+
ch.cost[side] = tmp4;
166+
for (int i = 0; i < n; i++) {
167+
ch.adj[side][i] = new ArrayList<Integer>();
168+
ch.cost[side][i] = new ArrayList<Long>();
169+
}
170+
}
171+
172+
for (int i = 0; i < m; i++) {
173+
int x, y;
174+
Long c;
175+
x = in.nextInt();
176+
y = in.nextInt();
177+
c = in.nextLong();
178+
ch.adj[0][x - 1].add(y - 1);
179+
ch.cost[0][x - 1].add(c);
180+
ch.adj[1][y - 1].add(x - 1);
181+
ch.cost[1][y - 1].add(c);
182+
}
183+
184+
ch.preprocess();
185+
System.out.println("Ready");
186+
187+
int t = in.nextInt();
188+
189+
for (int i = 0; i < t; i++) {
190+
int u, v;
191+
u = in.nextInt();
192+
v = in.nextInt();
193+
System.out.println(ch.query(u-1, v-1));
194+
}
195+
}
196+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import java.util.Scanner;
2+
import java.util.ArrayList;
3+
import java.util.Arrays;
4+
import java.util.PriorityQueue;
5+
6+
public class DistPreprocessSmall {
7+
private static class Impl {
8+
// See the descriptions of these fields in the starter for friend_suggestion
9+
int n;
10+
ArrayList<Integer>[][] adj;
11+
ArrayList<Long>[][] cost;
12+
Long[][] distance;
13+
ArrayList<PriorityQueue<Entry>> queue;
14+
boolean[] visited;
15+
ArrayList<Integer> workset;
16+
final Long INFINITY = Long.MAX_VALUE / 4;
17+
18+
// Position of the node in the node ordering
19+
Integer[] rank;
20+
// Level of the node for level heuristic in the node ordering
21+
Long[] level;
22+
23+
Impl(int n) {
24+
this.n = n;
25+
visited = new boolean[n];
26+
Arrays.fill(visited, false);
27+
workset = new ArrayList<Integer>();
28+
rank = new Integer[n];
29+
level = new Long[n];
30+
distance = new Long[][] {new Long[n], new Long[n]};
31+
for (int i = 0; i < n; ++i) {
32+
distance[0][i] = distance[1][i] = INFINITY;
33+
level[i] = 0L;
34+
rank[i] = 0;
35+
}
36+
queue = new ArrayList<PriorityQueue<Entry>>();
37+
queue.add(new PriorityQueue<Entry>(n));
38+
queue.add(new PriorityQueue<Entry>(n));
39+
}
40+
41+
// Preprocess the graph
42+
void preprocess() {
43+
// This priority queue will contain pairs (importance, node) with the least important node in the head
44+
PriorityQueue<Entry> q = new PriorityQueue<Entry>(n);
45+
// Implement this method yourself
46+
}
47+
48+
void add_edge(int side, int u, int v, Long c) {
49+
for (int i = 0; i < adj[side][u].size(); ++i) {
50+
int w = adj[side][u].get(i);
51+
if (w == v) {
52+
Long cc = min(cost[side][u].get(i), c);
53+
cost[side][u].set(i, cc);
54+
return;
55+
}
56+
}
57+
adj[side][u].add(v);
58+
cost[side][u].add(c);
59+
}
60+
61+
void apply_shortcut(Shortcut sc) {
62+
add_edge(0, sc.u, sc.v, sc.cost);
63+
add_edge(1, sc.v, sc.u, sc.cost);
64+
}
65+
66+
void clear() {
67+
for (int v : workset) {
68+
distance[0][v] = distance[1][v] = INFINITY;
69+
visited[v] = false;
70+
}
71+
workset.clear();
72+
queue.get(0).clear();
73+
queue.get(1).clear();
74+
}
75+
76+
void mark_visited(int u) {
77+
visited[u] = true;
78+
workset.add(u);
79+
}
80+
81+
// See the description of this method in the starter for friend_suggestion
82+
boolean visit(int side, int v, Long dist) {
83+
// Implement this method yourself
84+
return false;
85+
}
86+
87+
// Add the shortcuts corresponding to contracting node v. Return v's importance.
88+
Long shortcut(int v) {
89+
// Implement this method yourself
90+
91+
// Compute the node importance in the end
92+
Long shortcuts = 0;
93+
Long vlevel = 0L;
94+
Long neighbors = 0L;
95+
Long shortcutCover = 0L;
96+
// Compute the correct values for the above heuristics before computing the node importance
97+
Long importance = (shortcuts - adj[0][v].size() - adj[1][v].size()) + neighbors + shortcutCover + vlevel;
98+
return importance;
99+
}
100+
101+
// Returns the distance from s to t in the graph
102+
Long query(int s, int t) {
103+
if (s == t) {
104+
return 0L;
105+
}
106+
visit(0, s, 0L);
107+
visit(1, t, 0L);
108+
Long estimate = INFINITY;
109+
// Implement the rest of the algorithm yourself
110+
return estimate == INFINITY ? -1 : estimate;
111+
}
112+
113+
class Entry implements Comparable<Entry>
114+
{
115+
Long cost;
116+
int node;
117+
118+
public Entry(Long cost, int node)
119+
{
120+
this.cost = cost;
121+
this.node = node;
122+
}
123+
124+
public int compareTo(Entry other)
125+
{
126+
if (cost == other.cost) {
127+
return node < other.node ? -1 : node > other.node ? 1: 0;
128+
}
129+
return cost < other.cost ? -1 : cost > other.cost ? 1 : 0;
130+
}
131+
}
132+
133+
class Shortcut
134+
{
135+
int u;
136+
int v;
137+
Long cost;
138+
139+
public Shortcut(int u, int v, Long c)
140+
{
141+
this.u = u;
142+
this.v = v;
143+
cost = c;
144+
}
145+
}
146+
}
147+
148+
public static void main(String args[]) {
149+
Scanner in = new Scanner(System.in);
150+
int n = in.nextInt();
151+
int m = in.nextInt();
152+
Impl ch = new Impl(n);
153+
@SuppressWarnings("unchecked")
154+
ArrayList<Integer>[][] tmp1 = (ArrayList<Integer>[][])new ArrayList[2][];
155+
ch.adj = tmp1;
156+
@SuppressWarnings("unchecked")
157+
ArrayList<Long>[][] tmp2 = (ArrayList<Long>[][])new ArrayList[2][];
158+
ch.cost = tmp2;
159+
for (int side = 0; side < 2; ++side) {
160+
@SuppressWarnings("unchecked")
161+
ArrayList<Integer>[] tmp3 = (ArrayList<Integer>[])new ArrayList[n];
162+
ch.adj[side] = tmp3;
163+
@SuppressWarnings("unchecked")
164+
ArrayList<Long>[] tmp4 = (ArrayList<Long>[])new ArrayList[n];
165+
ch.cost[side] = tmp4;
166+
for (int i = 0; i < n; i++) {
167+
ch.adj[side][i] = new ArrayList<Integer>();
168+
ch.cost[side][i] = new ArrayList<Long>();
169+
}
170+
}
171+
172+
for (int i = 0; i < m; i++) {
173+
int x, y;
174+
Long c;
175+
x = in.nextInt();
176+
y = in.nextInt();
177+
c = in.nextLong();
178+
ch.adj[0][x - 1].add(y - 1);
179+
ch.cost[0][x - 1].add(c);
180+
ch.adj[1][y - 1].add(x - 1);
181+
ch.cost[1][y - 1].add(c);
182+
}
183+
184+
ch.preprocess();
185+
System.out.println("Ready");
186+
187+
int t = in.nextInt();
188+
189+
for (int i = 0; i < t; i++) {
190+
int u, v;
191+
u = in.nextInt();
192+
v = in.nextInt();
193+
System.out.println(ch.query(u-1, v-1));
194+
}
195+
}
196+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /