Skip to content

Instantly share code, notes, and snippets.

@henrycunh
Last active October 23, 2020 22:01
Show Gist options
  • Save henrycunh/61dc22fa2b30d24fa4342b318bbc9d4a to your computer and use it in GitHub Desktop.
Save henrycunh/61dc22fa2b30d24fa4342b318bbc9d4a to your computer and use it in GitHub Desktop.
te amo fessô diego
import sys
class Graph():
def __init__(self, vertices):
self.vertices = vertices
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
def list_distances(self, dist):
for node in range(self.vertices):
print (node, " > ", dist[node])
def min_dist(self, dist, shortest_path_set):
min = sys.maxsize
for v in range(self.vertices):
if dist[v] < min and shortest_path_set[v] == False:
min = dist[v]
min_index = v
return min_index
def shortest_path(self, target, is_impostor = False):
dist = ([sys.maxsize] * self.vertices)
dist[0] = 0
shortest_path_set = [False] * self.vertices
for u in range(self.vertices):
u = self.min_dist(dist, shortest_path_set)
shortest_path_set[u] = True
for v in range(self.vertices):
weight = self.graph[u][v]
if self.graph[u][v] == -1 and is_impostor:
weight = 1
is_greater = weight > 0 and shortest_path_set[v] == False and dist[v] > dist[u] + weight
if is_greater:
dist[v] = dist[u] + weight
return dist[target]
def __str__(self):
graph_string = ''
for line in self.graph:
graph_string += ' '.join([f"{val:3}" for val in line]) + '\n'
return graph_string
[vertices, edges, vents, queries] = [int(num) for num in input().split(' ')]
link_input = [float(num) for num in input().split(' ')]
vent_input = [int(num) for num in input().split(' ')]
query_list = []
ship = Graph(vertices + 1)
for i in range(edges):
from_vertice, to_vertice, weight = link_input[3 * i : 3 * (i + 1)]
ship.graph[int(from_vertice)][int(to_vertice)] = weight
for i in range(vents):
from_vertice, to_vertice = vent_input[2 * i : 2 * (i + 1)]
ship.graph[int(from_vertice)][int(to_vertice)] = -1
for query in range(queries):
source = int(input())
crew_dist = ship.shortest_path(source)
impostor_dist = ship.shortest_path(source, True)
print('victory' if int(crew_dist) <= int(impostor_dist) else 'defeat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment