├── README.md ├── week 1 ├── connected_components │ ├── ConnectedComponents.java │ ├── connected_components.cpp │ └── connected_components.py ├── graph_decomposition_problems_1.pdf └── reachability │ ├── Reachability.java │ ├── reachability.cpp │ └── reachability.py ├── week 2 ├── acyclicity │ ├── Acyclicity.java │ ├── acyclicity.cpp │ └── acyclicity.py ├── graph_decomposition_problems_2.pdf ├── strongly_connected │ ├── StronglyConnected.java │ ├── strongly_connected.cpp │ └── strongly_connected.py └── toposort │ ├── Toposort.java │ ├── toposort.cpp │ └── toposort.py ├── week 3 ├── bfs │ ├── BFS.java │ ├── bfs.cpp │ └── bfs.py ├── bipartite │ ├── Bipartite.java │ ├── bipartite.cpp │ └── bipartite.py └── paths_in_graphs_problems_1.pdf ├── week 4 ├── dijkstra │ ├── Dijkstra.java │ ├── dijkstra.cpp │ └── dijkstra.py ├── negative_cycle │ ├── NegativeCycle.java │ ├── negative_cycle.cpp │ └── negative_cycle.py ├── paths_in_graphs_problems_2.pdf └── shortest_paths │ ├── ShortestPaths.java │ ├── shortest_paths.cpp │ └── shortest_paths.py ├── week 5 ├── clustering │ ├── Clustering.java │ ├── clustering.cpp │ └── clustering.py ├── connecting_points │ ├── ConnectingPoints.java │ ├── connecting_points.cpp │ └── connecting_points.py └── spanning_trees_problems.pdf └── week 6 ├── Advanced-Shortest-Paths.pdf ├── dist_with_coords ├── DistWithCoords.java ├── dist_with_coords.cpp ├── dist_with_coords.py3 └── tests │ ├── 01 │ ├── 01.a │ ├── 02 │ ├── 02.a │ ├── 03 │ └── 03.a └── friend_suggestion ├── FriendSuggestion.java ├── friend_suggestion.cpp └── friend_suggestion.py3 /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms on Graphs 2 | Assignments in Java, C++, Python for Algorithms on Graphs on Coursera 3 | 4 | Note: I don't have access to submitting my assignments. It's just for my personal learning purpose. 5 | 6 | ## Week 1 7 | [Study Notes](https://gist.github.com/akueisara/612c88a940b7e2b9c0d6a631df28375e) 8 | ### Programming Assignment 1: Decomposition of Graphs 9 | Problem: [Finding an Exit from a Maze](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%201/reachability)
10 | Problem: [Adding Exits to a Maze](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%201/connected_components)
11 | 12 | ## Week 2 13 | [Study Notes](https://gist.github.com/akueisara/120d8d5b4e1a663c606987b00e6c3c15) 14 | ### Programming Assignment 2: Decomposition of Graphs 15 | Problem: [Checking Consistency of CS Curriculum](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%202/acyclicity)
16 | Problem: [Determining an Order of Courses](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%202/toposort)
17 | Advanced Problem: [Checking Whether Any Intersection in a City 18 | is Reachable from Any Other](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%202/strongly_connected)
19 | 20 | ## Week 3 21 | [Study Notes](https://gist.github.com/akueisara/4d274697d5553837a1973a42d31a2224) 22 | ### Programming Assignment 3: Paths in Graphs 23 | Problem: [Computing the Minimum Number of Flight Segments](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%203/bfs)
24 | Problem: [Checking whether a Graph is Bipartite](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%203/bipartite)
25 | 26 | ## Week 4 27 | [Study Notes](https://gist.github.com/akueisara/00e76a5552e41be21b6838cbfcea4b6c) 28 | ### Programming Assignment 4: Paths in Graphs 29 | Problem: [Computing the Minimum Cost of a Flight](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%204/dijkstra)
30 | Problem: [Detecting Anomalies in Currency Exchange Rates](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%204/negative_cycle)
31 | Advanced Problem: [Exchanging Money Optimally](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%204/shortest_paths)
32 | 33 | ## Week 5 34 | [Study Notes](https://gist.github.com/akueisara/42c7b82c43fb2a1ca78303b1624fa7c7) 35 | ### Programming Assignment 5: Minimum Spanning Trees 36 | Problem: [Building Roads to Connect Cities](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%205/connecting_points)
37 | Problem: [Clustering](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%205/clustering)
38 | 39 | ## Week 6 40 | ### Programming Assignment 6: Advanced Shortest Paths 41 | Problem: [Friend Suggestion](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%206/friend_suggestion)
42 | Problem: [Compute Distance Faster Using Coordinates](https://github.com/akueisara/algorithms-on-graphs/tree/master/week%206/dist_with_coords)
-------------------------------------------------------------------------------- /week 1/connected_components/ConnectedComponents.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class ConnectedComponents { 5 | private static int numberOfComponents(ArrayList[] adj) { 6 | int result = 0; 7 | //write your code here 8 | int[] visited = new int[adj.length]; 9 | for (int i = 0; i < adj.length; i++) { 10 | if (visited[i] == 0) { 11 | explore(adj, i, visited); 12 | result++; 13 | } 14 | } 15 | return result; 16 | } 17 | 18 | private static void explore(ArrayList[] adj, int x, int[] visited) { 19 | visited[x] = 1; 20 | for (int i = 0; i < adj[x].size(); i++) { 21 | if (visited[adj[x].get(i)] == 0) 22 | explore(adj, adj[x].get(i), visited); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner scanner = new Scanner(System.in); 28 | int n = scanner.nextInt(); 29 | int m = scanner.nextInt(); 30 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 31 | for (int i = 0; i < n; i++) { 32 | adj[i] = new ArrayList(); 33 | } 34 | for (int i = 0; i < m; i++) { 35 | int x, y; 36 | x = scanner.nextInt(); 37 | y = scanner.nextInt(); 38 | adj[x - 1].add(y - 1); 39 | adj[y - 1].add(x - 1); 40 | } 41 | System.out.println(numberOfComponents(adj)); 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /week 1/connected_components/connected_components.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | void explore(vector > &adj, int x, vector &visited) { 8 | visited[x] = 1; 9 | for (int i = 0; i < adj[x].size(); i++) { 10 | if (!visited[adj[x][i]]) 11 | explore(adj, adj[x][i], visited); 12 | } 13 | } 14 | 15 | int number_of_components(vector > &adj) { 16 | int res = 0; 17 | //write your code here 18 | vector visited(adj.size()); 19 | for (int i = 0; i < adj.size(); i++) 20 | { 21 | if (!visited[i]) 22 | { 23 | explore(adj, i, visited); 24 | res++; 25 | } 26 | } 27 | return res; 28 | } 29 | 30 | int main() { 31 | size_t n, m; 32 | std::cin >> n >> m; 33 | vector > adj(n, vector()); 34 | for (size_t i = 0; i < m; i++) { 35 | int x, y; 36 | std::cin >> x >> y; 37 | adj[x - 1].push_back(y - 1); 38 | adj[y - 1].push_back(x - 1); 39 | } 40 | std::cout << number_of_components(adj); 41 | } 42 | -------------------------------------------------------------------------------- /week 1/connected_components/connected_components.py: -------------------------------------------------------------------------------- 1 | def number_of_components(adj): 2 | result = 0 3 | #write your code here 4 | visited = [0] * len(adj) 5 | for i in xrange(len(adj)): 6 | if not visited[i]: 7 | explore(adj, i, visited) 8 | result += 1 9 | return result 10 | 11 | def explore(adj, x, visited): 12 | visited[x] = 1 13 | for i in xrange(len(adj[x])): 14 | if not visited[adj[x][i]]: 15 | explore(adj, adj[x][i], visited) 16 | 17 | if __name__ == '__main__': 18 | n, m = map(int, raw_input().split()) 19 | adj = [[] for _ in range(n)] 20 | for i in range(m): 21 | a, b = map(int, raw_input().split()) 22 | # adjacency list 23 | adj[a - 1].append(b - 1) 24 | adj[b - 1].append(a - 1) 25 | print(number_of_components(adj)) 26 | -------------------------------------------------------------------------------- /week 1/graph_decomposition_problems_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 1/graph_decomposition_problems_1.pdf -------------------------------------------------------------------------------- /week 1/reachability/Reachability.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Reachability { 5 | private static int reach(ArrayList[] adj, int x, int y) { 6 | //write your code here 7 | int[] visited = new int[adj.length]; 8 | return explore(adj, x, y, visited); 9 | } 10 | 11 | private static int explore(ArrayList[] adj, int x, int y, int[] visited) { 12 | if (x == y) { 13 | return 1; 14 | } 15 | visited[x] = 1; 16 | for (int i = 0; i < adj[x].size(); i++) { 17 | if (visited[adj[x].get(i)] == 0) { 18 | if(explore(adj, adj[x].get(i), y, visited) == 1) { 19 | return 1; 20 | } 21 | } 22 | } 23 | return 0; 24 | } 25 | 26 | public static void main(String[] args) { 27 | Scanner scanner = new Scanner(System.in); 28 | int n = scanner.nextInt(); 29 | int m = scanner.nextInt(); 30 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 31 | for (int i = 0; i < n; i++) { 32 | adj[i] = new ArrayList(); 33 | } 34 | for (int i = 0; i < m; i++) { 35 | int x, y; 36 | x = scanner.nextInt(); 37 | y = scanner.nextInt(); 38 | // adjacency list 39 | adj[x - 1].add(y - 1); 40 | adj[y - 1].add(x - 1); 41 | } 42 | int x = scanner.nextInt() - 1; 43 | int y = scanner.nextInt() - 1; 44 | System.out.println(reach(adj, x, y)); 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /week 1/reachability/reachability.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | int explore(vector > &adj, int x, int y, vector &visited) { 8 | if (x == y) { 9 | return 1; 10 | } 11 | visited[x] = 1; 12 | for(int i = 0; i < adj[x].size(); i++) { 13 | if (!visited[adj[x][i]]) { 14 | if(explore(adj, adj[x][i], y, visited)) { 15 | return 1; 16 | } 17 | } 18 | } 19 | return 0; 20 | } 21 | 22 | 23 | int reach(vector > &adj, int x, int y) { 24 | //write your code here 25 | vector visited(adj.size()); 26 | return explore(adj, x, y, visited); 27 | } 28 | 29 | int main() { 30 | size_t n, m; 31 | std::cin >> n >> m; 32 | vector > adj(n, vector()); 33 | for (size_t i = 0; i < m; i++) { 34 | int x, y; 35 | std::cin >> x >> y; 36 | // adjacency list 37 | adj[x - 1].push_back(y - 1); 38 | adj[y - 1].push_back(x - 1); 39 | } 40 | int x, y; 41 | std::cin >> x >> y; 42 | std::cout << reach(adj, x - 1, y - 1); 43 | } 44 | -------------------------------------------------------------------------------- /week 1/reachability/reachability.py: -------------------------------------------------------------------------------- 1 | def reach(adj, x, y): 2 | #write your code here 3 | visited = [0] * len(adj) 4 | return explore(adj, x, y, visited) 5 | 6 | def explore(adj, x, y, visited): 7 | if (x == y): 8 | return 1 9 | visited[x] = 1 10 | for i in xrange(len(adj[x])): 11 | if (not visited[adj[x][i]]): 12 | if(explore(adj, adj[x][i], y, visited)): 13 | return 1 14 | return 0 15 | 16 | if __name__ == '__main__': 17 | n, m = map(int, raw_input().split()) 18 | adj = [[] for _ in range(n)] 19 | for i in range(m): 20 | a, b = map(int, raw_input().split()) 21 | # adjacency list 22 | adj[a - 1].append(b - 1) 23 | adj[b - 1].append(a - 1) 24 | x, y = map(int, raw_input().split()) 25 | print(reach(adj, x-1, y-1)) -------------------------------------------------------------------------------- /week 2/acyclicity/Acyclicity.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class Acyclicity { 5 | private static int acyclic(ArrayList[] adj) { 6 | // Mark all the vertices as not visited and not part of recursion stack 7 | int[] visited = new int[adj.length]; 8 | int[] recStack = new int[adj.length]; 9 | // Call the recursive helper function to detect cycle in different DFS trees 10 | for (int i = 0; i < adj.length; i++) { 11 | if (visited[i] == 0) { 12 | if (dfs(adj, i, visited, recStack) == 1) 13 | return 1; 14 | } 15 | } 16 | return 0; 17 | } 18 | 19 | private static int dfs(ArrayList[] adj, int x, int[] visited, int[] recStack) { 20 | // Mark the current node as visited and part of recursion stack 21 | visited[x] = 1; 22 | recStack[x] = 1; 23 | // Recur for all the vertices adjacent to this vertex 24 | for (int i = 0; i < adj[x].size(); i++) { 25 | if (visited[adj[x].get(i)] == 0 && dfs(adj, adj[x].get(i), visited, recStack) == 1) 26 | return 1; 27 | else if(recStack[adj[x].get(i)] == 1) 28 | return 1; 29 | } 30 | recStack[x] = 0; // remove the vertex from recursion stack 31 | return 0; 32 | } 33 | 34 | public static void main(String[] args) { 35 | Scanner scanner = new Scanner(System.in); 36 | int n = scanner.nextInt(); 37 | int m = scanner.nextInt(); 38 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 39 | for (int i = 0; i < n; i++) { 40 | adj[i] = new ArrayList(); 41 | } 42 | for (int i = 0; i < m; i++) { 43 | int x, y; 44 | x = scanner.nextInt(); 45 | y = scanner.nextInt(); 46 | adj[x - 1].add(y - 1); 47 | } 48 | System.out.println(acyclic(adj)); 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /week 2/acyclicity/acyclicity.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using std::vector; 5 | using std::pair; 6 | 7 | int dfs(vector > &adj, int x, vector &visited, vector &recStack) { 8 | // Mark the current node as visited and part of recursion stack 9 | visited[x] = 1; 10 | recStack[x] = 1; 11 | // Recur for all the vertices adjacent to this vertex 12 | for (int i = 0; i < adj[x].size(); i++) { 13 | if (!visited[adj[x][i]] && dfs(adj, adj[x][i], visited, recStack)) 14 | return 1; 15 | else if(recStack[adj[x][i]]) 16 | return 1; 17 | } 18 | recStack[x] = 0; // remove the vertex from recursion stack 19 | return 0; 20 | } 21 | 22 | int acyclic(vector > &adj) { 23 | // Mark all the vertices as not visited and not part of recursion stack 24 | vector visited(adj.size()); 25 | vector recStack(adj.size()); 26 | for (int i = 0; i < adj.size(); i++) 27 | { 28 | if (!visited[i]) { 29 | if (dfs(adj, i, visited, recStack)) 30 | return 1; 31 | } 32 | } 33 | return 0; 34 | } 35 | 36 | int main() { 37 | size_t n, m; 38 | std::cin >> n >> m; 39 | vector > adj(n, vector()); 40 | for (size_t i = 0; i < m; i++) { 41 | int x, y; 42 | std::cin >> x >> y; 43 | adj[x - 1].push_back(y - 1); 44 | } 45 | std::cout << acyclic(adj); 46 | } 47 | -------------------------------------------------------------------------------- /week 2/acyclicity/acyclicity.py: -------------------------------------------------------------------------------- 1 | def acyclic(adj): 2 | # Mark all the vertices as not visited and not part of recursion stack 3 | visited = [0 for _ in range(len(adj))] 4 | rec_stack = [0 for _ in range(len(adj))] 5 | # Call the recursive helper function to detect cycle in different DFS trees 6 | for i in range(len(adj)): 7 | if not visited[i]: 8 | if dfs(adj, i, visited, rec_stack): 9 | return 1 10 | return 0 11 | 12 | def dfs(adj, x, visited, rec_stack): 13 | # Mark the current node as visited and part of recursion stack 14 | visited[x] = 1 15 | rec_stack[x] = 1 16 | # Recur for all the vertices adjacent to this vertex 17 | for i in range(len(adj[x])): 18 | if not visited[adj[x][i]] and dfs(adj, adj[x][i], visited, rec_stack): 19 | return 1 20 | elif rec_stack[adj[x][i]]: 21 | return 1 22 | rec_stack[x] = 0 # remove the vertex from recursion stack 23 | return 0 24 | 25 | if __name__ == '__main__': 26 | n, m = map(int, raw_input().split()) 27 | adj = [[] for _ in range(n)] 28 | for i in range(m): 29 | a, b = map(int, raw_input().split()) 30 | # adjacency list 31 | adj[a - 1].append(b - 1) 32 | print(acyclic(adj)) 33 | -------------------------------------------------------------------------------- /week 2/graph_decomposition_problems_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 2/graph_decomposition_problems_2.pdf -------------------------------------------------------------------------------- /week 2/strongly_connected/StronglyConnected.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | import java.util.Stack; 5 | 6 | public class StronglyConnected { 7 | private static int numberOfStronglyConnectedComponents(ArrayList[] adj) { 8 | int result = 0; 9 | Stack stack = new Stack<>(); 10 | 11 | // Mark all the vertices as not visited (For first DFS) 12 | int visited[] = new int[adj.length]; 13 | 14 | // Fill vertices in stack according to their finishing times 15 | for(int i = 0; i < adj.length; i++){ 16 | if(visited[i] == 0){ 17 | dfs(adj, i, visited, stack); 18 | } 19 | } 20 | 21 | // get the reversed adj list 22 | ArrayList[] rAdj = reverseEdges(adj); 23 | 24 | // Mark all the vertices as not visited (For second DFS) 25 | visited = new int[adj.length]; 26 | 27 | // Now process all vertices in order defined by Stack 28 | while (! stack.isEmpty()) { 29 | // Pop a vertex from stack 30 | int x = stack.pop(); 31 | 32 | // get one Strongly connected component of the popped vertex 33 | if(visited[x] == 0){ 34 | dfs(rAdj, x, visited, new Stack()); 35 | result++; 36 | } 37 | } 38 | return result; 39 | } 40 | 41 | private static void dfs(ArrayList[] adj, int x, int visited[], Stack stack){ 42 | // Mark the current node as visited 43 | visited[x] = 1; 44 | 45 | // Recur for all the vertices adjacent to this vertex 46 | for(int i = 0; i < adj[x].size(); i++){ 47 | if(visited[adj[x].get(i)] == 0){ 48 | visited[adj[x].get(i)] = 1; 49 | dfs(adj, adj[x].get(i), visited, stack); 50 | } 51 | } 52 | 53 | // All vertices reachable from x are processed by now, push x to Stack 54 | stack.push(x); 55 | } 56 | 57 | private static ArrayList[] reverseEdges(ArrayList[] adj){ 58 | ArrayList[] rAdj = new ArrayList[adj.length]; 59 | for(int i = 0; i < adj.length; i++){ 60 | rAdj[i] = new ArrayList(); 61 | } 62 | for(int i = 0; i < adj.length; i++){ 63 | // Recur for all the vertices adjacent to this vertex 64 | for(int j = 0; j < adj[i].size(); j++){ 65 | rAdj[adj[i].get(j)].add(i); 66 | } 67 | } 68 | return rAdj; 69 | } 70 | 71 | public static void main(String[] args) { 72 | Scanner scanner = new Scanner(System.in); 73 | int n = scanner.nextInt(); 74 | int m = scanner.nextInt(); 75 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 76 | for (int i = 0; i < n; i++) { 77 | adj[i] = new ArrayList(); 78 | } 79 | for (int i = 0; i < m; i++) { 80 | int x, y; 81 | x = scanner.nextInt(); 82 | y = scanner.nextInt(); 83 | adj[x - 1].add(y - 1); 84 | } 85 | System.out.println(numberOfStronglyConnectedComponents(adj)); 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /week 2/strongly_connected/strongly_connected.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::vector; 7 | using std::pair; 8 | using std::stack; 9 | 10 | vector > reverseEdges(vector > &adj){ 11 | vector > rAdj(adj.size(), vector()); 12 | for(int i = 0; i < adj.size(); i++){ 13 | // Recur for all the vertices adjacent to this vertex 14 | for(int j = 0; j < adj[i].size(); j++){ 15 | rAdj[adj[i][j]].push_back(i); 16 | } 17 | } 18 | return rAdj; 19 | } 20 | 21 | void dfs(vector > &adj, int x, vector &visited, stack &Stack) { 22 | // Mark the current node as visited 23 | visited[x] = 1; 24 | 25 | // Recur for all the vertices adjacent to this vertex 26 | for (int i = 0; i < adj[x].size(); i++) { 27 | if(!visited[adj[x][i]]){ 28 | visited[adj[x][i]] = 1; 29 | dfs(adj, adj[x][i], visited, Stack); 30 | } 31 | } 32 | 33 | // All vertices reachable from x are processed by now, push x to Stack 34 | Stack.push(x); 35 | } 36 | 37 | int number_of_strongly_connected_components(vector > adj) { 38 | int result = 0; 39 | stack Stack; 40 | 41 | // Mark all the vertices as not visited (For first DFS) 42 | vector visited(adj.size(), 0); 43 | 44 | // Fill vertices in stack according to their finishing times 45 | for (int i = 0; i < adj.size(); i++) { 46 | if(!visited[i]){ 47 | dfs(adj, i, visited, Stack); 48 | } 49 | } 50 | 51 | // get the reversed adj list 52 | vector > rAdj = reverseEdges(adj); 53 | 54 | // Mark all the vertices as not visited (For second DFS) 55 | for(int i = 0; i < adj.size(); i++) { 56 | visited[i] = 0; 57 | } 58 | 59 | // Now process all vertices in order defined by Stack 60 | while (! Stack.empty()) { 61 | // Pop a vertex from stack 62 | int x = Stack.top(); 63 | Stack.pop(); 64 | 65 | // get one Strongly connected component of the popped vertex 66 | if (!visited[x]) { 67 | stack componentStack; 68 | dfs(rAdj, x, visited, componentStack); 69 | result++; 70 | } 71 | } 72 | return result; 73 | } 74 | 75 | int main() { 76 | size_t n, m; 77 | std::cin >> n >> m; 78 | vector > adj(n, vector()); 79 | for (size_t i = 0; i < m; i++) { 80 | int x, y; 81 | std::cin >> x >> y; 82 | adj[x - 1].push_back(y - 1); 83 | } 84 | std::cout << number_of_strongly_connected_components(adj); 85 | } 86 | -------------------------------------------------------------------------------- /week 2/strongly_connected/strongly_connected.py: -------------------------------------------------------------------------------- 1 | def dfs(adj, x, visited, stack): 2 | # Mark the current node as visited 3 | visited[x] = 1 4 | 5 | # Recur for all the vertices adjacent to this vertex 6 | for i in range(len(adj[x])): 7 | if not visited[adj[x][i]]: 8 | visited[adj[x][i]] = 1 9 | dfs(adj, adj[x][i], visited, stack) 10 | 11 | # All vertices reachable from x are processed by now, push x to Stack 12 | stack.append(x) 13 | 14 | def reverseEdges(adj): 15 | r_adj = [[] for _ in range(len(adj))] 16 | for i in range(len(adj)): 17 | for j in range(len(adj[i])): 18 | r_adj[adj[i][j]].append(i) 19 | return r_adj 20 | 21 | def number_of_strongly_connected_components(adj): 22 | result = 0 23 | stack = [] 24 | 25 | # Mark all the vertices as not visited (For first DFS) 26 | visited = [0] * len(adj) 27 | 28 | # Fill vertices in stack according to their finishing times 29 | for i in range(len(adj)): 30 | if not visited[i]: 31 | dfs(adj, i, visited, stack) 32 | 33 | # get the reversed adj list 34 | r_adj = reverseEdges(adj) 35 | 36 | # Mark all the vertices as not visited (For second DFS) 37 | visited = [0] * len(adj) 38 | 39 | # Now process all vertices in order defined by Stack 40 | while stack: 41 | x = stack.pop() 42 | if not visited[x]: 43 | dfs(r_adj, x, visited, []) 44 | result+=1 45 | return result 46 | 47 | if __name__ == '__main__': 48 | n, m = map(int, raw_input().split()) 49 | adj = [[] for _ in range(n)] 50 | for i in range(m): 51 | a, b = map(int, raw_input().split()) 52 | # adjacency list 53 | adj[a - 1].append(b - 1) 54 | print(number_of_strongly_connected_components(adj)) 55 | -------------------------------------------------------------------------------- /week 2/toposort/Toposort.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Collections; 3 | import java.util.Scanner; 4 | 5 | public class Toposort { 6 | private static ArrayList toposort(ArrayList[] adj) { 7 | int used[] = new int[adj.length]; 8 | ArrayList order = new ArrayList(); 9 | for (int i = 0; i < adj.length; i++) { 10 | if (used[i] == 0) { 11 | dfs(adj, used, order, i); 12 | } 13 | } 14 | return order; 15 | } 16 | 17 | private static void dfs(ArrayList[] adj, int[] used, ArrayList order, int s) { 18 | used[s] = 1; 19 | for (int i = 0; i < adj[s].size(); i++) { 20 | if (used[adj[s].get(i)] == 0) { 21 | dfs(adj, used, order, adj[s].get(i)); 22 | } 23 | } 24 | order.add(0, s); 25 | } 26 | 27 | public static void main(String[] args) { 28 | Scanner scanner = new Scanner(System.in); 29 | int n = scanner.nextInt(); 30 | int m = scanner.nextInt(); 31 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 32 | for (int i = 0; i < n; i++) { 33 | adj[i] = new ArrayList(); 34 | } 35 | for (int i = 0; i < m; i++) { 36 | int x, y; 37 | x = scanner.nextInt(); 38 | y = scanner.nextInt(); 39 | adj[x - 1].add(y - 1); 40 | } 41 | ArrayList order = toposort(adj); 42 | for (int x : order) { 43 | System.out.print((x + 1) + " "); 44 | } 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /week 2/toposort/toposort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::pair; 7 | 8 | void dfs(vector > &adj, vector &used, vector &order, int x) { 9 | used[x] = 1; 10 | for (int i = 0; i < adj[x].size(); i++) { 11 | if(!used[adj[x][i]]) 12 | dfs(adj, used, order, adj[x][i]); 13 | } 14 | order.insert(order.begin(), x); 15 | } 16 | 17 | vector toposort(vector > adj) { 18 | vector used(adj.size(), 0); 19 | vector order; 20 | for (int i = 0; i < adj.size(); i++) { 21 | if (!used[i]) { 22 | dfs(adj, used, order, i); 23 | } 24 | } 25 | return order; 26 | } 27 | 28 | int main() { 29 | size_t n, m; 30 | std::cin >> n >> m; 31 | vector > adj(n, vector()); 32 | for (size_t i = 0; i < m; i++) { 33 | int x, y; 34 | std::cin >> x >> y; 35 | adj[x - 1].push_back(y - 1); 36 | } 37 | vector order = toposort(adj); 38 | for (size_t i = 0; i < order.size(); i++) { 39 | std::cout << order[i] + 1 << " "; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /week 2/toposort/toposort.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | 3 | import sys 4 | 5 | def dfs(adj, used, order, x): 6 | used[x] = 1 7 | for i in range(len(adj[x])): 8 | if not used[adj[x][i]]: 9 | dfs(adj, used, order, adj[x][i]) 10 | order.append(x) 11 | 12 | 13 | def toposort(adj): 14 | used = [0] * len(adj) 15 | order = [] 16 | for i in range(len(adj)): 17 | if not used[i]: 18 | dfs(adj, used, order, i) 19 | order.reverse() 20 | return order 21 | 22 | if __name__ == '__main__': 23 | n, m = map(int, raw_input().split()) 24 | adj = [[] for _ in range(n)] 25 | for i in range(m): 26 | a, b = map(int, raw_input().split()) 27 | # adjacency list 28 | adj[a - 1].append(b - 1) 29 | order = toposort(adj) 30 | for x in order: 31 | print x + 1, 32 | 33 | -------------------------------------------------------------------------------- /week 3/bfs/BFS.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | import java.util.Scanner; 5 | 6 | public class BFS { 7 | private static int distance(ArrayList[] adj, int s, int t) { 8 | //write your code here 9 | if (s == t) { 10 | return 0; 11 | } 12 | 13 | int[] dist = new int[adj.length]; 14 | for(int i=0; i queue = new LinkedList(); 22 | 23 | // Enqueue the current node 24 | queue.add(s); 25 | 26 | while (!queue.isEmpty()) { 27 | // Dequeue a vertex from queue 28 | int u = queue.poll(); 29 | 30 | // Get all adjacent vertices of the dequeued vertex u 31 | // If a adjacent has not been discovered, then enqueue it 32 | for (int v: adj[u]) { 33 | if (dist[v] == Integer.MAX_VALUE) { 34 | queue.add(v); 35 | dist[v] = dist[u] + 1; 36 | } 37 | } 38 | } 39 | 40 | if(dist[t] != Integer.MAX_VALUE) { 41 | return dist[t]; 42 | } 43 | 44 | return -1; 45 | } 46 | 47 | public static void main(String[] args) { 48 | Scanner scanner = new Scanner(System.in); 49 | int n = scanner.nextInt(); 50 | int m = scanner.nextInt(); 51 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 52 | for (int i = 0; i < n; i++) { 53 | adj[i] = new ArrayList(); 54 | } 55 | for (int i = 0; i < m; i++) { 56 | int x, y; 57 | x = scanner.nextInt(); 58 | y = scanner.nextInt(); 59 | adj[x - 1].add(y - 1); 60 | adj[y - 1].add(x - 1); 61 | } 62 | int x = scanner.nextInt() - 1; 63 | int y = scanner.nextInt() - 1; 64 | System.out.println(distance(adj, x, y)); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /week 3/bfs/bfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using std::vector; 8 | using std::queue; 9 | using std::numeric_limits; 10 | using std::queue; 11 | 12 | int distance(vector > &adj, int s, int t) { 13 | //write your code here 14 | if (s == t) { 15 | return 0; 16 | } 17 | 18 | vector dist(adj.size()); 19 | for(int i=0; i::max(); 21 | } 22 | 23 | dist[s] = 0; 24 | 25 | // Create a queue for BFS 26 | queue queue; 27 | 28 | // Enqueue the current node 29 | queue.push(s); 30 | 31 | while(!queue.empty()) { 32 | // Dequeue a vertex from queue 33 | int u = queue.front(); 34 | queue.pop(); 35 | 36 | // Get all adjacent vertices of the dequeued vertex u 37 | // If a adjacent has not been discovered, then enqueue it 38 | for(int i=0; i < adj[u].size(); ++i) { 39 | int v = adj[u][i]; 40 | if(dist[v] == numeric_limits::max()) { 41 | queue.push(v); 42 | dist[v] = dist[u] + 1; 43 | } 44 | } 45 | } 46 | 47 | if(dist[t] != numeric_limits::max()) { 48 | return dist[t]; 49 | } 50 | 51 | return -1; 52 | } 53 | 54 | int main() { 55 | int n, m; 56 | std::cin >> n >> m; 57 | vector > adj(n, vector()); 58 | for (int i = 0; i < m; i++) { 59 | int x, y; 60 | std::cin >> x >> y; 61 | adj[x - 1].push_back(y - 1); 62 | adj[y - 1].push_back(x - 1); 63 | } 64 | int s, t; 65 | std::cin >> s >> t; 66 | s--, t--; 67 | std::cout << distance(adj, s, t); 68 | } 69 | -------------------------------------------------------------------------------- /week 3/bfs/bfs.py: -------------------------------------------------------------------------------- 1 | def distance(adj, s, t): 2 | #write your code here 3 | dist = [len(adj)] * len(adj) 4 | dist[s] = 0 5 | queue = [] 6 | queue.append(s) 7 | while queue: 8 | u = queue.pop(0) 9 | for v in adj[u]: 10 | if dist[v] == len(adj): 11 | queue.append(v) 12 | dist[v] = dist[u] + 1 13 | if dist[t] != len(adj): 14 | return dist[t] 15 | return -1 16 | 17 | if __name__ == '__main__': 18 | n, m = map(int, raw_input().split()) 19 | adj = [[] for _ in range(n)] 20 | for i in range(m): 21 | a, b = map(int, raw_input().split()) 22 | # adjacency list 23 | adj[a - 1].append(b - 1) 24 | adj[b - 1].append(a - 1) 25 | s, t = map(int, raw_input().split()) 26 | s = s - 1 27 | t = t - 1 28 | print(distance(adj, s, t)) 29 | -------------------------------------------------------------------------------- /week 3/bipartite/Bipartite.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.LinkedList; 3 | import java.util.Queue; 4 | import java.util.Scanner; 5 | 6 | public class Bipartite { 7 | // This function returns 1 if the graph is Bipartite, else 0 8 | private static int bipartite(ArrayList[] adj) { 9 | //write your code here 10 | // Create a color array to store colors assigned to all veritces. 11 | // Vertex number is used as index in this array. The value '-1' 12 | // of colorArr[i] is used to indicate that no color is assigned 13 | // to vertex 'i'. The value 1 is used to indicate first color 14 | // is assigned and value 0 indicates second color is assigned. 15 | int[] colorArr = new int[adj.length]; 16 | for(int i=0; i queue = new LinkedList(); 26 | queue.add(0); 27 | 28 | // Run while there are vertices in queue (Similar to BFS) 29 | while (!queue.isEmpty()) { 30 | // Dequeue a vertex from queue 31 | int u = queue.poll(); 32 | 33 | // Find all non-colored adjacent vertices 34 | for (int v: adj[u]) { 35 | // Destination v is not colored 36 | if (colorArr[v] == -1) { 37 | // Assign alternate color to this adjacent v of u 38 | colorArr[v] = 1- colorArr[u]; 39 | queue.add(v); 40 | } 41 | 42 | // Destination v is colored with same color as u 43 | else if (colorArr[v] == colorArr[u]) 44 | return 0; 45 | } 46 | } 47 | // If we reach here, then all adjacent vertices can 48 | // be colored with alternate color 49 | return 1; 50 | } 51 | 52 | public static void main(String[] args) { 53 | Scanner scanner = new Scanner(System.in); 54 | int n = scanner.nextInt(); 55 | int m = scanner.nextInt(); 56 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 57 | for (int i = 0; i < n; i++) { 58 | adj[i] = new ArrayList(); 59 | } 60 | for (int i = 0; i < m; i++) { 61 | int x, y; 62 | x = scanner.nextInt(); 63 | y = scanner.nextInt(); 64 | adj[x - 1].add(y - 1); 65 | adj[y - 1].add(x - 1); 66 | } 67 | System.out.println(bipartite(adj)); 68 | } 69 | } 70 | 71 | -------------------------------------------------------------------------------- /week 3/bipartite/bipartite.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::queue; 7 | 8 | int bipartite(vector > &adj) { 9 | //write your code here 10 | // Create a color array to store colors assigned to all veritces. 11 | // Vertex number is used as index in this array. The value '-1' 12 | // of colorArr[i] is used to indicate that no color is assigned 13 | // to vertex 'i'. The value 1 is used to indicate first color 14 | // is assigned and value 0 indicates second color is assigned. 15 | int colorArr[adj.size()]; 16 | for (int i = 0; i < adj.size(); ++i) 17 | colorArr[i] = -1; 18 | 19 | // Assign first color to the source vertex 20 | colorArr[0] = 1; 21 | 22 | // Create a queue (FIFO) of vertex numbers and enqueue 23 | // source vertex for BFS traversal 24 | queue queue; 25 | queue.push(0); 26 | 27 | // Run while there are vertices in queue (Similar to BFS) 28 | while(!queue.empty()) { 29 | // Dequeue a vertex from queue 30 | int u = queue.front(); 31 | queue.pop(); 32 | 33 | // Find all non-colored adjacent vertices 34 | for(int i=0; i < adj[u].size(); ++i) { 35 | int v = adj[u][i]; 36 | // Destination v is not colored 37 | if (colorArr[v] == -1) { 38 | // Assign alternate color to this adjacent v of u 39 | colorArr[v] = 1- colorArr[u]; 40 | queue.push(v); 41 | } 42 | 43 | // Destination v is colored with same color as u 44 | else if (colorArr[v] == colorArr[u]) 45 | return 0; 46 | } 47 | } 48 | // If we reach here, then all adjacent vertices can 49 | // be colored with alternate color 50 | return 1; 51 | } 52 | 53 | int main() { 54 | int n, m; 55 | std::cin >> n >> m; 56 | vector > adj(n, vector()); 57 | for (int i = 0; i < m; i++) { 58 | int x, y; 59 | std::cin >> x >> y; 60 | adj[x - 1].push_back(y - 1); 61 | adj[y - 1].push_back(x - 1); 62 | } 63 | std::cout << bipartite(adj); 64 | } 65 | -------------------------------------------------------------------------------- /week 3/bipartite/bipartite.py: -------------------------------------------------------------------------------- 1 | def bipartite(adj): 2 | #write your code here 3 | color_arr = [-1] * len(adj) 4 | color_arr[0] = 1 5 | queue = [] 6 | queue.append(0) 7 | while queue: 8 | u = queue.pop(0) 9 | for v in adj[u]: 10 | if color_arr[v] == -1: 11 | color_arr[v] = 1 - color_arr[u] 12 | queue.append(v) 13 | elif color_arr[v] == color_arr[u]: 14 | return 0 15 | return 1 16 | 17 | if __name__ == '__main__': 18 | n, m = map(int, raw_input().split()) 19 | adj = [[] for _ in range(n)] 20 | for i in range(m): 21 | a, b = map(int, raw_input().split()) 22 | # adjacency list 23 | adj[a - 1].append(b - 1) 24 | adj[b - 1].append(a - 1) 25 | print(bipartite(adj)) 26 | -------------------------------------------------------------------------------- /week 3/paths_in_graphs_problems_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 3/paths_in_graphs_problems_1.pdf -------------------------------------------------------------------------------- /week 4/dijkstra/Dijkstra.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Dijkstra { 4 | 5 | private static final int inf = Integer.MAX_VALUE; 6 | 7 | public static class Node implements Comparable { 8 | int index; 9 | long distance; 10 | 11 | public Node(int index, long distance) { 12 | this.index = index; 13 | this.distance = distance; 14 | } 15 | 16 | @Override 17 | public int compareTo(Node o) { 18 | if (this.distance > o.distance) return 1; 19 | else if (this.distance < o.distance) return -1; 20 | else return 0; 21 | } 22 | } 23 | 24 | private static int distance(ArrayList[] adj, ArrayList[] cost, int s, int t) { 25 | //write your code here 26 | int[] dist = new int[adj.length]; 27 | for (int i = 0;i < dist.length;i++) { 28 | dist[i] = inf; 29 | } 30 | dist[s] = 0; 31 | PriorityQueue queue = new PriorityQueue(); 32 | queue.add(new Node(s, dist[s])); 33 | while(!queue.isEmpty()){ 34 | Node u = queue.remove(); 35 | int u_index = u.index; 36 | for (int v : adj[u_index]) { 37 | int v_index = adj[u_index].indexOf(v); 38 | if (dist[v] > dist[u_index] + cost[u_index].get(v_index)) { 39 | dist[v] = dist[u_index] + cost[u_index].get(v_index); 40 | queue.add(new Node(v, dist[v])); 41 | } 42 | } 43 | } 44 | if(dist[t] == inf) 45 | return -1; 46 | return dist[t]; 47 | } 48 | 49 | public static void main(String[] args) { 50 | Scanner scanner = new Scanner(System.in); 51 | int n = scanner.nextInt(); 52 | int m = scanner.nextInt(); 53 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 54 | ArrayList[] cost = (ArrayList[])new ArrayList[n]; 55 | for (int i = 0; i < n; i++) { 56 | adj[i] = new ArrayList(); 57 | cost[i] = new ArrayList(); 58 | } 59 | for (int i = 0; i < m; i++) { 60 | int x, y, w; 61 | x = scanner.nextInt(); 62 | y = scanner.nextInt(); 63 | w = scanner.nextInt(); 64 | adj[x - 1].add(y - 1); 65 | cost[x - 1].add(w); 66 | } 67 | int x = scanner.nextInt() - 1; 68 | int y = scanner.nextInt() - 1; 69 | System.out.println(distance(adj, cost, x, y)); 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /week 4/dijkstra/dijkstra.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::vector; 7 | using std::queue; 8 | using std::pair; 9 | using std::priority_queue; 10 | using std::numeric_limits; 11 | 12 | const int inf = numeric_limits::max(); 13 | 14 | struct Node { 15 | int index, distance; 16 | Node(int a = 0, int b = 0): index(a), distance(b) {} 17 | }; 18 | 19 | struct cmp { 20 | bool operator()(Node a, Node b){ 21 | return a.distance > b.distance; 22 | } 23 | }; 24 | 25 | int distance(vector > &adj, vector > &cost, int s, int t) { 26 | //write your code here 27 | vector dist(adj.size(), inf); 28 | dist[s] = 0; 29 | priority_queue, cmp> pq; 30 | pq.push(Node(s, dist[s])); 31 | while(!pq.empty()) { 32 | Node u = pq.top(); 33 | pq.pop(); 34 | int u_index = u.index; 35 | for (int i = 0; i < adj[u_index].size(); i++) { 36 | int v = adj[u_index][i]; 37 | if(dist[v] > dist[u_index] + cost[u_index][i]) { 38 | dist[v] = dist[u_index] + cost[u_index][i]; 39 | pq.push(Node(v, dist[v])); 40 | } 41 | } 42 | } 43 | if(dist[t] == inf) 44 | return -1; 45 | return dist[t]; 46 | } 47 | 48 | int main() { 49 | int n, m; 50 | std::cin >> n >> m; 51 | vector > adj(n, vector()); 52 | vector > cost(n, vector()); 53 | for (int i = 0; i < m; i++) { 54 | int x, y, w; 55 | std::cin >> x >> y >> w; 56 | adj[x - 1].push_back(y - 1); 57 | cost[x - 1].push_back(w); 58 | } 59 | int s, t; 60 | std::cin >> s >> t; 61 | s--, t--; 62 | std::cout << distance(adj, cost, s, t); 63 | } 64 | -------------------------------------------------------------------------------- /week 4/dijkstra/dijkstra.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | #import sys 3 | 4 | import Queue # change to import queue if using python 3 5 | 6 | class Node(object): 7 | def __init__(self, index, distance): 8 | self.index = index 9 | self.distance = distance 10 | def __cmp__(self, other): 11 | return cmp(self.distance, other.distance) 12 | 13 | def distance(adj, cost, s, t): 14 | #write your code here 15 | dist=[float('inf')]*len(adj) 16 | dist[s] = 0 17 | pq = Queue.PriorityQueue() 18 | pq.put(Node(s, dist[s])) 19 | while not pq.empty(): 20 | u = pq.get() 21 | u_index = u.index 22 | for v in adj[u_index]: 23 | v_index = adj[u_index].index(v) 24 | if dist[v] > dist[u_index] + cost[u_index][v_index]: 25 | dist[v] = dist[u_index] + cost[u_index][v_index] 26 | pq.put(Node(v, dist[v])) 27 | if dist[t] == float('inf'): 28 | return -1 29 | return dist[t] 30 | 31 | 32 | if __name__ == '__main__': 33 | n, m = map(int, raw_input().split()) 34 | adj = [[] for _ in range(n)] 35 | cost = [[] for _ in range(n)] 36 | for i in range(m): 37 | a, b, w = map(int, raw_input().split()) 38 | adj[a - 1].append(b - 1) 39 | cost[a - 1].append(w) 40 | s, t = map(int, raw_input().split()) 41 | s, t = s-1, t-1 42 | print(distance(adj, cost, s, t)) 43 | 44 | ''' 45 | input = sys.stdin.read() 46 | data = list(map(int, input.split())) 47 | n, m = data[0:2] 48 | data = data[2:] 49 | edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3])) 50 | data = data[3 * m:] 51 | adj = [[] for _ in range(n)] 52 | cost = [[] for _ in range(n)] 53 | for ((a, b), w) in edges: 54 | adj[a - 1].append(b - 1) 55 | cost[a - 1].append(w) 56 | s, t = data[0] - 1, data[1] - 1 57 | print(distance(adj, cost, s, t)) 58 | ''' 59 | -------------------------------------------------------------------------------- /week 4/negative_cycle/NegativeCycle.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Scanner; 3 | 4 | public class NegativeCycle { 5 | private static final int inf = Integer.MAX_VALUE; 6 | 7 | private static int negativeCycle(ArrayList[] adj, ArrayList[] cost) { 8 | // write your code here 9 | long[] dist = new long[adj.length]; 10 | for (int i = 0;i < adj.length; i++) { 11 | dist[i] = inf; 12 | } 13 | dist[0] = 0; 14 | for (int i = 0; i < adj.length; i++) { 15 | for(int u = 0; u < adj.length; u++){ 16 | for (int v : adj[u]) { 17 | int v_index = adj[u].indexOf(v); 18 | if (dist[v] > dist[u] + cost[u].get(v_index)) { 19 | dist[v] = dist[u] + cost[u].get(v_index); 20 | if(i == adj.length - 1) 21 | return 1; 22 | } 23 | } 24 | } 25 | } 26 | return 0; 27 | } 28 | 29 | public static void main(String[] args) { 30 | Scanner scanner = new Scanner(System.in); 31 | int n = scanner.nextInt(); 32 | int m = scanner.nextInt(); 33 | ArrayList[] adj = (ArrayList[])new ArrayList[n]; 34 | ArrayList[] cost = (ArrayList[])new ArrayList[n]; 35 | for (int i = 0; i < n; i++) { 36 | adj[i] = new ArrayList(); 37 | cost[i] = new ArrayList(); 38 | } 39 | for (int i = 0; i < m; i++) { 40 | int x, y, w; 41 | x = scanner.nextInt(); 42 | y = scanner.nextInt(); 43 | w = scanner.nextInt(); 44 | adj[x - 1].add(y - 1); 45 | cost[x - 1].add(w); 46 | } 47 | System.out.println(negativeCycle(adj, cost)); 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /week 4/negative_cycle/negative_cycle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using std::vector; 6 | using std::numeric_limits; 7 | 8 | const int inf = numeric_limits::max(); 9 | 10 | int negative_cycle(vector > &adj, vector > &cost) { 11 | //write your code here 12 | vector dist(adj.size(), inf); 13 | dist[0] = 0; 14 | for (int i = 0; i < adj.size(); i++) { 15 | for(int u = 0; u < adj.size(); u++){ 16 | for (int k = 0; k < adj[u].size(); k++) { 17 | int v = adj[u][k]; 18 | if(dist[u] != inf && dist[v] > dist[u] + cost[u][k]) { 19 | dist[v] = dist[u] + cost[u][k]; 20 | if(i == adj.size() - 1) 21 | return 1; 22 | } 23 | } 24 | } 25 | } 26 | return 0; 27 | } 28 | 29 | int main() { 30 | int n, m; 31 | std::cin >> n >> m; 32 | vector > adj(n, vector()); 33 | vector > cost(n, vector()); 34 | for (int i = 0; i < m; i++) { 35 | int x, y, w; 36 | std::cin >> x >> y >> w; 37 | adj[x - 1].push_back(y - 1); 38 | cost[x - 1].push_back(w); 39 | } 40 | std::cout << negative_cycle(adj, cost); 41 | } 42 | -------------------------------------------------------------------------------- /week 4/negative_cycle/negative_cycle.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | #import sys 3 | 4 | def negative_cycle(adj, cost): 5 | #write your code here 6 | dist=[float('inf')]*len(adj) 7 | dist[0] = 0 8 | for i in range(len(adj)): 9 | for u in range(len(adj)): 10 | for v in adj[u]: 11 | v_index = adj[u].index(v) 12 | if dist[v] > dist[u] + cost[u][v_index]: 13 | dist[v] = dist[u] + cost[u][v_index] 14 | if i == len(adj) - 1: 15 | return 1 16 | return 0 17 | 18 | 19 | if __name__ == '__main__': 20 | n, m = map(int, raw_input().split()) 21 | adj = [[] for _ in range(n)] 22 | cost = [[] for _ in range(n)] 23 | for i in range(m): 24 | a, b, w = map(int, raw_input().split()) 25 | adj[a - 1].append(b - 1) 26 | cost[a - 1].append(w) 27 | print(negative_cycle(adj, cost)) 28 | ''' 29 | input = sys.stdin.read() 30 | data = list(map(int, input.split())) 31 | n, m = data[0:2] 32 | data = data[2:] 33 | edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3])) 34 | data = data[3 * m:] 35 | adj = [[] for _ in range(n)] 36 | cost = [[] for _ in range(n)] 37 | for ((a, b), w) in edges: 38 | adj[a - 1].append(b - 1) 39 | cost[a - 1].append(w) 40 | print(negative_cycle(adj, cost)) 41 | ''' 42 | -------------------------------------------------------------------------------- /week 4/paths_in_graphs_problems_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 4/paths_in_graphs_problems_2.pdf -------------------------------------------------------------------------------- /week 4/shortest_paths/ShortestPaths.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ShortestPaths { 4 | 5 | private static void shortestPaths(ArrayList < Integer > [] adj, ArrayList < Integer > [] cost, int s, long[] distance, int[] reachable, int[] shortest) { 6 | //write your code here 7 | distance[s] = 0; 8 | reachable[s] = 1; 9 | Queue < Integer > queue = new LinkedList < > (); 10 | for (int i = 0; i < adj.length; i++) { 11 | for (int u = 0; u < adj.length; u++) { 12 | for (int v: adj[u]) { 13 | int v_index = adj[u].indexOf(v); 14 | if (distance[u] != Long.MAX_VALUE && distance[v] > distance[u] + cost[u].get(v_index)) { 15 | distance[v] = distance[u] + cost[u].get(v_index); 16 | reachable[v] = 1; 17 | if (i == adj.length - 1) { 18 | queue.add(v); 19 | } 20 | } 21 | } 22 | } 23 | } 24 | 25 | int[] visited = new int[adj.length]; 26 | while (!queue.isEmpty()) { 27 | int u = queue.remove(); 28 | visited[u] = 1; 29 | if (u != s) 30 | shortest[u] = 0; 31 | for (int v: adj[u]) { 32 | if (visited[v] == 0) { 33 | queue.add(v); 34 | visited[v] = 1; 35 | shortest[v] = 0; 36 | } 37 | } 38 | } 39 | distance[s] = 0; 40 | } 41 | 42 | public static void main(String[] args) { 43 | Scanner scanner = new Scanner(System.in); 44 | int n = scanner.nextInt(); 45 | int m = scanner.nextInt(); 46 | ArrayList < Integer > [] adj = (ArrayList < Integer > []) new ArrayList[n]; 47 | ArrayList < Integer > [] cost = (ArrayList < Integer > []) new ArrayList[n]; 48 | for (int i = 0; i < n; i++) { 49 | adj[i] = new ArrayList < Integer > (); 50 | cost[i] = new ArrayList < Integer > (); 51 | } 52 | for (int i = 0; i < m; i++) { 53 | int x, y, w; 54 | x = scanner.nextInt(); 55 | y = scanner.nextInt(); 56 | w = scanner.nextInt(); 57 | adj[x - 1].add(y - 1); 58 | cost[x - 1].add(w); 59 | } 60 | int s = scanner.nextInt() - 1; 61 | long distance[] = new long[n]; 62 | int reachable[] = new int[n]; 63 | int shortest[] = new int[n]; 64 | for (int i = 0; i < n; i++) { 65 | distance[i] = Long.MAX_VALUE; 66 | reachable[i] = 0; 67 | shortest[i] = 1; 68 | } 69 | shortestPaths(adj, cost, s, distance, reachable, shortest); 70 | for (int i = 0; i < n; i++) { 71 | if (reachable[i] == 0) { 72 | System.out.println('*'); 73 | } else if (shortest[i] == 0) { 74 | System.out.println('-'); 75 | } else { 76 | System.out.println(distance[i]); 77 | } 78 | } 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /week 4/shortest_paths/shortest_paths.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using std::vector; 7 | using std::queue; 8 | using std::pair; 9 | using std::priority_queue; 10 | 11 | void shortest_paths(vector > &adj, vector > &cost, int s, vector &distance, vector &reachable, vector &shortest) { 12 | //write your code here 13 | reachable[s] = 1; 14 | distance[s] = 0; 15 | queue q; 16 | for (int i = 0; i < adj.size(); i++) { 17 | for (int u = 0; u < adj.size(); u++){ 18 | for (int k = 0; k < adj[u].size(); k++) { 19 | int v = adj[u][k]; 20 | if (distance[u]!= std::numeric_limits::max() && distance[v] > distance[u] + cost[u][k]) { 21 | distance[v] = distance[u] + cost[u][k]; 22 | reachable[v] = 1; 23 | if (i == adj.size() - 1) { 24 | q.push(v); 25 | } 26 | } 27 | } 28 | } 29 | } 30 | 31 | vector visited(adj.size()); 32 | while (!q.empty()){ 33 | int u = q.front(); 34 | q.pop(); 35 | visited[u] = 1; 36 | // if(u != s) 37 | shortest[u] = 0; 38 | for (int i = 0; i < adj[u].size(); i++) { 39 | int v = adj[u][i]; 40 | if (!visited[v]) { 41 | q.push(v); 42 | visited[v] = 1; 43 | shortest[v] = 0; 44 | } 45 | } 46 | } 47 | distance[s] = 0; 48 | } 49 | 50 | int main() { 51 | int n, m, s; 52 | std::cin >> n >> m; 53 | vector > adj(n, vector()); 54 | vector > cost(n, vector()); 55 | for (int i = 0; i < m; i++) { 56 | int x, y, w; 57 | std::cin >> x >> y >> w; 58 | adj[x - 1].push_back(y - 1); 59 | cost[x - 1].push_back(w); 60 | } 61 | std::cin >> s; 62 | s--; 63 | vector distance(n, std::numeric_limits::max()); 64 | vector reachable(n, 0); 65 | vector shortest(n, 1); 66 | shortest_paths(adj, cost, s, distance, reachable, shortest); 67 | for (int i = 0; i < n; i++) { 68 | if (!reachable[i]) { 69 | std::cout << "*\n"; 70 | } else if (!shortest[i]) { 71 | std::cout << "-\n"; 72 | } else { 73 | std::cout << distance[i] << "\n"; 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /week 4/shortest_paths/shortest_paths.py: -------------------------------------------------------------------------------- 1 | #Uses python3 2 | #import sys 3 | #import queue 4 | 5 | #Uses python2 6 | import Queue 7 | 8 | 9 | def shortet_paths(adj, cost, s, distance, reachable, shortest): 10 | #write your code here 11 | distance[s] = 0 12 | reachable[s] = 1 13 | q = Queue.Queue() 14 | for i in range(len(adj)): 15 | for u in range(len(adj)): 16 | for v in adj[u]: 17 | v_index = adj[u].index(v) 18 | if distance[u] != 10**19 and distance[v] > distance[u] + cost[u][v_index]: 19 | distance[v] = distance[u] + cost[u][v_index] 20 | reachable[v] = 1 21 | if i == len(adj) - 1: 22 | q.put(v) 23 | 24 | visited = [0] * len(adj) 25 | while not q.empty(): 26 | u = q.get() 27 | visited[u] = 1 28 | if u != s: 29 | shortest[u] = 0 30 | for v in adj[u]: 31 | if visited[v] == 0: 32 | q.put(v) 33 | visited[v] = 1 34 | shortest[v] = 0 35 | distance[s] = 0 36 | 37 | if __name__ == '__main__': 38 | n, m = map(int, raw_input().split()) 39 | adj = [[] for _ in range(n)] 40 | cost = [[] for _ in range(n)] 41 | for i in range(m): 42 | a, b, w = map(int, raw_input().split()) 43 | adj[a - 1].append(b - 1) 44 | cost[a - 1].append(w) 45 | s = int(raw_input()) 46 | ''' 47 | input = sys.stdin.read() 48 | data = list(map(int, input.split())) 49 | n, m = data[0:2] 50 | data = data[2:] 51 | edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3])) 52 | data = data[3 * m:] 53 | adj = [[] for _ in range(n)] 54 | cost = [[] for _ in range(n)] 55 | for ((a, b), w) in edges: 56 | adj[a - 1].append(b - 1) 57 | cost[a - 1].append(w) 58 | s = data[0] 59 | ''' 60 | s -= 1 61 | distance = [10**19] * n 62 | reachable = [0] * n 63 | shortest = [1] * n 64 | shortet_paths(adj, cost, s, distance, reachable, shortest) 65 | for x in range(n): 66 | if reachable[x] == 0: 67 | print('*') 68 | elif shortest[x] == 0: 69 | print('-') 70 | else: 71 | print(distance[x]) 72 | -------------------------------------------------------------------------------- /week 5/clustering/Clustering.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.PriorityQueue; 3 | import java.util.Comparator; 4 | 5 | public class Clustering { 6 | private static class Node { 7 | int x; 8 | int y; 9 | int parent; 10 | int rank; 11 | 12 | public Node(int a, int b, int c) { 13 | x = a; 14 | y = b; 15 | parent = c; 16 | rank = 0; 17 | } 18 | } 19 | 20 | private static class Edge { 21 | int u; 22 | int v; 23 | double weight; 24 | 25 | public Edge(int a, int b, double c) { 26 | u = a; 27 | v = b; 28 | weight = c; 29 | } 30 | } 31 | 32 | private static void MakeSet(int i, Node[] nodes, int[] x, int[] y) { 33 | nodes[i] = new Node(x[i], y[i], i); 34 | } 35 | 36 | private static double Weight(int x1, int y1, int x2, int y2) { 37 | return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 38 | } 39 | 40 | private static int Find(int i, Node[] nodes) { 41 | if (i != nodes[i].parent) { 42 | nodes[i].parent = Find(nodes[i].parent, nodes); 43 | } 44 | return nodes[i].parent; 45 | } 46 | 47 | private static void Union(int u, int v, Node[] nodes) { 48 | int r1 = Find(u, nodes); 49 | int r2 = Find(v, nodes); 50 | if (r1 != r2) { 51 | if (nodes[r1].rank > nodes[r2].rank) { 52 | nodes[r2].parent = r1; 53 | } else { 54 | nodes[r1].parent = r2; 55 | if (nodes[r1].rank == nodes[r2].rank) { 56 | nodes[r2].rank++; 57 | } 58 | } 59 | } 60 | } 61 | 62 | private static double clustering(int[] x, int[] y, int k) { 63 | //write your code here 64 | int n = x.length; 65 | Node[] nodes = new Node[n]; 66 | for (int i = 0; i < n; i++) { 67 | MakeSet(i, nodes, x, y); 68 | } 69 | PriorityQueue edges = new PriorityQueue<>(new Comparator(){ 70 | @Override 71 | public int compare(Edge e1, Edge e2) { 72 | return e1.weight < e2.weight ? -1 : 1; 73 | } 74 | }); 75 | for (int i = 0; i < n; i++) { 76 | for (int j = i + 1; j < n; j++) { 77 | edges.offer(new Edge(i, j, Weight(x[i], y[i], x[j], y[j]))); 78 | } 79 | } 80 | int unionNum = 0; 81 | while(!edges.isEmpty()){ 82 | Edge currentEdge = edges.poll(); 83 | int u = currentEdge.u; 84 | int v = currentEdge.v; 85 | if(Find(u, nodes) != Find(v, nodes)) { 86 | unionNum++; 87 | Union(u, v, nodes); 88 | } 89 | if(unionNum > n - k) return currentEdge.weight; 90 | } 91 | return -1.; 92 | } 93 | 94 | public static void main(String[] args) { 95 | Scanner scanner = new Scanner(System.in); 96 | int n = scanner.nextInt(); 97 | int[] x = new int[n]; 98 | int[] y = new int[n]; 99 | for (int i = 0; i < n; i++) { 100 | x[i] = scanner.nextInt(); 101 | y[i] = scanner.nextInt(); 102 | } 103 | int k = scanner.nextInt(); 104 | System.out.println(clustering(x, y, k)); 105 | } 106 | } 107 | 108 | -------------------------------------------------------------------------------- /week 5/clustering/clustering.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using std::vector; 9 | using std::pair; 10 | 11 | struct node { 12 | int parent; 13 | int rank; 14 | int x; 15 | int y; 16 | 17 | node(int a, int b, int c = -1, int d = 0) { 18 | x = a; 19 | y = b; 20 | parent = c; 21 | rank = d; 22 | } 23 | }; 24 | 25 | struct edge { 26 | int u; 27 | int v; 28 | double weight; 29 | 30 | edge(int a, int b, double c) { 31 | u = a; 32 | v = b; 33 | weight = c; 34 | } 35 | }; 36 | 37 | bool cmp(edge a, edge b) { 38 | return a.weight < b.weight; 39 | } 40 | 41 | void make_set(int i, vector &nodes, vector &x, vector &y) { 42 | nodes.push_back(node(x[i], y[i], i)); 43 | } 44 | 45 | double weight(int x1, int y1, int x2, int y2) { 46 | return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 47 | } 48 | 49 | int Find(int i, vector &nodes) { 50 | if (i != nodes[i].parent) { 51 | nodes[i].parent = Find(nodes[i].parent, nodes); 52 | } 53 | return nodes[i].parent; 54 | } 55 | 56 | void Union(int u, int v, vector &nodes) { 57 | int r1 = Find(u, nodes); 58 | int r2 = Find(v, nodes); 59 | if (r1 != r2) { 60 | if (nodes[r1].rank > nodes[r2].rank) { 61 | nodes[r2].parent = r1; 62 | } else { 63 | nodes[r1].parent = r2; 64 | if (nodes[r1].rank == nodes[r2].rank) { 65 | nodes[r2].rank++; 66 | } 67 | } 68 | } 69 | } 70 | 71 | 72 | double clustering(vector x, vector y, int k) { 73 | //write your code here 74 | int n = x.size(); 75 | vector nodes; 76 | for (int i = 0; i < n; i++) { 77 | make_set(i, nodes, x, y); 78 | } 79 | vector edges; 80 | for (int i = 0; i < n; i++) { 81 | for (int j = i + 1; j < n; j++) { 82 | edges.push_back(edge(i, j, weight(x[i], y[i], x[j], y[j]))); 83 | } 84 | } 85 | std::sort(edges.begin(), edges.end(), cmp); 86 | int union_num = 0; 87 | for (int i = 0; i < edges.size(); i++) { 88 | edge current_edge = edges[i]; 89 | int u = current_edge.u; 90 | int v = current_edge.v; 91 | if (Find(u, nodes) != Find(v, nodes)) { 92 | union_num++; 93 | Union(u, v, nodes); 94 | } 95 | if(union_num > n - k) return current_edge.weight; 96 | } 97 | return -1.; 98 | } 99 | 100 | int main() { 101 | size_t n; 102 | int k; 103 | std::cin >> n; 104 | vector x(n), y(n); 105 | for (size_t i = 0; i < n; i++) { 106 | std::cin >> x[i] >> y[i]; 107 | } 108 | std::cin >> k; 109 | std::cout << std::setprecision(10) << clustering(x, y, k) << std::endl; 110 | } 111 | -------------------------------------------------------------------------------- /week 5/clustering/clustering.py: -------------------------------------------------------------------------------- 1 | #import sys 2 | import math 3 | 4 | class Node: 5 | def __init__(self, a, b, c): 6 | self.x = a 7 | self.y = b 8 | self.parent = c 9 | self.rank = 0 10 | 11 | class Edge: 12 | def __init__(self, a, b, c): 13 | self.u = a 14 | self.v = b 15 | self.weight = c 16 | 17 | def MakeSet(i, nodes, x, y): 18 | nodes.append(Node(x[i], y[i], i)) 19 | 20 | def weight(x1, y1, x2, y2): 21 | return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) 22 | 23 | def Find(i, nodes): 24 | if (i != nodes[i].parent) : 25 | nodes[i].parent = Find(nodes[i].parent, nodes) 26 | return nodes[i].parent 27 | 28 | def Union(u, v, nodes): 29 | r1 = Find(u, nodes) 30 | r2 = Find(v, nodes) 31 | if (r1 != r2): 32 | if (nodes[r1].rank > nodes[r2].rank): 33 | nodes[r2].parent = r1 34 | else: 35 | nodes[r1].parent = r2 36 | if (nodes[r1].rank == nodes[r2].rank): 37 | nodes[r2].rank += 1 38 | 39 | def clustering(x, y, k): 40 | #write your code here 41 | n = len(x) 42 | nodes = [] 43 | for i in range(n): 44 | MakeSet(i, nodes, x, y) 45 | edges = [] 46 | for i in range(n): 47 | for j in range(i+1, n): 48 | edges.append(Edge(i, j, weight(x[i], y[i], x[j], y[j]))) 49 | edges = sorted(edges, key=lambda edge: edge.weight) 50 | union_num = 0 51 | for edge in edges: 52 | if Find(edge.u, nodes) != Find(edge.v, nodes): 53 | union_num += 1 54 | Union(edge.u, edge.v, nodes) 55 | if(union_num > n - k): 56 | return edge.weight 57 | return -1. 58 | 59 | 60 | if __name__ == '__main__': 61 | n = int(raw_input()) 62 | x = [0] * n 63 | y = [0] * n 64 | for i in range(n): 65 | x[i], y[i] = map(int, raw_input().split()) 66 | k = int(raw_input()) 67 | ''' 68 | input = sys.stdin.read() 69 | data = list(map(int, input.split())) 70 | n = data[0] 71 | data = data[1:] 72 | x = data[0:2 * n:2] 73 | y = data[1:2 * n:2] 74 | data = data[2 * n:] 75 | k = data[0] 76 | ''' 77 | print("{0:.9f}".format(clustering(x, y, k))) 78 | -------------------------------------------------------------------------------- /week 5/connecting_points/ConnectingPoints.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.PriorityQueue; 3 | import java.util.Comparator; 4 | 5 | public class ConnectingPoints { 6 | private static class Node { 7 | int x; 8 | int y; 9 | int parent; 10 | int rank; 11 | 12 | public Node(int a, int b, int c) { 13 | x = a; 14 | y = b; 15 | parent = c; 16 | rank = 0; 17 | } 18 | } 19 | 20 | private static class Edge { 21 | int u; 22 | int v; 23 | double weight; 24 | 25 | public Edge(int a, int b, double c) { 26 | u = a; 27 | v = b; 28 | weight = c; 29 | } 30 | } 31 | 32 | private static void MakeSet(int i, Node[] nodes, int[] x, int[] y) { 33 | nodes[i] = new Node(x[i], y[i], i); 34 | } 35 | 36 | private static double Weight(int x1, int y1, int x2, int y2) { 37 | return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 38 | } 39 | 40 | private static int Find(int i, Node[] nodes) { 41 | if (i != nodes[i].parent) { 42 | nodes[i].parent = Find(nodes[i].parent, nodes); 43 | } 44 | return nodes[i].parent; 45 | } 46 | 47 | private static void Union(int u, int v, Node[] nodes) { 48 | int r1 = Find(u, nodes); 49 | int r2 = Find(v, nodes); 50 | if (r1 != r2) { 51 | if (nodes[r1].rank > nodes[r2].rank) { 52 | nodes[r2].parent = r1; 53 | } else { 54 | nodes[r1].parent = r2; 55 | if (nodes[r1].rank == nodes[r2].rank) { 56 | nodes[r2].rank++; 57 | } 58 | } 59 | } 60 | } 61 | 62 | private static double minimumDistance(int[] x, int[] y) { 63 | double result = 0.; 64 | //write your code here 65 | int n = x.length; 66 | Node[] nodes = new Node[n]; 67 | for (int i = 0; i < n; i++) { 68 | MakeSet(i, nodes, x, y); 69 | } 70 | PriorityQueue edges = new PriorityQueue<>(new Comparator(){ 71 | @Override 72 | public int compare(Edge e1, Edge e2) { 73 | return e1.weight < e2.weight ? -1 : 1; 74 | } 75 | }); 76 | for (int i = 0; i < n; i++) { 77 | for (int j = i + 1; j < n; j++) { 78 | edges.offer(new Edge(i, j, Weight(x[i], y[i], x[j], y[j]))); 79 | } 80 | } 81 | while (!edges.isEmpty()) { 82 | Edge currentEdge = edges.poll(); 83 | int u = currentEdge.u; 84 | int v = currentEdge.v; 85 | if (Find(u, nodes) != Find(v, nodes)) { 86 | result += currentEdge.weight; 87 | Union(u, v, nodes); 88 | } 89 | } 90 | 91 | return result; 92 | } 93 | 94 | public static void main(String[] args) { 95 | Scanner scanner = new Scanner(System.in); 96 | int n = scanner.nextInt(); 97 | int[] x = new int[n]; 98 | int[] y = new int[n]; 99 | for (int i = 0; i < n; i++) { 100 | x[i] = scanner.nextInt(); 101 | y[i] = scanner.nextInt(); 102 | } 103 | System.out.println(minimumDistance(x, y)); 104 | } 105 | } 106 | 107 | -------------------------------------------------------------------------------- /week 5/connecting_points/connecting_points.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using std::vector; 8 | 9 | struct node { 10 | int parent; 11 | int rank; 12 | int x; 13 | int y; 14 | 15 | node(int a, int b, int c = -1, int d = 0) { 16 | x = a; 17 | y = b; 18 | parent = c; 19 | rank = d; 20 | } 21 | }; 22 | 23 | struct edge { 24 | int u; 25 | int v; 26 | double weight; 27 | 28 | edge(int a, int b, double c) { 29 | u = a; 30 | v = b; 31 | weight = c; 32 | } 33 | }; 34 | 35 | bool cmp(edge a, edge b) { 36 | return a.weight < b.weight; 37 | } 38 | 39 | void make_set(int i, vector &nodes, vector &x, vector &y) { 40 | nodes.push_back(node(x[i], y[i], i)); 41 | } 42 | 43 | double weight(int x1, int y1, int x2, int y2) { 44 | return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); 45 | } 46 | 47 | int Find(int i, vector &nodes) { 48 | if (i != nodes[i].parent) { 49 | nodes[i].parent = Find(nodes[i].parent, nodes); 50 | } 51 | return nodes[i].parent; 52 | } 53 | 54 | void Union(int u, int v, vector &nodes) { 55 | int r1 = Find(u, nodes); 56 | int r2 = Find(v, nodes); 57 | if (r1 != r2) { 58 | if (nodes[r1].rank > nodes[r2].rank) { 59 | nodes[r2].parent = r1; 60 | } else { 61 | nodes[r1].parent = r2; 62 | if (nodes[r1].rank == nodes[r2].rank) { 63 | nodes[r2].rank++; 64 | } 65 | } 66 | } 67 | } 68 | 69 | double minimum_distance(vector x, vector y) { 70 | double result = 0.; 71 | //write your code here 72 | int n = x.size(); 73 | vector nodes; 74 | for (int i = 0; i < n; i++) { 75 | make_set(i, nodes, x, y); 76 | } 77 | vector edges; 78 | for (int i = 0; i < n; i++) { 79 | for (int j = i + 1; j < n; j++) { 80 | edges.push_back(edge(i, j, weight(x[i], y[i], x[j], y[j]))); 81 | } 82 | } 83 | std::sort(edges.begin(), edges.end(), cmp); 84 | for (int i = 0; i < edges.size(); i++) { 85 | edge current_edge = edges[i]; 86 | int u = current_edge.u; 87 | int v = current_edge.v; 88 | if (Find(u, nodes) != Find(v, nodes)) { 89 | result += current_edge.weight; 90 | Union(u, v, nodes); 91 | } 92 | } 93 | return result; 94 | } 95 | 96 | int main() { 97 | size_t n; 98 | std::cin >> n; 99 | vector x(n), y(n); 100 | for (size_t i = 0; i < n; i++) { 101 | std::cin >> x[i] >> y[i]; 102 | } 103 | std::cout << std::setprecision(10) << minimum_distance(x, y) << std::endl; 104 | } 105 | -------------------------------------------------------------------------------- /week 5/connecting_points/connecting_points.py: -------------------------------------------------------------------------------- 1 | #import sys 2 | import math 3 | 4 | class Node: 5 | def __init__(self, a, b, c): 6 | self.x = a 7 | self.y = b 8 | self.parent = c 9 | self.rank = 0 10 | 11 | class Edge: 12 | def __init__(self, a, b, c): 13 | self.u = a 14 | self.v = b 15 | self.weight = c 16 | 17 | def MakeSet(i, nodes, x, y): 18 | nodes.append(Node(x[i], y[i], i)) 19 | 20 | def weight(x1, y1, x2, y2): 21 | return math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) 22 | 23 | def Find(i, nodes): 24 | if (i != nodes[i].parent) : 25 | nodes[i].parent = Find(nodes[i].parent, nodes) 26 | return nodes[i].parent 27 | 28 | def Union(u, v, nodes): 29 | r1 = Find(u, nodes) 30 | r2 = Find(v, nodes) 31 | if (r1 != r2): 32 | if (nodes[r1].rank > nodes[r2].rank): 33 | nodes[r2].parent = r1 34 | else: 35 | nodes[r1].parent = r2 36 | if (nodes[r1].rank == nodes[r2].rank): 37 | nodes[r2].rank += 1 38 | 39 | def minimum_distance(x, y): 40 | result = 0. 41 | #write your code here 42 | n = len(x) 43 | nodes = [] 44 | for i in range(n): 45 | MakeSet(i, nodes, x, y) 46 | edges = [] 47 | for i in range(n): 48 | for j in range(i+1, n): 49 | edges.append(Edge(i, j, weight(x[i], y[i], x[j], y[j]))) 50 | edges = sorted(edges, key=lambda edge: edge.weight) 51 | for edge in edges: 52 | if Find(edge.u, nodes) != Find(edge.v, nodes): 53 | result += edge.weight 54 | Union(edge.u, edge.v, nodes) 55 | return result 56 | 57 | 58 | if __name__ == '__main__': 59 | n = int(raw_input()) 60 | x = [0] * n 61 | y = [0] * n 62 | for i in range(n): 63 | x[i], y[i] = map(int, raw_input().split()) 64 | ''' 65 | input = sys.stdin.read() 66 | data = list(map(int, input.split())) 67 | n = data[0] 68 | x = data[1::2] 69 | y = data[2::2] 70 | ''' 71 | print("{0:.9f}".format(minimum_distance(x, y))) 72 | -------------------------------------------------------------------------------- /week 5/spanning_trees_problems.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 5/spanning_trees_problems.pdf -------------------------------------------------------------------------------- /week 6/Advanced-Shortest-Paths.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akueisara/algo-on-graphs/06c71e9558838a982b624606cd0b5e7def0fd99e/week 6/Advanced-Shortest-Paths.pdf -------------------------------------------------------------------------------- /week 6/dist_with_coords/DistWithCoords.java: -------------------------------------------------------------------------------- 1 | // Failed case #6/9: Wrong answer .. don't know why.. 2 | 3 | import java.util.Scanner; 4 | import java.util.ArrayList; 5 | import java.util.Arrays; 6 | import java.util.PriorityQueue; 7 | import java.util.HashMap; 8 | import java.lang.Math; 9 | 10 | public class DistWithCoords { 11 | private static class Impl { 12 | // Number of nodes 13 | int n; 14 | // Coordinates of nodes 15 | int[] x; 16 | int[] y; 17 | // See description of these fields in the starters for friend_suggestion 18 | ArrayList[] adj; 19 | ArrayList[] cost; 20 | Long[] distance; 21 | PriorityQueue queue; 22 | boolean[] visited; 23 | ArrayList workset; 24 | HashMap p; 25 | final Long INFINITY = Long.MAX_VALUE / 4; 26 | 27 | Impl(int n) { 28 | this.n = n; 29 | visited = new boolean[n]; 30 | x = new int[n]; 31 | y = new int[n]; 32 | Arrays.fill(visited, false); 33 | workset = new ArrayList(); 34 | p = new HashMap(); 35 | distance = new Long[n]; 36 | for (int i = 0; i < n; ++i) { 37 | distance[i] = INFINITY; 38 | } 39 | queue = new PriorityQueue(); 40 | } 41 | 42 | // See the description of this method in the starters for friend_suggestion 43 | void clear() { 44 | for (int v : workset) { 45 | distance[v] = INFINITY; 46 | visited[v] = false; 47 | } 48 | workset.clear(); 49 | queue.clear(); 50 | p.clear(); 51 | } 52 | 53 | // See the description of this method in the starters for friend_suggestion 54 | void visit(int v, Long dist, Long measure) { 55 | // Implement this method yourself 56 | if (distance[v] > dist) { 57 | distance[v] = dist; 58 | queue.add(new Entry(distance[v] + measure, v)); 59 | workset.add(v); 60 | } 61 | } 62 | 63 | Long potential(int u, int t) { 64 | if (!p.containsKey(u)) { 65 | p.put(u, (long) Math.sqrt((x[u]-x[t])*(x[u]-x[t])+(y[u]-y[t])*(y[u]-y[t]))); 66 | } 67 | return p.get(u); 68 | } 69 | 70 | int extractMin() { 71 | Entry e = queue.poll(); 72 | return e.node; 73 | } 74 | 75 | void process(int u, int t, ArrayList[] adj, ArrayList[] cost) { 76 | for (int i = 0; i < adj[u].size(); ++i) { 77 | int v = adj[u].get(i); 78 | if (visited[v] != true) { 79 | Long w = (long) cost[u].get(i); 80 | visit(v, distance[u] + w, potential(v, t)); 81 | } 82 | } 83 | } 84 | 85 | // Returns the distance from s to t in the graph. 86 | Long query(int s, int t) { 87 | clear(); 88 | visit(s, 0L, potential(s, t)); 89 | // Implement the rest of the algorithm yourself 90 | while (!queue.isEmpty()) { 91 | int v = extractMin(); 92 | if (v == t) { 93 | if(distance[t] != INFINITY) 94 | return distance[t]; 95 | else 96 | return -1L; 97 | } 98 | if (visited[v] != true) { 99 | process(v, t, adj, cost); 100 | visited[v] = true; 101 | } 102 | } 103 | return -1L; 104 | } 105 | 106 | class Entry implements Comparable 107 | { 108 | Long cost; 109 | int node; 110 | 111 | public Entry(Long cost, int node) 112 | { 113 | this.cost = cost; 114 | this.node = node; 115 | } 116 | 117 | public int compareTo(Entry other) 118 | { 119 | return cost < other.cost ? -1 : cost > other.cost ? 1 : 0; 120 | } 121 | } 122 | } 123 | 124 | public static void main(String args[]) { 125 | Scanner in = new Scanner(System.in); 126 | int n = in.nextInt(); 127 | int m = in.nextInt(); 128 | Impl DistWithCoords = new Impl(n); 129 | DistWithCoords.adj = (ArrayList[])new ArrayList[n]; 130 | DistWithCoords.cost = (ArrayList[])new ArrayList[n]; 131 | for (int i = 0; i < n; i++) { 132 | DistWithCoords.adj[i] = new ArrayList(); 133 | DistWithCoords.cost[i] = new ArrayList(); 134 | } 135 | 136 | for (int i = 0; i < n; i++) { 137 | int x, y; 138 | x = in.nextInt(); 139 | y = in.nextInt(); 140 | DistWithCoords.x[i] = x; 141 | DistWithCoords.y[i] = y; 142 | } 143 | 144 | for (int i = 0; i < m; i++) { 145 | int x, y, c; 146 | x = in.nextInt(); 147 | y = in.nextInt(); 148 | c = in.nextInt(); 149 | DistWithCoords.adj[x - 1].add(y - 1); 150 | DistWithCoords.cost[x - 1].add(c); 151 | } 152 | 153 | int t = in.nextInt(); 154 | 155 | for (int i = 0; i < t; i++) { 156 | int u, v; 157 | u = in.nextInt(); 158 | v = in.nextInt(); 159 | System.out.println(DistWithCoords.query(u-1, v-1)); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/dist_with_coords.cpp: -------------------------------------------------------------------------------- 1 | // Failed case #9/9: unknown signal 11... don't know how to slove it QQ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | // See the explanations of these typedefs and constants in the starter for friend_suggestion 15 | typedef vector> Adj; 16 | typedef long long Len; 17 | typedef priority_queue,vector>,greater>> Queue; 18 | 19 | const Len INFINITY_ = numeric_limits::max() / 4; 20 | 21 | class AStar { 22 | // See the descriptions of these fields in the starter for friend_suggestion 23 | int n_; 24 | Adj adj_; 25 | Adj cost_; 26 | vector distance_; 27 | vector visited_; 28 | vector workset_; 29 | vector p_; 30 | // Coordinates of the nodes 31 | vector> xy_; 32 | 33 | public: 34 | AStar(int n, Adj adj, Adj cost, vector> xy) 35 | : n_(n), adj_(adj), cost_(cost), distance_(vector(n_, INFINITY_)), visited_(n), xy_(xy), p_(vector(n_, -1)) 36 | {workset_.reserve(n);} 37 | 38 | // See the description of this method in the starter for friend_suggestion 39 | void clear() { 40 | for (int i = 0; i < workset_.size(); ++i) { 41 | int v = workset_[i]; 42 | distance_[v] = INFINITY_; 43 | visited_[v] = false; 44 | p_[v] = -1; 45 | } 46 | workset_.clear(); 47 | } 48 | 49 | // See the description of this method in the starter for friend_suggestion 50 | void visit(Queue& q, int v, Len dist, Len measure) { 51 | // Implement this method yourself 52 | if (distance_[v] > dist) { 53 | distance_[v] = dist; 54 | q.push({distance_[v] + measure, v}); 55 | workset_.push_back(v); 56 | } 57 | } 58 | 59 | Len Potential(int u, int t) { 60 | if(p_[u] == -1) { 61 | pair p_u = xy_[u]; 62 | pair p_t = xy_[t]; 63 | p_[u] = sqrt((p_u.first - p_t.first)*(p_u.first - p_t.first) + (p_u.second - p_t.second)*(p_u.second - p_t.second)); 64 | } 65 | return p_[u]; 66 | } 67 | 68 | int extractMin(Queue& q) { 69 | pair p = q.top(); 70 | q.pop(); 71 | return p.second; 72 | } 73 | 74 | void Process(Queue& q, int u, int t, vector> &adj, const vector> &cost) { 75 | for (int i = 0; i < adj[u].size(); ++i) { 76 | int v = adj[u][i]; 77 | if (visited_[v] != true) { 78 | int w = cost[u][i]; 79 | visit(q, v, distance_[u] + w, Potential(v, t)); 80 | } 81 | } 82 | } 83 | 84 | // Returns the distance from s to t in the graph 85 | Len query(int s, int t) { 86 | clear(); 87 | Queue q; 88 | visit(q, s, 0, Potential(s, t)); 89 | // Implement the rest of the algorithm yourself 90 | while (!q.empty()) { 91 | int v = extractMin(q); 92 | if (v == t) { 93 | if(distance_[t] != INFINITY_) 94 | return distance_[t]; 95 | else 96 | return -1; 97 | } 98 | if (visited_[v] != true) { 99 | Process(q, v, t, adj_, cost_); 100 | visited_[v] = true; 101 | } 102 | } 103 | return -1; 104 | } 105 | }; 106 | 107 | int main() { 108 | int n, m; 109 | scanf("%d%d", &n, &m); 110 | vector> xy(n); 111 | for (int i=0;i dist: 34 | self.d[v] = dist 35 | q.put((self.d[v] + measure, v)) 36 | self.workset.append(v) 37 | 38 | def potential(self, u, t): 39 | if u not in self.p: 40 | u = (self.x[u], self.y[u]) 41 | t = (self.x[t], self.y[t]) 42 | self.p[u] = math.sqrt((u[0]-t[0])**2+(u[1]-t[1])**2) 43 | return self.p[u] 44 | 45 | def extract_min(self, q): 46 | _, v = q.get() 47 | return v 48 | 49 | def process(self, q, v, t): 50 | for u, w in zip(adj[v], cost[v]): 51 | if not self.visited[u]: 52 | self.visit(q, u, self.d[v] + w, self.potential(u, t)) 53 | 54 | # Returns the distance from s to t in the graph 55 | def query(self, s, t): 56 | self.clear() 57 | q = queue.PriorityQueue() 58 | # Implement the rest of the algorithm yourself 59 | self.visit(q, s, 0, self.potential(s, t)) 60 | while not q.empty(): 61 | v = self.extract_min(q) 62 | if v == t: 63 | return (self.d[t] if self.d[t] != self.inf else -1) 64 | if not self.visited[v]: 65 | self.process(q, v, t) 66 | self.visited[v] = True 67 | return -1 68 | 69 | def readl(): 70 | return map(int, sys.stdin.readline().split()) 71 | 72 | if __name__ == '__main__': 73 | n,m = readl() 74 | x = [0 for _ in range(n)] 75 | y = [0 for _ in range(n)] 76 | adj = [[] for _ in range(n)] 77 | cost = [[] for _ in range(n)] 78 | for i in range(n): 79 | a, b = readl() 80 | x[i] = a 81 | y[i] = b 82 | for e in range(m): 83 | u,v,c = readl() 84 | adj[u-1].append(v-1) 85 | cost[u-1].append(c) 86 | t, = readl() 87 | astar = AStar(n, adj, cost, x, y) 88 | for i in range(t): 89 | s, t = readl() 90 | print(astar.query(s-1, t-1)) 91 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/01: -------------------------------------------------------------------------------- 1 | 2 1 2 | 0 0 3 | 0 1 4 | 1 2 1 5 | 4 6 | 1 1 7 | 2 2 8 | 1 2 9 | 2 1 10 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/01.a: -------------------------------------------------------------------------------- 1 | 0 2 | 0 3 | 1 4 | -1 5 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/02: -------------------------------------------------------------------------------- 1 | 18 36 2 | -74258291 40695601 3 | -74259991 40694901 4 | -74257543 40695865 5 | -74260991 40694301 6 | -74256591 40696201 7 | -74261991 40694501 8 | -74261391 40694201 9 | -74259891 40694101 10 | -74254991 40696501 11 | -74262191 40694601 12 | -74263591 40693401 13 | -74259091 40695901 14 | -74262491 40693501 15 | -74258091 40693501 16 | -74254887 40696033 17 | -74254891 40697301 18 | -74253490 40697001 19 | -74262591 40694701 20 | 1 2 1838 21 | 1 3 793 22 | 2 4 1166 23 | 2 1 1838 24 | 3 1 793 25 | 3 5 1009 26 | 4 6 1019 27 | 4 2 1166 28 | 4 7 412 29 | 4 8 1118 30 | 5 9 1627 31 | 5 3 1009 32 | 6 10 223 33 | 6 11 1941 34 | 6 4 1019 35 | 6 12 3220 36 | 7 4 412 37 | 7 13 1303 38 | 8 4 1118 39 | 8 14 1897 40 | 9 15 479 41 | 9 16 806 42 | 9 5 1627 43 | 9 17 1582 44 | 10 18 412 45 | 10 6 223 46 | 10 12 3361 47 | 11 6 1941 48 | 12 10 3361 49 | 12 6 3220 50 | 13 7 1303 51 | 14 8 1897 52 | 15 9 479 53 | 16 9 806 54 | 17 9 1582 55 | 18 10 412 56 | 10 57 | 2 17 58 | 10 8 59 | 6 8 60 | 11 13 61 | 16 2 62 | 15 14 63 | 15 8 64 | 6 5 65 | 7 1 66 | 17 11 67 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/02.a: -------------------------------------------------------------------------------- 1 | 6849 2 | 2360 3 | 2137 4 | 4675 5 | 6073 6 | 9927 7 | 8030 8 | 5825 9 | 3416 10 | 10975 11 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/03: -------------------------------------------------------------------------------- 1 | 100 1000 2 | 3097 9733 3 | 4950 275 4 | 2506 1855 5 | 3995 6281 6 | 6802 7863 7 | 9181 5848 8 | 5707 6125 9 | 4493 2397 10 | 218 7209 11 | 5206 2527 12 | 2597 7107 13 | 9572 8034 14 | 2438 3445 15 | 8528 5749 16 | 4509 1633 17 | 5903 1120 18 | 4911 7401 19 | 9006 9354 20 | 9610 6301 21 | 5159 3943 22 | 6608 4014 23 | 983 6970 24 | 7933 3377 25 | 4504 9327 26 | 2598 3013 27 | 9854 5588 28 | 9425 5619 29 | 3829 8108 30 | 5363 5695 31 | 2848 5596 32 | 6671 6205 33 | 2703 52 34 | 9240 2787 35 | 5142 2211 36 | 4463 2330 37 | 2704 5165 38 | 9929 7205 39 | 7253 9617 40 | 8609 6978 41 | 9616 1464 42 | 385 6742 43 | 8784 2932 44 | 4449 8303 45 | 4697 5851 46 | 114 2124 47 | 1158 6795 48 | 9479 7280 49 | 1309 9994 50 | 7041 9403 51 | 6124 1365 52 | 717 955 53 | 3397 3039 54 | 5203 5855 55 | 6835 6922 56 | 3659 1121 57 | 998 7977 58 | 5482 3239 59 | 5046 6053 60 | 572 9390 61 | 1477 2709 62 | 5169 9837 63 | 6205 3776 64 | 4447 3508 65 | 3808 9664 66 | 4414 4073 67 | 142 330 68 | 7805 7488 69 | 5744 1639 70 | 7449 7145 71 | 3256 2731 72 | 5279 5058 73 | 646 1988 74 | 1626 276 75 | 1074 7382 76 | 5546 5971 77 | 5595 1644 78 | 6951 2968 79 | 812 5793 80 | 5234 3229 81 | 111 1757 82 | 9472 8058 83 | 7864 4104 84 | 2697 8600 85 | 2593 9010 86 | 4426 5205 87 | 8315 7664 88 | 7162 4601 89 | 4242 9832 90 | 6825 2201 91 | 2695 412 92 | 4430 1218 93 | 3962 2050 94 | 858 6355 95 | 9624 6037 96 | 8941 9204 97 | 4551 6734 98 | 8304 2244 99 | 6742 5592 100 | 4825 678 101 | 6034 813 102 | 95 6 3365 103 | 7 33 4861 104 | 44 73 6365 105 | 36 91 4308 106 | 6 50 5427 107 | 48 37 9060 108 | 61 29 4147 109 | 34 36 3831 110 | 73 58 6714 111 | 23 19 3371 112 | 21 42 2431 113 | 18 92 8877 114 | 51 72 1036 115 | 77 14 3198 116 | 29 27 4063 117 | 7 32 6776 118 | 63 55 2514 119 | 80 77 6947 120 | 85 93 3749 121 | 6 29 3822 122 | 37 3 9151 123 | 74 70 5138 124 | 26 28 6531 125 | 31 34 4277 126 | 85 91 3988 127 | 94 20 4932 128 | 82 54 3000 129 | 63 44 2357 130 | 52 54 5187 131 | 68 89 1219 132 | 44 85 701 133 | 78 14 7717 134 | 46 63 4650 135 | 87 59 8147 136 | 9 8 6437 137 | 42 25 6187 138 | 38 28 3742 139 | 27 22 8550 140 | 36 39 6178 141 | 88 28 1773 142 | 25 60 1162 143 | 62 39 4005 144 | 64 48 2521 145 | 85 42 4916 146 | 34 14 4898 147 | 98 34 3741 148 | 98 22 5922 149 | 95 78 8816 150 | 36 46 2247 151 | 43 4 2073 152 | 92 93 5308 153 | 46 58 3959 154 | 67 34 5911 155 | 59 44 5436 156 | 90 15 2187 157 | 59 11 3052 158 | 20 54 3419 159 | 91 10 1522 160 | 87 57 2163 161 | 19 68 6057 162 | 27 42 2763 163 | 17 5 1947 164 | 16 50 330 165 | 80 81 11285 166 | 62 93 5937 167 | 64 92 7616 168 | 49 74 6300 169 | 70 15 1667 170 | 40 8 5208 171 | 48 55 9179 172 | 62 29 2096 173 | 24 66 9999 174 | 87 57 2163 175 | 18 93 8683 176 | 6 46 8079 177 | 98 55 5431 178 | 85 50 4199 179 | 41 100 8190 180 | 44 73 6365 181 | 88 52 6846 182 | 94 52 6912 183 | 97 36 6317 184 | 54 30 4202 185 | 87 54 2344 186 | 67 35 6147 187 | 93 94 8772 188 | 51 73 1135 189 | 71 73 6018 190 | 57 67 4843 191 | 65 16 3308 192 | 6 8 5822 193 | 94 10 5643 194 | 100 13 4457 195 | 96 14 4098 196 | 20 40 5101 197 | 28 87 4839 198 | 41 13 3884 199 | 62 68 2187 200 | 78 51 4839 201 | 96 15 5102 202 | 100 51 5319 203 | 29 91 4574 204 | 40 91 5192 205 | 62 75 2292 206 | 51 29 6638 207 | 5 31 1664 208 | 53 49 3996 209 | 24 8 6931 210 | 11 85 2639 211 | 74 87 6694 212 | 52 37 7748 213 | 59 53 5827 214 | 36 43 3591 215 | 66 61 10755 216 | 33 80 9187 217 | 11 83 1497 218 | 16 37 7297 219 | 11 76 6232 220 | 55 46 6201 221 | 86 92 7104 222 | 2 11 7226 223 | 40 34 4536 224 | 88 92 7788 225 | 29 89 3788 226 | 77 20 2041 227 | 3 18 9924 228 | 18 16 8800 229 | 5 18 2661 230 | 35 93 5404 231 | 80 65 4887 232 | 36 17 3142 233 | 89 60 5373 234 | 27 95 3618 235 | 91 20 2821 236 | 61 33 8141 237 | 80 22 5286 238 | 9 55 6994 239 | 85 35 2876 240 | 19 43 5536 241 | 49 19 4028 242 | 8 73 3567 243 | 81 69 2220 244 | 45 94 10284 245 | 94 57 4999 246 | 10 17 4883 247 | 86 36 6143 248 | 85 54 2959 249 | 12 81 103 250 | 3 86 8216 251 | 50 54 5603 252 | 74 86 7247 253 | 41 59 2655 254 | 12 30 7153 255 | 37 50 6971 256 | 72 11 5479 257 | 24 78 5111 258 | 83 64 1539 259 | 80 100 5998 260 | 33 35 4799 261 | 48 47 8609 262 | 93 6 8339 263 | 70 8 1282 264 | 59 94 9654 265 | 82 50 3245 266 | 44 56 4267 267 | 98 69 1707 268 | 71 39 3844 269 | 96 31 2186 270 | 91 18 9335 271 | 90 45 3098 272 | 62 59 7953 273 | 9 64 4350 274 | 25 81 8527 275 | 67 77 4600 276 | 96 18 5169 277 | 99 53 5191 278 | 42 95 6274 279 | 56 6 8456 280 | 16 3 3476 281 | 64 82 6883 282 | 39 19 1209 283 | 61 27 5993 284 | 49 65 5943 285 | 69 19 2320 286 | 40 99 4856 287 | 34 56 7101 288 | 40 30 7930 289 | 48 40 11907 290 | 64 76 8217 291 | 53 54 1950 292 | 79 80 5331 293 | 87 63 2927 294 | 17 80 7410 295 | 9 44 4681 296 | 90 31 7027 297 | 62 72 5840 298 | 79 91 2166 299 | 12 7 4311 300 | 20 80 5501 301 | 17 85 2249 302 | 17 91 6202 303 | 25 43 5605 304 | 40 20 5101 305 | 67 80 9594 306 | 78 77 6758 307 | 97 72 7663 308 | 13 33 6834 309 | 21 67 3675 310 | 41 70 4933 311 | 16 42 3404 312 | 45 90 3098 313 | 3 55 1367 314 | 83 43 1777 315 | 31 98 618 316 | 91 28 6917 317 | 74 84 2227 318 | 98 77 2633 319 | 57 8 1299 320 | 67 54 1124 321 | 42 50 3088 322 | 39 29 3491 323 | 49 86 2156 324 | 72 70 2714 325 | 24 9 4781 326 | 70 76 2580 327 | 23 90 6019 328 | 89 12 6448 329 | 34 94 5893 330 | 48 86 7384 331 | 33 54 4784 332 | 13 38 7829 333 | 29 38 4354 334 | 59 43 4027 335 | 65 47 5995 336 | 49 98 3823 337 | 18 62 6242 338 | 46 66 6545 339 | 23 1 7987 340 | 14 44 3833 341 | 50 49 8091 342 | 84 12 7047 343 | 58 75 507 344 | 62 46 5882 345 | 70 39 6834 346 | 98 65 2780 347 | 3 7 5337 348 | 94 53 4425 349 | 22 77 7186 350 | 83 63 5385 351 | 71 56 5182 352 | 80 59 7647 353 | 21 93 6209 354 | 11 32 7056 355 | 15 50 1638 356 | 88 24 569 357 | 9 88 4804 358 | 69 51 9146 359 | 6 45 9802 360 | 76 17 5798 361 | 82 60 6538 362 | 82 84 7201 363 | 78 42 8470 364 | 38 3 9099 365 | 26 74 8962 366 | 13 70 1086 367 | 99 95 9468 368 | 63 26 5794 369 | 75 25 4177 370 | 90 24 9097 371 | 57 2 3012 372 | 94 23 3152 373 | 16 5 6803 374 | 21 24 5715 375 | 16 11 6840 376 | 84 74 2227 377 | 58 9 4965 378 | 51 2 4288 379 | 58 86 3645 380 | 97 96 5852 381 | 19 87 2981 382 | 42 92 4903 383 | 94 86 2089 384 | 31 87 1678 385 | 72 64 8302 386 | 31 83 4640 387 | 30 66 5921 388 | 33 9 10048 389 | 3 73 1808 390 | 34 77 1962 391 | 85 52 2398 392 | 7 31 968 393 | 56 26 9173 394 | 28 8 5750 395 | 1 77 7786 396 | 95 88 4741 397 | 53 27 4229 398 | 94 75 4079 399 | 73 29 6583 400 | 90 8 2679 401 | 3 48 8227 402 | 90 40 7001 403 | 27 32 8728 404 | 68 28 6747 405 | 54 26 3301 406 | 75 50 4643 407 | 86 21 4030 408 | 1 31 5022 409 | 35 16 1881 410 | 99 20 3283 411 | 14 2 6540 412 | 98 65 2780 413 | 61 83 2765 414 | 93 90 6221 415 | 40 2 4816 416 | 42 75 4441 417 | 20 8 1684 418 | 2 35 2112 419 | 67 87 2958 420 | 30 26 7007 421 | 92 12 8203 422 | 10 82 3091 423 | 64 94 6855 424 | 91 88 8617 425 | 67 12 1850 426 | 83 90 8189 427 | 89 67 5378 428 | 29 82 2965 429 | 38 92 8252 430 | 66 82 8595 431 | 98 29 1383 432 | 34 63 1472 433 | 41 4 3640 434 | 58 8 3698 435 | 40 47 5818 436 | 50 87 3399 437 | 37 31 3409 438 | 13 9 4370 439 | 99 33 4893 440 | 54 11 4243 441 | 53 48 5683 442 | 36 60 2746 443 | 78 91 5833 444 | 75 40 6073 445 | 94 64 6855 446 | 29 23 3461 447 | 86 46 7210 448 | 63 23 3489 449 | 45 33 9151 450 | 81 82 4269 451 | 90 34 3038 452 | 58 72 5991 453 | 30 42 6507 454 | 53 7 572 455 | 51 39 9928 456 | 8 98 3908 457 | 98 2 5611 458 | 53 75 363 459 | 44 89 4226 460 | 100 19 6551 461 | 59 25 6692 462 | 28 59 3501 463 | 57 16 2161 464 | 30 66 5921 465 | 2 72 4633 466 | 57 92 1930 467 | 82 16 3571 468 | 3 56 6305 469 | 50 34 1297 470 | 8 3 2060 471 | 48 86 7384 472 | 40 76 4026 473 | 5 96 2519 474 | 27 89 4295 475 | 47 94 1252 476 | 80 31 7926 477 | 9 26 9772 478 | 99 52 2760 479 | 43 70 5699 480 | 75 92 4229 481 | 19 99 7384 482 | 48 92 8376 483 | 22 59 2455 484 | 23 98 2515 485 | 49 79 6434 486 | 17 1 2955 487 | 54 26 3301 488 | 80 94 10432 489 | 19 34 6058 490 | 59 64 3248 491 | 87 96 3372 492 | 27 80 10083 493 | 38 94 4294 494 | 35 25 1987 495 | 81 97 5931 496 | 28 37 6167 497 | 60 24 7278 498 | 31 86 2199 499 | 87 50 3399 500 | 26 11 7415 501 | 78 94 8816 502 | 31 84 4950 503 | 7 84 4246 504 | 4 29 1489 505 | 88 5 3230 506 | 18 19 3113 507 | 89 78 7005 508 | 65 83 4842 509 | 39 24 4730 510 | 61 37 5440 511 | 72 73 1973 512 | 73 38 10905 513 | 2 24 9063 514 | 70 19 7289 515 | 96 90 6589 516 | 90 47 9654 517 | 54 46 5679 518 | 68 8 1463 519 | 50 16 330 520 | 56 55 7355 521 | 11 64 2830 522 | 45 26 10338 523 | 55 64 8545 524 | 22 23 7824 525 | 7 16 5009 526 | 17 32 7674 527 | 30 21 4080 528 | 88 47 5826 529 | 77 38 6656 530 | 40 77 3061 531 | 55 14 6718 532 | 32 51 2182 533 | 8 56 6585 534 | 45 53 6311 535 | 11 19 7060 536 | 17 2 7127 537 | 55 21 4132 538 | 56 42 9278 539 | 44 18 5554 540 | 15 77 2784 541 | 94 33 3273 542 | 80 20 5501 543 | 36 76 4556 544 | 15 4 4677 545 | 39 13 7111 546 | 63 98 3101 547 | 5 38 1812 548 | 38 9 7436 549 | 9 24 4781 550 | 70 13 1086 551 | 21 5 3854 552 | 8 30 3598 553 | 23 84 7762 554 | 53 10 3329 555 | 85 12 5873 556 | 33 73 8018 557 | 100 28 7621 558 | 14 58 3496 559 | 10 95 7651 560 | 13 99 3655 561 | 47 21 4349 562 | 83 99 8203 563 | 98 13 4810 564 | 94 25 7650 565 | 99 72 4380 566 | 90 34 3038 567 | 33 3 6799 568 | 45 21 6764 569 | 34 52 1932 570 | 6 79 4737 571 | 67 21 3675 572 | 92 40 5685 573 | 66 43 9062 574 | 15 7 4650 575 | 40 26 4131 576 | 15 84 7622 577 | 25 99 3227 578 | 24 88 569 579 | 72 6 9368 580 | 85 2 4958 581 | 51 1 9095 582 | 5 7 2055 583 | 46 12 8505 584 | 82 61 6335 585 | 62 72 5840 586 | 43 3 6735 587 | 55 88 8731 588 | 85 63 1698 589 | 96 84 3003 590 | 75 11 3161 591 | 41 89 7880 592 | 40 98 5030 593 | 22 89 7542 594 | 29 26 4493 595 | 58 33 5316 596 | 40 74 10392 597 | 91 35 1113 598 | 36 64 4633 599 | 66 4 7090 600 | 56 4 3444 601 | 47 31 3007 602 | 32 30 5546 603 | 85 27 5017 604 | 44 28 2419 605 | 78 35 5033 606 | 59 46 2661 607 | 49 53 3996 608 | 63 78 4294 609 | 11 71 3376 610 | 72 82 7522 611 | 47 78 8794 612 | 6 59 9310 613 | 26 12 2463 614 | 72 26 9887 615 | 4 58 1076 616 | 19 69 2320 617 | 7 41 5358 618 | 52 96 3872 619 | 41 88 4943 620 | 36 44 2108 621 | 79 59 7727 622 | 55 77 3775 623 | 87 62 1264 624 | 69 32 8535 625 | 9 25 4824 626 | 93 41 612 627 | 1 98 5517 628 | 85 79 2135 629 | 25 92 1670 630 | 78 89 7005 631 | 87 57 2163 632 | 93 34 5961 633 | 57 100 2489 634 | 91 72 3862 635 | 66 1 9857 636 | 10 80 5153 637 | 62 48 7915 638 | 89 6 4342 639 | 77 8 2524 640 | 75 48 5843 641 | 88 80 9071 642 | 48 15 8953 643 | 72 63 4094 644 | 76 74 7306 645 | 84 53 4095 646 | 5 69 967 647 | 23 35 3625 648 | 35 99 1692 649 | 98 8 3908 650 | 88 82 6778 651 | 85 61 4692 652 | 58 62 2555 653 | 37 14 2021 654 | 93 87 6544 655 | 27 22 8550 656 | 3 52 1482 657 | 9 26 9772 658 | 85 18 6180 659 | 34 58 3844 660 | 100 58 5333 661 | 2 84 9048 662 | 85 16 4344 663 | 49 23 6092 664 | 100 11 7172 665 | 75 13 4006 666 | 90 82 6353 667 | 53 46 4153 668 | 36 72 3786 669 | 23 30 5549 670 | 54 29 1917 671 | 19 74 8605 672 | 26 51 10245 673 | 22 85 3870 674 | 39 19 1209 675 | 86 77 4891 676 | 99 98 5275 677 | 14 69 1765 678 | 95 79 7032 679 | 5 17 1947 680 | 2 43 8044 681 | 71 58 1022 682 | 73 94 9857 683 | 84 78 3678 684 | 83 76 7536 685 | 64 34 7572 686 | 96 39 4066 687 | 14 82 1774 688 | 4 86 4536 689 | 71 16 3988 690 | 22 70 4810 691 | 18 83 6354 692 | 10 33 4043 693 | 42 33 479 694 | 9 60 4673 695 | 62 59 7953 696 | 89 48 9548 697 | 66 20 6183 698 | 34 80 5052 699 | 61 45 9222 700 | 47 36 7098 701 | 14 23 2446 702 | 98 16 4551 703 | 60 16 4703 704 | 89 81 6428 705 | 27 63 5408 706 | 71 46 4473 707 | 56 43 3467 708 | 55 24 8250 709 | 92 53 4003 710 | 60 4 4371 711 | 17 39 3723 712 | 29 21 2092 713 | 73 41 6585 714 | 98 23 2515 715 | 88 98 4923 716 | 91 90 1914 717 | 59 100 10169 718 | 72 27 9501 719 | 67 79 4975 720 | 19 42 3469 721 | 56 100 8757 722 | 58 42 4870 723 | 50 19 6043 724 | 38 100 8888 725 | 61 40 9481 726 | 63 23 3489 727 | 59 38 6685 728 | 24 34 7145 729 | 40 17 7576 730 | 88 89 8057 731 | 22 3 5337 732 | 9 22 802 733 | 15 46 6155 734 | 98 1 5517 735 | 5 6 3118 736 | 54 99 6560 737 | 65 55 3048 738 | 17 70 4955 739 | 91 97 4008 740 | 40 2 4816 741 | 82 35 3836 742 | 99 76 1236 743 | 88 81 5523 744 | 38 86 2224 745 | 26 30 7007 746 | 66 74 7114 747 | 14 88 5920 748 | 3 2 2911 749 | 93 88 4852 750 | 34 49 7439 751 | 48 3 8227 752 | 22 38 6806 753 | 82 88 6778 754 | 61 63 6371 755 | 36 53 2593 756 | 2 32 2259 757 | 62 60 4847 758 | 57 29 2459 759 | 96 34 4562 760 | 12 45 11153 761 | 5 14 2730 762 | 72 57 4996 763 | 7 51 7186 764 | 78 28 3803 765 | 73 29 6583 766 | 10 28 5749 767 | 62 87 1264 768 | 1 51 9095 769 | 74 39 7546 770 | 20 59 7122 771 | 86 5 1527 772 | 74 82 7540 773 | 25 53 3856 774 | 3 57 3283 775 | 81 53 4804 776 | 24 66 9999 777 | 15 5 6639 778 | 40 20 5101 779 | 42 36 6478 780 | 38 31 3462 781 | 15 22 6397 782 | 44 78 3886 783 | 67 98 2174 784 | 58 81 4859 785 | 36 4 1707 786 | 27 39 1586 787 | 53 60 4877 788 | 21 40 3944 789 | 99 67 7434 790 | 17 88 2522 791 | 9 42 9575 792 | 91 43 7086 793 | 15 88 8204 794 | 85 20 1460 795 | 3 93 4793 796 | 93 4 3138 797 | 16 33 3731 798 | 48 52 7262 799 | 14 61 5291 800 | 85 54 2959 801 | 37 40 5750 802 | 94 44 4931 803 | 82 76 3347 804 | 73 91 2959 805 | 52 35 1281 806 | 58 16 5007 807 | 35 39 6229 808 | 23 19 3371 809 | 29 45 6349 810 | 19 47 988 811 | 45 21 6764 812 | 52 50 3200 813 | 91 99 670 814 | 60 17 5815 815 | 39 65 5103 816 | 27 77 3627 817 | 65 53 1949 818 | 52 27 6557 819 | 56 5 5806 820 | 93 70 4346 821 | 57 40 4499 822 | 1 57 6919 823 | 48 73 9724 824 | 53 66 7493 825 | 43 50 7138 826 | 92 97 4347 827 | 22 21 6355 828 | 28 60 5890 829 | 76 41 7290 830 | 92 56 6627 831 | 20 69 3937 832 | 99 83 8203 833 | 79 19 5347 834 | 15 78 5566 835 | 91 50 1701 836 | 67 63 5208 837 | 2 70 2984 838 | 55 49 8946 839 | 99 31 5828 840 | 23 31 3097 841 | 24 19 5936 842 | 25 84 5998 843 | 46 59 2661 844 | 79 83 5941 845 | 75 34 3782 846 | 13 79 2805 847 | 15 43 6671 848 | 3 94 8256 849 | 38 32 10593 850 | 3 100 3679 851 | 74 22 422 852 | 43 13 5258 853 | 27 74 8536 854 | 72 38 10093 855 | 76 70 2580 856 | 82 94 2615 857 | 67 59 7479 858 | 43 41 4354 859 | 55 56 7355 860 | 81 86 1223 861 | 81 83 6797 862 | 75 91 4883 863 | 77 45 6889 864 | 21 90 5319 865 | 44 82 3617 866 | 77 62 1100 867 | 56 54 5932 868 | 49 31 3220 869 | 53 67 3072 870 | 42 5 5315 871 | 5 23 4627 872 | 2 89 2688 873 | 53 30 2370 874 | 4 24 3089 875 | 67 58 3110 876 | 74 49 6300 877 | 88 22 4338 878 | 24 12 5231 879 | 67 90 8729 880 | 64 96 3023 881 | 21 6 3160 882 | 97 71 4132 883 | 66 59 9071 884 | 92 78 4893 885 | 6 13 7159 886 | 38 31 3462 887 | 90 23 6019 888 | 13 33 6834 889 | 95 54 3106 890 | 67 65 4813 891 | 79 31 3305 892 | 27 48 9221 893 | 61 71 4781 894 | 66 85 6490 895 | 89 91 2589 896 | 40 76 4026 897 | 61 29 4147 898 | 68 74 7403 899 | 34 80 5052 900 | 77 21 1101 901 | 35 50 1921 902 | 58 2 5779 903 | 79 43 5135 904 | 22 41 640 905 | 61 53 3983 906 | 47 82 3564 907 | 2 73 3325 908 | 57 46 5599 909 | 46 68 6901 910 | 4 92 4232 911 | 25 12 8594 912 | 1 86 5614 913 | 79 88 6678 914 | 73 88 9908 915 | 44 70 3437 916 | 11 55 6080 917 | 93 14 7694 918 | 19 98 2955 919 | 30 54 4202 920 | 91 16 1477 921 | 26 99 7029 922 | 81 97 5931 923 | 66 28 8608 924 | 75 76 4328 925 | 51 80 1006 926 | 2 47 8342 927 | 36 7 3153 928 | 11 81 6941 929 | 58 44 404 930 | 84 82 7201 931 | 14 69 1765 932 | 50 46 7359 933 | 28 49 3464 934 | 1 5 4151 935 | 8 30 3598 936 | 71 70 3084 937 | 17 25 4961 938 | 91 79 2166 939 | 77 94 4070 940 | 75 98 1255 941 | 31 30 3872 942 | 4 63 2810 943 | 57 43 5169 944 | 5 48 5892 945 | 87 71 1938 946 | 97 25 5758 947 | 47 62 4796 948 | 45 56 5920 949 | 82 57 2535 950 | 7 88 3986 951 | 52 81 7881 952 | 1 53 4413 953 | 50 72 5514 954 | 43 74 3499 955 | 51 69 9146 956 | 76 10 965 957 | 58 14 3496 958 | 85 89 3845 959 | 23 89 1616 960 | 1 72 8124 961 | 67 45 9377 962 | 98 21 1584 963 | 79 91 2166 964 | 70 65 1773 965 | 75 19 4078 966 | 36 3 3316 967 | 44 7 1047 968 | 23 21 1471 969 | 72 91 3862 970 | 78 68 6449 971 | 20 16 2920 972 | 47 15 7523 973 | 4 52 3297 974 | 27 83 7359 975 | 55 87 4938 976 | 18 82 5373 977 | 89 52 3529 978 | 49 37 3630 979 | 94 31 2958 980 | 29 1 4631 981 | 53 96 1095 982 | 83 49 4418 983 | 60 63 3076 984 | 43 10 5826 985 | 33 63 4847 986 | 96 6 4715 987 | 47 85 5463 988 | 50 63 2722 989 | 10 69 5134 990 | 30 54 4202 991 | 76 34 726 992 | 46 18 8255 993 | 84 8 6881 994 | 62 59 7953 995 | 63 59 7044 996 | 29 81 4741 997 | 45 6 9802 998 | 24 11 2927 999 | 1 31 5022 1000 | 55 90 1197 1001 | 18 58 5156 1002 | 54 1 4678 1003 | 12 85 5873 1004 | 37 71 5122 1005 | 68 41 7400 1006 | 76 36 4556 1007 | 69 71 3011 1008 | 72 51 1036 1009 | 77 25 4354 1010 | 50 62 2413 1011 | 13 56 4756 1012 | 44 45 5908 1013 | 39 53 3587 1014 | 46 74 593 1015 | 66 27 10684 1016 | 39 2 7637 1017 | 90 79 3793 1018 | 41 65 4833 1019 | 75 47 4146 1020 | 13 57 3051 1021 | 85 35 2876 1022 | 13 97 5988 1023 | 16 25 3809 1024 | 2 61 9565 1025 | 86 58 3645 1026 | 56 57 6524 1027 | 89 64 8050 1028 | 79 57 249 1029 | 64 23 7520 1030 | 71 20 1122 1031 | 62 25 3687 1032 | 56 85 4409 1033 | 91 1 8619 1034 | 56 75 4971 1035 | 56 47 8510 1036 | 50 17 6157 1037 | 39 93 7776 1038 | 7 56 5061 1039 | 67 81 1762 1040 | 67 83 5228 1041 | 89 24 7495 1042 | 3 47 8835 1043 | 65 15 2442 1044 | 46 5 5745 1045 | 88 40 9946 1046 | 10 100 1904 1047 | 91 66 4379 1048 | 7 69 2019 1049 | 96 21 3411 1050 | 90 60 2600 1051 | 59 21 8083 1052 | 95 4 5746 1053 | 69 33 4712 1054 | 55 43 7226 1055 | 99 64 9044 1056 | 11 60 4539 1057 | 35 32 2879 1058 | 10 62 1600 1059 | 35 91 1113 1060 | 78 23 7520 1061 | 5 35 6008 1062 | 86 97 5421 1063 | 89 29 3788 1064 | 22 36 2494 1065 | 24 75 3515 1066 | 2 25 3610 1067 | 82 3 5811 1068 | 72 30 4227 1069 | 89 72 6183 1070 | 37 3 9151 1071 | 81 56 8475 1072 | 28 66 8608 1073 | 35 75 3799 1074 | 15 95 8773 1075 | 77 79 1737 1076 | 47 61 5012 1077 | 98 16 4551 1078 | 59 34 8511 1079 | 40 92 5685 1080 | 32 40 7056 1081 | 44 84 3796 1082 | 7 97 4670 1083 | 84 55 7961 1084 | 32 70 2736 1085 | 97 35 3842 1086 | 47 40 5818 1087 | 14 100 5531 1088 | 76 45 5502 1089 | 38 71 4969 1090 | 38 55 9225 1091 | 90 31 7027 1092 | 4 85 1160 1093 | 31 19 2941 1094 | 19 86 1881 1095 | 20 17 3467 1096 | 98 20 2286 1097 | 75 2 5728 1098 | 56 41 1379 1099 | 43 29 2764 1100 | 91 38 8861 1101 | 45 57 5483 1102 | 1000 1103 | 23 89 1104 | 17 57 1105 | 14 85 1106 | 41 16 1107 | 54 73 1108 | 15 42 1109 | 25 9 1110 | 5 41 1111 | 43 13 1112 | 4 79 1113 | 7 98 1114 | 3 84 1115 | 92 11 1116 | 59 89 1117 | 24 86 1118 | 98 39 1119 | 36 59 1120 | 90 43 1121 | 7 10 1122 | 14 46 1123 | 77 5 1124 | 95 41 1125 | 69 5 1126 | 54 58 1127 | 51 19 1128 | 32 2 1129 | 15 59 1130 | 59 84 1131 | 54 59 1132 | 68 71 1133 | 78 31 1134 | 2 9 1135 | 96 11 1136 | 47 84 1137 | 41 100 1138 | 87 86 1139 | 62 85 1140 | 48 5 1141 | 83 44 1142 | 25 9 1143 | 57 1 1144 | 22 53 1145 | 8 68 1146 | 38 27 1147 | 64 79 1148 | 97 23 1149 | 70 15 1150 | 59 92 1151 | 43 11 1152 | 94 65 1153 | 22 11 1154 | 33 43 1155 | 62 31 1156 | 81 62 1157 | 52 92 1158 | 47 27 1159 | 95 89 1160 | 75 56 1161 | 62 88 1162 | 51 4 1163 | 69 37 1164 | 91 77 1165 | 78 76 1166 | 53 19 1167 | 24 17 1168 | 24 22 1169 | 93 33 1170 | 62 30 1171 | 6 33 1172 | 79 53 1173 | 6 50 1174 | 77 60 1175 | 11 60 1176 | 33 94 1177 | 37 83 1178 | 40 24 1179 | 88 78 1180 | 41 1 1181 | 19 1 1182 | 82 32 1183 | 25 49 1184 | 84 99 1185 | 87 97 1186 | 51 36 1187 | 79 15 1188 | 24 69 1189 | 51 68 1190 | 65 45 1191 | 50 13 1192 | 36 78 1193 | 58 53 1194 | 38 92 1195 | 92 75 1196 | 39 94 1197 | 47 66 1198 | 80 71 1199 | 54 64 1200 | 30 38 1201 | 99 10 1202 | 95 66 1203 | 11 20 1204 | 58 60 1205 | 26 88 1206 | 87 96 1207 | 87 67 1208 | 62 25 1209 | 55 16 1210 | 53 55 1211 | 95 20 1212 | 70 49 1213 | 98 42 1214 | 41 14 1215 | 4 4 1216 | 11 92 1217 | 42 78 1218 | 59 54 1219 | 27 70 1220 | 16 33 1221 | 98 22 1222 | 40 38 1223 | 80 19 1224 | 25 98 1225 | 68 39 1226 | 31 41 1227 | 60 26 1228 | 28 8 1229 | 68 18 1230 | 7 58 1231 | 94 89 1232 | 44 93 1233 | 90 65 1234 | 57 50 1235 | 87 43 1236 | 1 57 1237 | 49 14 1238 | 25 55 1239 | 60 44 1240 | 22 91 1241 | 75 24 1242 | 7 47 1243 | 11 22 1244 | 49 50 1245 | 89 95 1246 | 98 78 1247 | 83 74 1248 | 33 42 1249 | 74 26 1250 | 13 26 1251 | 60 82 1252 | 77 78 1253 | 65 8 1254 | 35 14 1255 | 16 7 1256 | 59 18 1257 | 30 13 1258 | 61 16 1259 | 6 70 1260 | 61 48 1261 | 92 31 1262 | 37 61 1263 | 84 20 1264 | 80 72 1265 | 50 17 1266 | 21 47 1267 | 28 15 1268 | 32 57 1269 | 94 94 1270 | 87 95 1271 | 16 79 1272 | 89 41 1273 | 3 67 1274 | 42 22 1275 | 90 13 1276 | 62 2 1277 | 77 96 1278 | 6 13 1279 | 54 62 1280 | 46 9 1281 | 14 41 1282 | 28 22 1283 | 91 81 1284 | 30 89 1285 | 10 94 1286 | 75 85 1287 | 81 49 1288 | 86 90 1289 | 54 41 1290 | 56 71 1291 | 66 18 1292 | 26 42 1293 | 4 80 1294 | 20 6 1295 | 25 52 1296 | 27 6 1297 | 85 94 1298 | 20 32 1299 | 99 41 1300 | 55 14 1301 | 13 1 1302 | 67 20 1303 | 48 44 1304 | 76 36 1305 | 93 26 1306 | 94 49 1307 | 38 95 1308 | 82 8 1309 | 56 34 1310 | 3 60 1311 | 82 90 1312 | 34 97 1313 | 81 75 1314 | 18 61 1315 | 1 42 1316 | 14 19 1317 | 49 30 1318 | 77 18 1319 | 81 32 1320 | 60 97 1321 | 25 86 1322 | 3 65 1323 | 70 38 1324 | 72 89 1325 | 13 45 1326 | 14 38 1327 | 40 13 1328 | 100 95 1329 | 53 37 1330 | 43 31 1331 | 64 45 1332 | 77 97 1333 | 2 9 1334 | 46 73 1335 | 25 45 1336 | 36 68 1337 | 22 58 1338 | 46 45 1339 | 50 21 1340 | 97 87 1341 | 22 82 1342 | 19 71 1343 | 79 70 1344 | 43 52 1345 | 29 69 1346 | 72 8 1347 | 8 15 1348 | 61 57 1349 | 64 46 1350 | 20 26 1351 | 88 52 1352 | 93 27 1353 | 24 33 1354 | 20 70 1355 | 35 31 1356 | 52 1 1357 | 8 75 1358 | 59 57 1359 | 38 62 1360 | 37 45 1361 | 45 1 1362 | 54 100 1363 | 58 85 1364 | 76 95 1365 | 35 13 1366 | 25 81 1367 | 87 66 1368 | 35 22 1369 | 74 45 1370 | 13 23 1371 | 33 81 1372 | 16 84 1373 | 24 72 1374 | 55 9 1375 | 58 76 1376 | 81 69 1377 | 11 7 1378 | 1 7 1379 | 65 27 1380 | 93 3 1381 | 60 76 1382 | 58 13 1383 | 99 71 1384 | 85 25 1385 | 51 87 1386 | 2 30 1387 | 5 61 1388 | 64 76 1389 | 90 47 1390 | 99 52 1391 | 20 50 1392 | 3 33 1393 | 53 50 1394 | 24 74 1395 | 68 64 1396 | 37 49 1397 | 94 26 1398 | 21 4 1399 | 77 56 1400 | 20 49 1401 | 83 8 1402 | 10 6 1403 | 56 32 1404 | 67 79 1405 | 54 51 1406 | 83 92 1407 | 60 29 1408 | 66 11 1409 | 82 32 1410 | 93 96 1411 | 95 30 1412 | 5 3 1413 | 5 95 1414 | 20 99 1415 | 6 33 1416 | 10 12 1417 | 93 48 1418 | 69 29 1419 | 90 75 1420 | 67 42 1421 | 69 92 1422 | 82 72 1423 | 69 7 1424 | 45 92 1425 | 21 27 1426 | 84 78 1427 | 50 88 1428 | 39 71 1429 | 16 63 1430 | 38 27 1431 | 42 92 1432 | 12 13 1433 | 83 91 1434 | 11 88 1435 | 60 60 1436 | 67 90 1437 | 11 67 1438 | 12 17 1439 | 80 18 1440 | 64 34 1441 | 79 58 1442 | 61 79 1443 | 91 18 1444 | 1 81 1445 | 65 78 1446 | 19 3 1447 | 15 8 1448 | 76 79 1449 | 82 15 1450 | 61 53 1451 | 27 27 1452 | 28 88 1453 | 48 27 1454 | 18 72 1455 | 98 21 1456 | 71 36 1457 | 98 61 1458 | 8 40 1459 | 78 4 1460 | 37 5 1461 | 10 96 1462 | 28 8 1463 | 33 41 1464 | 63 24 1465 | 19 86 1466 | 100 9 1467 | 20 43 1468 | 63 44 1469 | 61 1 1470 | 71 51 1471 | 41 97 1472 | 44 61 1473 | 68 24 1474 | 76 8 1475 | 75 35 1476 | 17 58 1477 | 97 75 1478 | 53 18 1479 | 7 86 1480 | 25 5 1481 | 66 17 1482 | 1 47 1483 | 81 75 1484 | 92 38 1485 | 93 85 1486 | 64 71 1487 | 30 2 1488 | 89 53 1489 | 100 53 1490 | 55 15 1491 | 34 78 1492 | 99 15 1493 | 20 83 1494 | 9 74 1495 | 63 89 1496 | 52 24 1497 | 8 87 1498 | 87 71 1499 | 68 42 1500 | 72 16 1501 | 25 23 1502 | 53 61 1503 | 2 40 1504 | 59 82 1505 | 55 70 1506 | 10 34 1507 | 23 37 1508 | 53 43 1509 | 9 15 1510 | 60 10 1511 | 8 51 1512 | 66 78 1513 | 61 42 1514 | 18 69 1515 | 1 50 1516 | 94 18 1517 | 98 86 1518 | 5 100 1519 | 55 63 1520 | 62 78 1521 | 74 94 1522 | 15 54 1523 | 81 2 1524 | 58 31 1525 | 26 46 1526 | 53 99 1527 | 42 23 1528 | 23 7 1529 | 25 67 1530 | 56 2 1531 | 85 97 1532 | 20 78 1533 | 96 16 1534 | 20 69 1535 | 56 52 1536 | 85 79 1537 | 89 14 1538 | 57 10 1539 | 10 43 1540 | 55 54 1541 | 17 52 1542 | 14 99 1543 | 65 97 1544 | 98 21 1545 | 90 90 1546 | 81 86 1547 | 7 11 1548 | 93 96 1549 | 6 1 1550 | 97 72 1551 | 93 16 1552 | 76 73 1553 | 30 2 1554 | 85 43 1555 | 59 27 1556 | 9 5 1557 | 78 67 1558 | 22 99 1559 | 90 10 1560 | 86 1 1561 | 11 19 1562 | 73 84 1563 | 60 70 1564 | 3 17 1565 | 85 56 1566 | 25 67 1567 | 85 29 1568 | 45 13 1569 | 74 48 1570 | 78 67 1571 | 7 89 1572 | 35 90 1573 | 79 62 1574 | 14 24 1575 | 52 24 1576 | 93 90 1577 | 16 42 1578 | 48 26 1579 | 32 48 1580 | 28 17 1581 | 28 38 1582 | 66 92 1583 | 20 21 1584 | 70 88 1585 | 88 60 1586 | 85 67 1587 | 58 63 1588 | 8 21 1589 | 4 32 1590 | 37 34 1591 | 39 56 1592 | 89 68 1593 | 28 87 1594 | 2 93 1595 | 9 25 1596 | 43 87 1597 | 60 19 1598 | 82 41 1599 | 32 73 1600 | 83 58 1601 | 59 83 1602 | 88 18 1603 | 85 38 1604 | 2 48 1605 | 14 61 1606 | 25 39 1607 | 39 4 1608 | 75 42 1609 | 78 48 1610 | 11 80 1611 | 90 98 1612 | 6 30 1613 | 2 3 1614 | 80 41 1615 | 78 70 1616 | 20 57 1617 | 18 76 1618 | 10 100 1619 | 17 85 1620 | 94 87 1621 | 10 54 1622 | 82 30 1623 | 36 46 1624 | 81 31 1625 | 18 79 1626 | 62 91 1627 | 95 13 1628 | 39 3 1629 | 65 12 1630 | 9 86 1631 | 22 17 1632 | 55 27 1633 | 52 71 1634 | 57 9 1635 | 69 30 1636 | 61 48 1637 | 31 32 1638 | 19 99 1639 | 3 94 1640 | 92 99 1641 | 38 68 1642 | 32 92 1643 | 49 3 1644 | 2 92 1645 | 25 39 1646 | 43 8 1647 | 17 65 1648 | 26 63 1649 | 24 50 1650 | 12 37 1651 | 43 87 1652 | 39 19 1653 | 41 94 1654 | 60 3 1655 | 64 36 1656 | 1 74 1657 | 88 70 1658 | 43 14 1659 | 1 91 1660 | 1 89 1661 | 47 7 1662 | 98 32 1663 | 87 75 1664 | 9 12 1665 | 91 25 1666 | 40 100 1667 | 30 56 1668 | 99 65 1669 | 81 63 1670 | 50 36 1671 | 3 12 1672 | 91 36 1673 | 92 36 1674 | 11 85 1675 | 78 52 1676 | 54 12 1677 | 60 88 1678 | 98 44 1679 | 58 84 1680 | 20 77 1681 | 14 98 1682 | 54 29 1683 | 34 89 1684 | 84 52 1685 | 79 8 1686 | 3 16 1687 | 13 50 1688 | 36 36 1689 | 48 10 1690 | 34 43 1691 | 17 26 1692 | 93 15 1693 | 12 43 1694 | 46 2 1695 | 23 11 1696 | 82 29 1697 | 84 42 1698 | 33 87 1699 | 100 41 1700 | 1 57 1701 | 88 2 1702 | 51 36 1703 | 35 29 1704 | 20 45 1705 | 76 56 1706 | 99 57 1707 | 61 9 1708 | 95 85 1709 | 14 13 1710 | 96 47 1711 | 89 43 1712 | 52 22 1713 | 20 2 1714 | 66 77 1715 | 15 7 1716 | 16 4 1717 | 35 79 1718 | 45 61 1719 | 65 84 1720 | 20 27 1721 | 30 34 1722 | 6 76 1723 | 23 40 1724 | 78 63 1725 | 23 32 1726 | 22 23 1727 | 27 94 1728 | 42 85 1729 | 67 73 1730 | 91 75 1731 | 65 36 1732 | 25 49 1733 | 27 65 1734 | 51 86 1735 | 87 56 1736 | 8 51 1737 | 90 65 1738 | 26 66 1739 | 16 59 1740 | 81 35 1741 | 35 94 1742 | 87 41 1743 | 43 80 1744 | 79 86 1745 | 53 80 1746 | 11 79 1747 | 97 65 1748 | 20 79 1749 | 44 42 1750 | 50 57 1751 | 24 19 1752 | 82 88 1753 | 98 29 1754 | 37 86 1755 | 51 62 1756 | 6 82 1757 | 20 12 1758 | 65 86 1759 | 39 61 1760 | 30 48 1761 | 22 10 1762 | 36 30 1763 | 33 89 1764 | 69 2 1765 | 67 98 1766 | 30 30 1767 | 9 20 1768 | 30 53 1769 | 74 76 1770 | 77 58 1771 | 41 19 1772 | 56 5 1773 | 59 1 1774 | 44 68 1775 | 32 31 1776 | 10 42 1777 | 75 53 1778 | 28 94 1779 | 11 63 1780 | 59 45 1781 | 55 62 1782 | 10 93 1783 | 21 88 1784 | 26 15 1785 | 53 64 1786 | 43 72 1787 | 29 99 1788 | 39 80 1789 | 35 83 1790 | 3 39 1791 | 79 6 1792 | 44 23 1793 | 43 68 1794 | 21 16 1795 | 99 43 1796 | 27 60 1797 | 23 14 1798 | 15 69 1799 | 90 57 1800 | 67 79 1801 | 67 61 1802 | 65 93 1803 | 53 4 1804 | 99 52 1805 | 11 61 1806 | 74 96 1807 | 65 17 1808 | 18 96 1809 | 40 97 1810 | 78 52 1811 | 98 70 1812 | 48 75 1813 | 97 83 1814 | 8 37 1815 | 47 35 1816 | 82 83 1817 | 73 43 1818 | 66 84 1819 | 42 28 1820 | 6 48 1821 | 29 24 1822 | 38 47 1823 | 45 58 1824 | 12 48 1825 | 17 33 1826 | 62 54 1827 | 10 33 1828 | 98 84 1829 | 88 23 1830 | 27 69 1831 | 63 71 1832 | 47 63 1833 | 32 50 1834 | 58 19 1835 | 72 60 1836 | 85 92 1837 | 81 46 1838 | 91 45 1839 | 60 16 1840 | 78 14 1841 | 96 86 1842 | 10 79 1843 | 72 11 1844 | 36 11 1845 | 11 17 1846 | 26 23 1847 | 56 46 1848 | 64 37 1849 | 44 93 1850 | 56 1 1851 | 99 97 1852 | 71 3 1853 | 38 77 1854 | 45 32 1855 | 61 87 1856 | 48 77 1857 | 12 11 1858 | 43 40 1859 | 16 2 1860 | 100 96 1861 | 6 66 1862 | 63 72 1863 | 62 88 1864 | 22 78 1865 | 8 15 1866 | 100 28 1867 | 55 92 1868 | 66 19 1869 | 74 100 1870 | 36 33 1871 | 96 35 1872 | 11 15 1873 | 11 61 1874 | 61 84 1875 | 85 76 1876 | 16 66 1877 | 85 42 1878 | 1 71 1879 | 42 80 1880 | 38 20 1881 | 4 46 1882 | 96 29 1883 | 91 14 1884 | 67 97 1885 | 100 83 1886 | 80 32 1887 | 18 25 1888 | 81 40 1889 | 81 21 1890 | 72 91 1891 | 46 42 1892 | 41 89 1893 | 11 55 1894 | 32 27 1895 | 1 53 1896 | 52 77 1897 | 100 54 1898 | 53 64 1899 | 18 87 1900 | 35 72 1901 | 80 40 1902 | 13 94 1903 | 30 74 1904 | 26 96 1905 | 31 46 1906 | 39 16 1907 | 79 85 1908 | 68 27 1909 | 46 43 1910 | 76 27 1911 | 96 81 1912 | 63 29 1913 | 3 41 1914 | 20 24 1915 | 95 7 1916 | 15 85 1917 | 91 26 1918 | 52 31 1919 | 63 56 1920 | 21 85 1921 | 10 90 1922 | 52 74 1923 | 57 72 1924 | 15 72 1925 | 52 13 1926 | 45 94 1927 | 92 28 1928 | 57 95 1929 | 50 88 1930 | 43 99 1931 | 11 70 1932 | 75 47 1933 | 37 50 1934 | 65 6 1935 | 28 97 1936 | 12 74 1937 | 92 63 1938 | 82 2 1939 | 89 35 1940 | 72 8 1941 | 92 33 1942 | 82 96 1943 | 97 46 1944 | 10 44 1945 | 89 3 1946 | 67 58 1947 | 29 25 1948 | 73 36 1949 | 99 89 1950 | 92 56 1951 | 83 81 1952 | 1 44 1953 | 22 13 1954 | 13 96 1955 | 32 12 1956 | 86 6 1957 | 99 41 1958 | 33 53 1959 | 41 51 1960 | 74 1 1961 | 52 76 1962 | 14 73 1963 | 14 32 1964 | 74 48 1965 | 41 70 1966 | 90 93 1967 | 3 61 1968 | 23 86 1969 | 34 42 1970 | 64 44 1971 | 5 60 1972 | 71 89 1973 | 52 91 1974 | 45 57 1975 | 12 41 1976 | 63 52 1977 | 49 23 1978 | 66 90 1979 | 78 40 1980 | 22 36 1981 | 79 71 1982 | 33 88 1983 | 7 26 1984 | 36 11 1985 | 89 42 1986 | 9 85 1987 | 39 33 1988 | 38 100 1989 | 28 92 1990 | 7 24 1991 | 55 34 1992 | 26 5 1993 | 91 19 1994 | 61 15 1995 | 54 98 1996 | 51 24 1997 | 53 22 1998 | 35 20 1999 | 22 38 2000 | 51 52 2001 | 64 54 2002 | 50 88 2003 | 87 30 2004 | 26 44 2005 | 32 47 2006 | 30 63 2007 | 86 25 2008 | 86 56 2009 | 19 12 2010 | 54 88 2011 | 5 99 2012 | 61 38 2013 | 79 56 2014 | 36 51 2015 | 47 9 2016 | 74 85 2017 | 60 60 2018 | 94 93 2019 | 48 2 2020 | 20 36 2021 | 1 49 2022 | 62 67 2023 | 93 53 2024 | 64 15 2025 | 44 79 2026 | 85 71 2027 | 91 42 2028 | 78 80 2029 | 95 90 2030 | 95 95 2031 | 82 25 2032 | 24 97 2033 | 47 82 2034 | 1 20 2035 | 32 33 2036 | 92 4 2037 | 69 75 2038 | 3 35 2039 | 8 19 2040 | 61 100 2041 | 62 47 2042 | 57 53 2043 | 27 50 2044 | 61 63 2045 | 14 13 2046 | 92 29 2047 | 70 87 2048 | 44 98 2049 | 81 87 2050 | 64 59 2051 | 45 46 2052 | 55 44 2053 | 18 39 2054 | 14 38 2055 | 15 29 2056 | 19 32 2057 | 77 82 2058 | 3 54 2059 | 56 37 2060 | 53 50 2061 | 1 14 2062 | 56 74 2063 | 88 47 2064 | 49 25 2065 | 58 65 2066 | 21 70 2067 | 4 32 2068 | 81 31 2069 | 71 76 2070 | 50 74 2071 | 94 24 2072 | 10 64 2073 | 12 78 2074 | 74 97 2075 | 100 98 2076 | 49 56 2077 | 30 99 2078 | 38 76 2079 | 42 24 2080 | 64 87 2081 | 69 39 2082 | 89 41 2083 | 36 96 2084 | 22 13 2085 | 70 26 2086 | 80 60 2087 | 38 56 2088 | 1 45 2089 | 85 57 2090 | 36 13 2091 | 6 26 2092 | 61 71 2093 | 97 83 2094 | 82 63 2095 | 97 85 2096 | 9 58 2097 | 6 25 2098 | 95 35 2099 | 86 54 2100 | 64 68 2101 | 94 17 2102 | 61 87 2103 | -------------------------------------------------------------------------------- /week 6/dist_with_coords/tests/03.a: -------------------------------------------------------------------------------- 1 | 1616 2 | 4633 3 | 4534 4 | 8141 5 | 12283 6 | 5372 7 | 11574 8 | 7413 9 | 5258 10 | 3295 11 | 1586 12 | 8357 13 | 7527 14 | 9536 15 | 6557 16 | 7032 17 | 4908 18 | 8858 19 | 9065 20 | 9249 21 | 4955 22 | 9719 23 | 5728 24 | 8641 25 | 11039 26 | 6470 27 | 8816 28 | 5481 29 | 8340 30 | 10089 31 | 10487 32 | 8440 33 | 10451 34 | 7957 35 | 8190 36 | 6605 37 | 5329 38 | 8911 39 | 5330 40 | 11574 41 | 7090 42 | 5087 43 | 9828 44 | 9526 45 | 10197 46 | 9431 47 | 1667 48 | 8362 49 | 6817 50 | 6356 51 | 5507 52 | 12301 53 | 6643 54 | 7214 55 | 4938 56 | 9071 57 | 8811 58 | 8762 59 | 9304 60 | 10094 61 | 13023 62 | 4594 63 | 7739 64 | 4441 65 | 5746 66 | 4907 67 | 9530 68 | 7248 69 | 8824 70 | 6182 71 | 5427 72 | 5516 73 | 4539 74 | 12281 75 | 8049 76 | 9877 77 | 5680 78 | 9760 79 | 8310 80 | 6715 81 | 7852 82 | 10011 83 | 8440 84 | 8786 85 | 6267 86 | 4766 87 | 9403 88 | 7343 89 | 8711 90 | 5994 91 | 6491 92 | 8252 93 | 4366 94 | 3449 95 | 12800 96 | 11249 97 | 7073 98 | 9746 99 | 2201 100 | 13229 101 | 4099 102 | 5846 103 | 9785 104 | 3372 105 | 7006 106 | 3687 107 | 5352 108 | 7049 109 | 8366 110 | 7718 111 | 4015 112 | 8212 113 | 0 114 | 6953 115 | 9620 116 | 7777 117 | 9783 118 | 3731 119 | 5922 120 | 9717 121 | 10867 122 | 5474 123 | 10656 124 | 7180 125 | 8870 126 | 5750 127 | 13058 128 | 5606 129 | 4768 130 | 4450 131 | 9367 132 | 2491 133 | 7332 134 | 6919 135 | 5651 136 | 6752 137 | 5433 138 | 6802 139 | 8554 140 | 4897 141 | 7195 142 | 8297 143 | 11469 144 | 9507 145 | 5276 146 | 10084 147 | 10552 148 | 10003 149 | 8825 150 | 8809 151 | 6924 152 | 8116 153 | 6456 154 | 10916 155 | 14399 156 | 8769 157 | 8245 158 | 6825 159 | 5543 160 | 7312 161 | 7979 162 | 11566 163 | 6157 164 | 8129 165 | 11528 166 | 6873 167 | 0 168 | 11041 169 | 5326 170 | 9718 171 | 8126 172 | 9740 173 | 10988 174 | 6439 175 | 5736 176 | 7159 177 | 9613 178 | 8924 179 | 8895 180 | 7177 181 | 9020 182 | 9907 183 | 5706 184 | 5800 185 | 8800 186 | 9349 187 | 7334 188 | 9784 189 | 12670 190 | 10250 191 | 8121 192 | 8532 193 | 5987 194 | 6983 195 | 9722 196 | 8914 197 | 8526 198 | 6718 199 | 10141 200 | 4460 201 | 11433 202 | 4556 203 | 9120 204 | 8421 205 | 13144 206 | 3834 207 | 8753 208 | 5164 209 | 6863 210 | 8334 211 | 5167 212 | 9113 213 | 9532 214 | 4085 215 | 6366 216 | 7616 217 | 10755 218 | 11150 219 | 7595 220 | 7668 221 | 8915 222 | 8012 223 | 9168 224 | 9305 225 | 7692 226 | 15073 227 | 7626 228 | 5568 229 | 12710 230 | 7911 231 | 8440 232 | 8160 233 | 9965 234 | 7525 235 | 5277 236 | 10271 237 | 4360 238 | 8973 239 | 8219 240 | 4919 241 | 6245 242 | 5370 243 | 6913 244 | 3996 245 | 6811 246 | 9292 247 | 13298 248 | 6720 249 | 6846 250 | 8690 251 | 9264 252 | 8422 253 | 6584 254 | 9865 255 | 8622 256 | 8075 257 | 6404 258 | 11759 259 | 12573 260 | 9906 261 | 1105 262 | 8616 263 | 6594 264 | 8527 265 | 11638 266 | 10142 267 | 10932 268 | 8791 269 | 10651 270 | 9205 271 | 10986 272 | 12863 273 | 4835 274 | 2220 275 | 5849 276 | 4985 277 | 6178 278 | 7688 279 | 10282 280 | 4513 281 | 7003 282 | 4863 283 | 8480 284 | 7805 285 | 8021 286 | 8217 287 | 9654 288 | 2760 289 | 3250 290 | 8375 291 | 5006 292 | 9097 293 | 9269 294 | 11737 295 | 9452 296 | 9347 297 | 9109 298 | 11226 299 | 8154 300 | 8948 301 | 10164 302 | 4975 303 | 11389 304 | 8082 305 | 5860 306 | 11834 307 | 6715 308 | 8489 309 | 7308 310 | 8250 311 | 11346 312 | 8199 313 | 8824 314 | 10652 315 | 9036 316 | 6658 317 | 7389 318 | 6106 319 | 8769 320 | 8759 321 | 5484 322 | 7413 323 | 10779 324 | 3678 325 | 8679 326 | 6128 327 | 3052 328 | 9526 329 | 4903 330 | 9276 331 | 8998 332 | 9005 333 | 0 334 | 8729 335 | 9866 336 | 4800 337 | 13100 338 | 7572 339 | 7881 340 | 9043 341 | 9335 342 | 9247 343 | 8008 344 | 8503 345 | 5308 346 | 4425 347 | 7594 348 | 3983 349 | 0 350 | 11616 351 | 13819 352 | 11147 353 | 1584 354 | 7982 355 | 10027 356 | 9436 357 | 12553 358 | 7135 359 | 6236 360 | 5750 361 | 10815 362 | 7959 363 | 1881 364 | 8827 365 | 10936 366 | 2357 367 | 8778 368 | 8049 369 | 9872 370 | 5393 371 | 8714 372 | 3862 373 | 5996 374 | 6947 375 | 5661 376 | 6264 377 | 3167 378 | 8924 379 | 9650 380 | 8426 381 | 5167 382 | 11081 383 | 4298 384 | 8825 385 | 12840 386 | 8450 387 | 9265 388 | 3384 389 | 5766 390 | 5483 391 | 9159 392 | 6136 393 | 5105 394 | 11580 395 | 8365 396 | 1938 397 | 8689 398 | 5339 399 | 7727 400 | 9521 401 | 9315 402 | 9053 403 | 9960 404 | 7164 405 | 12803 406 | 6798 407 | 7279 408 | 11041 409 | 9412 410 | 12456 411 | 8670 412 | 5433 413 | 9410 414 | 6277 415 | 5908 416 | 7994 417 | 5707 418 | 8485 419 | 10155 420 | 7241 421 | 9816 422 | 2419 423 | 10999 424 | 5916 425 | 8211 426 | 7380 427 | 6928 428 | 9367 429 | 7996 430 | 10266 431 | 7070 432 | 3937 433 | 6741 434 | 2135 435 | 10119 436 | 7759 437 | 10196 438 | 7282 439 | 4647 440 | 7302 441 | 7191 442 | 1584 443 | 0 444 | 1223 445 | 7482 446 | 8489 447 | 8453 448 | 7663 449 | 7981 450 | 7429 451 | 12840 452 | 7270 453 | 10056 454 | 8034 455 | 11534 456 | 7472 457 | 7481 458 | 6429 459 | 7060 460 | 10914 461 | 8870 462 | 10164 463 | 8322 464 | 6928 465 | 4843 466 | 10680 467 | 8646 468 | 11534 469 | 5717 470 | 3027 471 | 5153 472 | 6489 473 | 11580 474 | 6221 475 | 3404 476 | 15750 477 | 12141 478 | 9094 479 | 8959 480 | 10804 481 | 7176 482 | 8280 483 | 7663 484 | 7227 485 | 2803 486 | 5492 487 | 6915 488 | 7686 489 | 9220 490 | 7898 491 | 4839 492 | 7516 493 | 4824 494 | 6968 495 | 9936 496 | 9789 497 | 3317 498 | 4926 499 | 4549 500 | 5891 501 | 8686 502 | 11754 503 | 5291 504 | 9017 505 | 8818 506 | 5270 507 | 13073 508 | 9600 509 | 6587 510 | 9420 511 | 6326 512 | 5926 513 | 8589 514 | 7027 515 | 8720 516 | 1904 517 | 2249 518 | 4636 519 | 5208 520 | 7202 521 | 2247 522 | 4414 523 | 8396 524 | 5995 525 | 10524 526 | 9712 527 | 6871 528 | 8895 529 | 5636 530 | 11034 531 | 8537 532 | 11316 533 | 9415 534 | 6825 535 | 8488 536 | 7384 537 | 8256 538 | 9881 539 | 8591 540 | 8803 541 | 9791 542 | 5280 543 | 9017 544 | 6377 545 | 6728 546 | 10034 547 | 8158 548 | 12533 549 | 6968 550 | 1209 551 | 10621 552 | 8179 553 | 11169 554 | 9159 555 | 9148 556 | 6645 557 | 9659 558 | 9648 559 | 6249 560 | 7870 561 | 3467 562 | 10012 563 | 3100 564 | 6895 565 | 15050 566 | 5589 567 | 7671 568 | 5128 569 | 9976 570 | 6462 571 | 10664 572 | 2639 573 | 9263 574 | 5764 575 | 7847 576 | 6144 577 | 4200 578 | 6509 579 | 4961 580 | 1917 581 | 6468 582 | 8917 583 | 1548 584 | 4644 585 | 4391 586 | 0 587 | 11178 588 | 7422 589 | 8509 590 | 6013 591 | 8677 592 | 9263 593 | 10113 594 | 4917 595 | 9728 596 | 10119 597 | 10269 598 | 6919 599 | 9812 600 | 8786 601 | 5987 602 | 9406 603 | 7827 604 | 5910 605 | 10768 606 | 6906 607 | 8009 608 | 6115 609 | 9675 610 | 9524 611 | 6655 612 | 12561 613 | 4650 614 | 7165 615 | 3279 616 | 15021 617 | 6047 618 | 9399 619 | 10892 620 | 8809 621 | 5415 622 | 9676 623 | 6504 624 | 7824 625 | 5035 626 | 8384 627 | 9649 628 | 4912 629 | 8766 630 | 7852 631 | 6689 632 | 11131 633 | 7120 634 | 9412 635 | 9367 636 | 12928 637 | 10096 638 | 8105 639 | 8341 640 | 7813 641 | 10194 642 | 5504 643 | 8482 644 | 4774 645 | 8989 646 | 7851 647 | 5617 648 | 5245 649 | 5936 650 | 6778 651 | 1383 652 | 5608 653 | 7216 654 | 6787 655 | 9183 656 | 5688 657 | 7209 658 | 13796 659 | 8015 660 | 4963 661 | 9952 662 | 9812 663 | 2174 664 | 0 665 | 6132 666 | 13293 667 | 7472 668 | 5324 669 | 9301 670 | 5806 671 | 11422 672 | 7144 673 | 7998 674 | 8219 675 | 5984 676 | 9732 677 | 4337 678 | 11344 679 | 4875 680 | 7537 681 | 6284 682 | 12512 683 | 6354 684 | 9140 685 | 5244 686 | 11951 687 | 8457 688 | 8992 689 | 7960 690 | 5148 691 | 7891 692 | 5849 693 | 9383 694 | 9143 695 | 8055 696 | 6669 697 | 4042 698 | 4975 699 | 8907 700 | 11360 701 | 8871 702 | 2760 703 | 7331 704 | 6604 705 | 9162 706 | 9040 707 | 9200 708 | 9263 709 | 5896 710 | 11536 711 | 10319 712 | 11290 713 | 7400 714 | 8740 715 | 10045 716 | 9341 717 | 10102 718 | 14738 719 | 7807 720 | 7391 721 | 9980 722 | 8745 723 | 7626 724 | 3608 725 | 4043 726 | 8827 727 | 7438 728 | 5115 729 | 7819 730 | 7161 731 | 6041 732 | 4585 733 | 8124 734 | 4314 735 | 8433 736 | 5012 737 | 4703 738 | 7717 739 | 4385 740 | 9064 741 | 5479 742 | 6117 743 | 7566 744 | 9943 745 | 6695 746 | 11581 747 | 4450 748 | 9564 749 | 9162 750 | 4866 751 | 6713 752 | 10754 753 | 7201 754 | 11737 755 | 8431 756 | 8800 757 | 6387 758 | 10360 759 | 11282 760 | 10169 761 | 9304 762 | 8488 763 | 6811 764 | 7621 765 | 7169 766 | 12440 767 | 9165 768 | 8014 769 | 7775 770 | 8127 771 | 7331 772 | 8081 773 | 5804 774 | 10332 775 | 4916 776 | 8129 777 | 9437 778 | 6091 779 | 9143 780 | 4187 781 | 7530 782 | 7693 783 | 8669 784 | 14184 785 | 9840 786 | 9197 787 | 5253 788 | 3862 789 | 8829 790 | 7880 791 | 6080 792 | 10687 793 | 4413 794 | 6459 795 | 9397 796 | 6354 797 | 6094 798 | 4975 799 | 10602 800 | 9607 801 | 10474 802 | 7835 803 | 8089 804 | 7919 805 | 8368 806 | 9070 807 | 6688 808 | 8724 809 | 8848 810 | 4484 811 | 5405 812 | 6558 813 | 8273 814 | 5837 815 | 9367 816 | 6058 817 | 6624 818 | 7890 819 | 9502 820 | 9102 821 | 7645 822 | 7152 823 | 7875 824 | 10284 825 | 8696 826 | 10140 827 | 8679 828 | 7801 829 | 6460 830 | 4146 831 | 6971 832 | 7759 833 | 11041 834 | 9129 835 | 9620 836 | 5547 837 | 3702 838 | 3996 839 | 9436 840 | 8135 841 | 8564 842 | 6228 843 | 7542 844 | 3110 845 | 7674 846 | 9421 847 | 7207 848 | 6627 849 | 9282 850 | 9555 851 | 4524 852 | 5903 853 | 11380 854 | 4645 855 | 8526 856 | 10642 857 | 11743 858 | 9013 859 | 4209 860 | 9175 861 | 8489 862 | 8646 863 | 4933 864 | 9532 865 | 12476 866 | 5252 867 | 5494 868 | 10251 869 | 8070 870 | 5652 871 | 2394 872 | 5483 873 | 9669 874 | 5456 875 | 6092 876 | 11899 877 | 11803 878 | 2494 879 | 6921 880 | 12237 881 | 7462 882 | 6117 883 | 7470 884 | 4672 885 | 5157 886 | 8888 887 | 8932 888 | 4555 889 | 4235 890 | 5316 891 | 7513 892 | 9532 893 | 7893 894 | 11612 895 | 5168 896 | 3934 897 | 6806 898 | 7524 899 | 9231 900 | 8679 901 | 6546 902 | 7838 903 | 10504 904 | 11833 905 | 8329 906 | 8316 907 | 10064 908 | 10293 909 | 7700 910 | 8501 911 | 8133 912 | 4822 913 | 11951 914 | 4292 915 | 0 916 | 9381 917 | 11138 918 | 8378 919 | 8409 920 | 7863 921 | 7394 922 | 8125 923 | 2836 924 | 8408 925 | 4881 926 | 5845 927 | 11112 928 | 0 929 | 5823 930 | 10524 931 | 3564 932 | 7803 933 | 10102 934 | 8446 935 | 4540 936 | 2763 937 | 7935 938 | 9216 939 | 6202 940 | 5933 941 | 5851 942 | 6371 943 | 8009 944 | 7004 945 | 6409 946 | 2633 947 | 6092 948 | 11130 949 | 10464 950 | 8064 951 | 10247 952 | 9305 953 | 5980 954 | 10415 955 | 4972 956 | 6669 957 | 13510 958 | 5006 959 | 6881 960 | 6966 961 | 5826 962 | 8536 963 | 4542 964 | 10550 965 | 6915 966 | 4414 967 | 5664 968 | 7952 969 | 8654 970 | 9883 971 | 9261 972 | 10739 973 | 7095 974 | 9629 975 | 10762 976 | 10633 977 | 10353 978 | 6887 979 | 6855 980 | 9718 981 | 3688 982 | 4524 983 | 8973 984 | 10526 985 | 10151 986 | 13249 987 | 2384 988 | 6631 989 | 8315 990 | 4781 991 | 10319 992 | 5545 993 | 6259 994 | 6079 995 | 9566 996 | 9782 997 | 7213 998 | 10338 999 | 5563 1000 | 7201 1001 | -------------------------------------------------------------------------------- /week 6/friend_suggestion/FriendSuggestion.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.util.ArrayList; 3 | import java.util.Arrays; 4 | import java.util.PriorityQueue; 5 | 6 | public class FriendSuggestion { 7 | private static class Impl { 8 | // Number of nodes 9 | int n; 10 | // adj[0] and cost[0] store the initial graph, adj[1] and cost[1] store the reversed graph. 11 | // Each graph is stored as array of adjacency lists for each node. adj stores the edges, 12 | // and cost stores their costs. 13 | ArrayList[][] adj; 14 | ArrayList[][] cost; 15 | // distance[0] and distance[1] correspond to distance estimates in the forward and backward searches. 16 | Long[][] distance; 17 | // Two priority queues, one for forward and one for backward search. 18 | ArrayList> queue; 19 | // visited[v] == true iff v was visited either by forward or backward search. 20 | boolean[] visited; 21 | // List of all the nodes which were visited either by forward or backward search. 22 | ArrayList workset; 23 | final Long INFINITY = Long.MAX_VALUE / 4; 24 | 25 | Impl(int n) { 26 | this.n = n; 27 | visited = new boolean[n]; 28 | Arrays.fill(visited, false); 29 | workset = new ArrayList(); 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 | } 34 | queue = new ArrayList>(); 35 | queue.add(new PriorityQueue(n)); 36 | queue.add(new PriorityQueue(n)); 37 | } 38 | 39 | // Reinitialize the data structures before new query after the previous query 40 | void clear() { 41 | for (int v : workset) { 42 | distance[0][v] = distance[1][v] = INFINITY; 43 | visited[v] = false; 44 | } 45 | workset.clear(); 46 | queue.get(0).clear(); 47 | queue.get(1).clear(); 48 | } 49 | 50 | // Try to relax the distance from direction side to node v using value dist. 51 | void visit(int side, int v, Long dist) { 52 | // Implement this method yourself 53 | if ( distance[side][v] > dist) { 54 | distance[side][v] = dist; 55 | queue.get(side).add(new Entry(distance[side][v], v)); 56 | workset.add(v); 57 | } 58 | } 59 | 60 | int extractMin(int side) { 61 | Entry e = queue.get(side).poll(); 62 | return e.node; 63 | } 64 | 65 | void Process(int side, int u, ArrayList[] adj, ArrayList[] cost) { 66 | for (int i = 0; i < adj[u].size(); ++i) { 67 | int v = adj[u].get(i); 68 | int w = cost[u].get(i); 69 | visit(side, v, distance[side][u] + w); 70 | } 71 | 72 | } 73 | 74 | Long ShortestPath(int v) { 75 | Long dist = INFINITY; 76 | for(int u: workset) { 77 | if (distance[0][u] + distance[1][u] < dist) { 78 | dist = distance[0][u] + distance[1][u]; 79 | } 80 | } 81 | if (dist == INFINITY) 82 | return -1L; 83 | return dist; 84 | } 85 | 86 | // Returns the distance from s to t in the graph. 87 | Long query(int s, int t) { 88 | clear(); 89 | visit(0, s, 0L); 90 | visit(1, t, 0L); 91 | // Implement the rest of the algorithm yourself 92 | while (!queue.get(0).isEmpty() && !queue.get(1).isEmpty()) { 93 | int v = extractMin(0); 94 | Process(0, v, adj[0], cost[0]); 95 | if (visited[v] == true) 96 | return ShortestPath(v); 97 | visited[v] = true; 98 | 99 | int v_r = extractMin(1); 100 | Process(1, v_r, adj[1], cost[1]); 101 | if (visited[v_r] == true) 102 | return ShortestPath(v_r); 103 | visited[v_r] = true; 104 | } 105 | return -1L; 106 | } 107 | 108 | class Entry implements Comparable 109 | { 110 | Long cost; 111 | int node; 112 | 113 | public Entry(Long cost, int node) 114 | { 115 | this.cost = cost; 116 | this.node = node; 117 | } 118 | 119 | public int compareTo(Entry other) 120 | { 121 | return cost < other.cost ? -1 : cost > other.cost ? 1 : 0; 122 | } 123 | } 124 | } 125 | 126 | public static void main(String args[]) { 127 | Scanner in = new Scanner(System.in); 128 | int n = in.nextInt(); 129 | int m = in.nextInt(); 130 | Impl bidij = new Impl(n); 131 | bidij.adj = (ArrayList[][])new ArrayList[2][]; 132 | bidij.cost = (ArrayList[][])new ArrayList[2][]; 133 | for (int side = 0; side < 2; ++side) { 134 | bidij.adj[side] = (ArrayList[])new ArrayList[n]; 135 | bidij.cost[side] = (ArrayList[])new ArrayList[n]; 136 | for (int i = 0; i < n; i++) { 137 | bidij.adj[side][i] = new ArrayList(); 138 | bidij.cost[side][i] = new ArrayList(); 139 | } 140 | } 141 | 142 | for (int i = 0; i < m; i++) { 143 | int x, y, c; 144 | x = in.nextInt(); 145 | y = in.nextInt(); 146 | c = in.nextInt(); 147 | bidij.adj[0][x - 1].add(y - 1); 148 | bidij.cost[0][x - 1].add(c); 149 | bidij.adj[1][y - 1].add(x - 1); 150 | bidij.cost[1][y - 1].add(c); 151 | } 152 | 153 | int t = in.nextInt(); 154 | 155 | for (int i = 0; i < t; i++) { 156 | int u, v; 157 | u = in.nextInt(); 158 | v = in.nextInt(); 159 | System.out.println(bidij.query(u-1, v-1)); 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /week 6/friend_suggestion/friend_suggestion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | // External vector of size 2 - for forward and backward search. 11 | // Internal 2-dimensional vector is vector of adjacency lists for each node. 12 | typedef vector>> Adj; 13 | 14 | // Distances can grow out of int type 15 | typedef long long Len; 16 | 17 | // Vector of two priority queues - for forward and backward searches. 18 | // Each priority queue stores the closest unprocessed node in its head. 19 | typedef vector,vector>,greater>>> Queue; 20 | 21 | const Len INFINITY = numeric_limits::max() / 4; 22 | 23 | class Bidijkstra { 24 | // Number of nodes 25 | int n_; 26 | // Graph adj_[0] and cost_[0] correspond to the initial graph, 27 | // adj_[1] and cost_[1] correspond to the reversed graph. 28 | // Graphs are stored as vectors of adjacency lists corresponding 29 | // to nodes. 30 | // Adjacency list itself is stored in adj_, and the corresponding 31 | // edge costs are stored in cost_. 32 | Adj adj_; 33 | Adj cost_; 34 | // distance_[0] stores distances for the forward search, 35 | // and distance_[1] stores distances for the backward search. 36 | vector> distance_; 37 | // Stores all the nodes visited either by forward or backward search. 38 | vector workset_; 39 | // Stores a flag for each node which is True iff the node was visited 40 | // either by forward or backward search. 41 | vector visited_; 42 | 43 | public: 44 | Bidijkstra(int n, Adj adj, Adj cost) 45 | : n_(n), adj_(adj), cost_(cost), distance_(2, vector(n, INFINITY)), visited_(n) 46 | { workset_.reserve(n); } 47 | 48 | // Initialize the data structures before new query, 49 | // clear the changes made by the previous query. 50 | void clear() { 51 | for (int i = 0; i < workset_.size(); ++i) { 52 | int v = workset_[i]; 53 | distance_[0][v] = distance_[1][v] = INFINITY; 54 | visited_[v] = false; 55 | } 56 | workset_.clear(); 57 | } 58 | 59 | // Processes visit of either forward or backward search 60 | // (determined by value of side), to node v trying to 61 | // relax the current distance by dist. 62 | void visit(Queue& q, int side, int v, Len dist) { 63 | // Implement this method yourself 64 | if ( distance_[side][v] > dist) { 65 | distance_[side][v] = dist; 66 | q[side].push({distance_[side][v], v}); 67 | workset_.push_back(v); 68 | } 69 | } 70 | 71 | int extractMin(Queue& q, int side) { 72 | pair p = q[side].top(); 73 | q[side].pop(); 74 | return p.second; 75 | } 76 | 77 | void Process(Queue& q, int side, int u, vector> &adj, const vector> &cost) { 78 | for (int i = 0; i < adj[u].size(); ++i) { 79 | int v = adj[u][i]; 80 | int w = cost[u][i]; 81 | visit(q, side, v, distance_[side][u] + w); 82 | } 83 | } 84 | 85 | Len ShortestPath(int v) { 86 | Len dist = INFINITY; 87 | for(int u: workset_) { 88 | if (distance_[0][u] + distance_[1][u] < dist) { 89 | dist = distance_[0][u] + distance_[1][u]; 90 | } 91 | } 92 | if (dist == INFINITY) 93 | return -1; 94 | return dist; 95 | } 96 | 97 | // Returns the distance from s to t in the graph. 98 | Len query(int s, int t) { 99 | clear(); 100 | Queue q(2); 101 | visit(q, 0, s, 0); 102 | visit(q, 1, t, 0); 103 | // Implement the rest of the algorithm yourself 104 | while (!q[0].empty() && !q[1].empty()) { 105 | int v = extractMin(q, 0); 106 | Process(q, 0, v, adj_[0], cost_[0]); 107 | if (visited_[v] == true) 108 | return ShortestPath(v); 109 | visited_[v] = true; 110 | 111 | int v_r = extractMin(q, 1); 112 | Process(q, 1, v_r, adj_[1], cost_[1]); 113 | if (visited_[v_r] == true) 114 | return ShortestPath(v_r); 115 | visited_[v_r] = true; 116 | } 117 | return -1; 118 | } 119 | }; 120 | 121 | int main() { 122 | int n, m; 123 | scanf("%d%d", &n, &m); 124 | Adj adj(2, vector>(n)); 125 | Adj cost(2, vector>(n)); 126 | for (int i=0; i dist: 25 | self.d[side][v] = dist 26 | q[side].put((self.d[side][v], v)) 27 | self.workset.append(v) 28 | 29 | def extract_min(self, q, side): 30 | _, v = q[side].get() 31 | return v 32 | 33 | def process(self, q, side, v, adj, cost): 34 | for u, w in zip(adj[v], cost[v]): 35 | self.visit(q, side, u, self.d[side][v] + w) 36 | 37 | def shortest_path(self, v): 38 | distance = self.inf 39 | for u in self.workset: 40 | if self.d[0][u] + self.d[1][u] < distance: 41 | distance = self.d[0][u] + self.d[1][u] 42 | return (distance if distance != self.inf else -1) 43 | 44 | def query(self, adj, cost, s, t): 45 | self.clear() 46 | q = [queue.PriorityQueue(), queue.PriorityQueue()] 47 | self.visit(q, 0, s, 0) 48 | self.visit(q, 1, t, 0) 49 | # Implement the rest of the algorithm yourself 50 | while not q[0].empty() and not q[1].empty(): 51 | for side in [0,1]: 52 | v = self.extract_min(q, side) 53 | self.process(q, side, v, adj[side], cost[side]) 54 | if self.visited[v]: 55 | return self.shortest_path(v) 56 | self.visited[v] = True 57 | return -1 58 | 59 | 60 | def readl(): 61 | return map(int, sys.stdin.readline().split()) 62 | 63 | 64 | if __name__ == '__main__': 65 | n,m = readl() 66 | adj = [[[] for _ in range(n)], [[] for _ in range(n)]] 67 | cost = [[[] for _ in range(n)], [[] for _ in range(n)]] 68 | for e in range(m): 69 | u,v,c = readl() 70 | adj[0][u-1].append(v-1) 71 | cost[0][u-1].append(c) 72 | adj[1][v-1].append(u-1) 73 | cost[1][v-1].append(c) 74 | t, = readl() 75 | bidij = BiDij(n) 76 | for i in range(t): 77 | s, t = readl() 78 | print(bidij.query(adj, cost, s-1, t-1)) 79 | --------------------------------------------------------------------------------