def distance(adj, cost, s, t): #write your code here #initialize max_weight = 1 for weight in cost: max_weight += sum(weight) weight = [max_weight for _ in range(len(adj))] relaxed = [0 for _ in range(len(adj))] queue = [(0, s)] weight[s] = 0 #bfs while queue: queue.sort() tempNode = queue.pop(0)[1] for i, end_node in enumerate(adj[tempNode]): if relaxed[end_node] != 2: tweight = cost[tempNode][i] + weight[tempNode] if weight[end_node] > tweight: if relaxed[end_node] == 1: queue.remove((weight[end_node], end_node)) weight[end_node] = tweight queue.append((weight[end_node], end_node)) relaxed[end_node] = 1 if relaxed[end_node] == 0: queue.append((weight[end_node], end_node)) relaxed[end_node] = 1 relaxed[tempNode] = 2 if weight[t] < max_weight: return weight[t] return -1
def cmd_q_minus(self, person=None, **kw): out = [] if not person: out.append('Know which person to remove from queue I do not.') return out queue.remove(person) out.append('In the queue: %s' % ', '.join(queue.listing())) return out
def next_fit(mem, plist): mem = list(mem) t = 0 print("time " + str(t) + "ms: Simulator started (Contiguous -- Next-Fit)") done = 0 count = 0 queue = [] for p in plist: count += p.times pos = 0 while done < count: for p in plist: for time in p.depart: if t == time and p.id in queue: remove(mem, p) done += 1 queue.remove(p.id) print("time " + str(t) + "ms: Process " + p.id + " removed:") printmem(mem) for p in plist: for time in p.arrival: if t == time: print("time " + str(t) + "ms: Process " + p.id + " arrived (requires " + str(p.size) + " frames)") if (room(mem) < p.size): print("time " + str(t) + "ms: Cannot place process " + p.id + " -- skipped!") done += 1 else: pos = search_first_place(mem, p, pos) if (pos == -1): print("time " + str(t) + "ms: Cannot place process " + p.id + " -- starting defragmentation") defrag_time, moved, movedlist = defragmentation( mem) update_time(plist, t, defrag_time) t += defrag_time print("time " + str(t) + "ms: Defragmentation complete (moved " + str(moved) + " frames: " + print_movedlist(movedlist) + ")") printmem(mem) pos = search_first_place(mem, p, pos) queue.append(p.id) add(mem, p, pos) print("time " + str(t) + "ms: Placed process " + p.id + ":") pos += p.size if (pos >= 256): pos -= 256 printmem(mem) t += 1 print("time " + str(t - 1) + "ms: Simulator ended (Contiguous -- Next-Fit)\n")
def part_one(lines): nodes, edges, rev_edges = make_graph(lines) can_contain = set() queue = ['shiny gold'] while len(queue) > 0: curr = queue[0] queue.remove(curr) up_nodes = rev_edges[curr].keys() can_contain.update(up_nodes) queue.extend(up_nodes) print(len(can_contain))
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): print("fire test") queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in knu.fire_place_num: knu.fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i not in knu.exit: #not in fire_place_num: queue.insert(0,i) for i in queue: substract = [] node_list = [] shortest_node_num = [] temp_exit_diret = 0 for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) #print(node_list) shortest_distance_node_index = [i for i, value in enumerate(substract) if value == min(substract)] temp_exit_diret = node[i].exit_diret if (min(substract) != -1 or node[i].exit_diret_num != shortest_distance_node_index) and min(substract) != float('inf'): node[i].exit_diret = 0 if send2 not in node[i].visited_place: node[i].visited_place.append(send2) print('7') #node[i].distance += 2 node[i].set_exit_diret_num(shortest_distance_node_index) for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if node[i].forward != None and node[i].forward.index in shortest_node_num: if node[i].forward.index not in node[i].visited_place: node[i].exit_diret += 32 if node[i].backward != None and node[i].backward.index in shortest_node_num : if node[i].backward.index not in node[i].visited_place: node[i].exit_diret += 16 if node[i].right != None and node[i].right.index in shortest_node_num : if node[i].right.index not in node[i].visited_place: node[i].exit_diret += 4 if node[i].left != None and node[i].left.index in shortest_node_num: if node[i].left.index not in node[i].visited_place: node[i].exit_diret += 8 if node[i].up != None and node[i].up.index in shortest_node_num : if node[i].up.index not in node[i].visited_place: node[i].exit_diret += 2 if node[i].down != None and node[i].down.index in shortest_node_num: if node[i].down.index not in node[i].visited_place: node[i].exit_diret += 1 else: del_list.append(i) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start,send2,i,node,linked_node_list)
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in fire_place_num: fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i: #not in fire_place_num: queue.insert(0, i) for i in queue: substract = [] node_list = [] shortest_node_num = [] temp_exit_diret = [] print("in ->", i) for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) #print(node_list) shortest_distance_node_index = [ i for i, value in enumerate(substract) if value == min(substract) ] temp_exit_diret = node[i].exit_diret if (min(substract) != -1 or node[i].exit_diret_num != shortest_distance_node_index ) and min(substract) != float('inf'): node[i].exit_diret = [] print(i, " in change") if send2 not in node[i].visited_place: node[i].visited_place.append(send2) #node[i].distance += 2 node[i].set_exit_diret_num(shortest_distance_node_index) for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if i == 19: print(node_list) print(substract) print(node[19].visited_place) print(shortest_node_num) print(node[i].exit_diret) print("short =>", shortest_node_num) if node[i].forward != None and node[ i].forward.index in shortest_node_num: if node[i].forward.index not in node[i].visited_place: node[i].exit_diret.append('forward') if node[i].backward != None and node[ i].backward.index in shortest_node_num: if node[i].backward.index not in node[i].visited_place: node[i].exit_diret.append('backward') if node[i].right != None and node[ i].right.index in shortest_node_num: if node[i].right.index not in node[i].visited_place: node[i].exit_diret.append('right') if node[i].left != None and node[i].left.index in shortest_node_num: if node[i].left.index not in node[i].visited_place: node[i].exit_diret.append('left') if node[i].up != None and node[i].up.index in shortest_node_num: if node[i].up.index not in node[i].visited_place: node[i].exit_diret.append('up') if node[i].down != None and node[i].down.index in shortest_node_num: if node[i].down.index not in node[i].visited_place: node[i].exit_diret.append('down') if i == 19: print("exit1", node[19].exit_diret) if not node[i].exit_diret: node[i].exit_diret = temp_exit_diret if i == 19: print("exit2", node[19].exit_diret) else: del_list.append(i) print(i, "visited_place ->", node[i].visited_place) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start, send2, i, node, linked_node_list)
def fire_test(fire_node_num, send_node_num1, send_node_num2, node, linked_node_list): queue = [] del_list = [] fire_start = fire_node_num send1 = send_node_num1 send2 = send_node_num2 node[fire_node_num].distance = float('inf') if fire_node_num not in fire_place_num: fire_place_num.append(fire_node_num) for i in linked_node_list[send2]: if i != send1 and i not in fire_place_num: queue.insert(0, i) for i in queue: substract = [] node_list = [] shortest_node_num = [] for j in linked_node_list[i]: node_list.append(j) if j != send2: substract.append(node[j].distance - node[i].distance) else: substract.append(float('inf')) if min(substract) != -1 or node[i].exit_diret_num != substract.count( -1): node[i].exit_diret = [] shortest_distance_node_index = [ i for i, value in enumerate(substract) if value == min(substract) ] for k in shortest_distance_node_index: shortest_node_num.append(node_list[k]) if node[i].forward != None and node[ i].forward.index in shortest_node_num: node[i].exit_diret.append('forward') if node[i].backward != None and node[ i].backward.index in shortest_node_num: node[i].exit_diret.append('backward') if node[i].right != None and node[ i].right.index in shortest_node_num: node[i].exit_diret.append('right') if node[i].left != None and node[i].left.index in shortest_node_num: node[i].exit_diret.append('left') if node[i].up != None and node[i].up.index in shortest_node_num: node[i].exit_diret.append('up') if node[i].down != None and node[i].down.index in shortest_node_num: node[i].exit_diret.append('down') node[i].set_exit_diret_num() else: del_list.append(i) for i in del_list: queue.remove(i) for i in queue: fire_test(fire_start, send2, i, node, linked_node_list)
def _delete(media_id): queue.remove(media_id) return flask.redirect(flask.url_for('_main'))
import sys import queue import heapq sys.stdin = open('./Algorithm_study/0218/lab02','r') INF = 10001 N, K = map(int, input().split()) queue = [] arr = list(map(int, input().split())) sum = 0 for i in range(N) : heapq.heappush(queue, arr[i]) print(queue) if i >= K-1 : heapq.heapify(queue) # print(i,arr[i-K+1]) print(arr[i-(K//2)]) sum += arr[i-(K//2)] queue.remove(arr[i-K+1]) print(sum)