Compare commits
2 Commits
7bf260628a
...
dda8fdf3fa
Author | SHA1 | Date | |
---|---|---|---|
dda8fdf3fa | |||
59bcdd0401 |
50
18-03-24/c.py
Normal file
50
18-03-24/c.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
## Récupération des entrées
|
||||||
|
n, m = [int(i) for i in input().split()]
|
||||||
|
|
||||||
|
# adj = [list() for i in range(n+1)]
|
||||||
|
anti_adj = [list() for i in range(n+1)]
|
||||||
|
for i in range(m):
|
||||||
|
u, v = [int(i) for i in input().split()]
|
||||||
|
anti_adj[v].append(u)
|
||||||
|
# adj[u].append(v)
|
||||||
|
|
||||||
|
k = int(input())
|
||||||
|
p = [int(i) for i in input().split()]
|
||||||
|
|
||||||
|
## Initialisation des structures
|
||||||
|
dist = [-1 for i in range(n+1)]
|
||||||
|
succ = {i+1:set() for i in range(n)}
|
||||||
|
size = {i+1:0 for i in range(n)}
|
||||||
|
|
||||||
|
non_vus = set((i+1 for i in range(n) if i+1 != p[-1]))
|
||||||
|
queue = [p[-1]]
|
||||||
|
dist[p[-1]] = 0
|
||||||
|
|
||||||
|
## Calcul des prochains avec leur coût, BFS par la fin
|
||||||
|
while len(queue) > 0:
|
||||||
|
nextQ = []
|
||||||
|
while len(queue) > 0:
|
||||||
|
s = queue.pop()
|
||||||
|
for pred in anti_adj[s]:
|
||||||
|
if pred in non_vus:
|
||||||
|
non_vus.remove(pred)
|
||||||
|
nextQ.append(pred)
|
||||||
|
dist[pred] = dist[s]+1
|
||||||
|
if dist[pred] == dist[s]+1:
|
||||||
|
succ[pred].add(s)
|
||||||
|
size[pred] += 1
|
||||||
|
queue = nextQ
|
||||||
|
|
||||||
|
## Calcul final
|
||||||
|
mini, maxi = 0, 0
|
||||||
|
position = p[0]
|
||||||
|
for i in range(1, k):
|
||||||
|
next_pos = p[i]
|
||||||
|
if next_pos not in succ[position]:
|
||||||
|
mini += 1
|
||||||
|
maxi += 1
|
||||||
|
elif len(succ[position]) > 1:
|
||||||
|
maxi += 1
|
||||||
|
position = next_pos
|
||||||
|
|
||||||
|
print(mini, maxi)
|
54
18-03-24/d.py
Normal file
54
18-03-24/d.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
def main():
|
||||||
|
n, m, k = map(int, input().split())
|
||||||
|
a = [int(i) for i in input().split()]
|
||||||
|
# roads = [list(map(int, input().split())) for _ in range(m)]
|
||||||
|
|
||||||
|
voisins = {i:set() for i in range(1, n+1)}
|
||||||
|
|
||||||
|
A = set(a) # pour les test d'appartenance
|
||||||
|
special = False
|
||||||
|
for _ in range(m):
|
||||||
|
x, y = map(int, input().split())
|
||||||
|
voisins[x].add(y)
|
||||||
|
voisins[y].add(x)
|
||||||
|
if not special and (x in A and y in A):
|
||||||
|
special = True
|
||||||
|
|
||||||
|
|
||||||
|
def distance(source, dist):
|
||||||
|
"""calculer la distance d'un sommet à tous les autres"""
|
||||||
|
non_vus = set((i+1 for i in range(n) if i+1 != source))
|
||||||
|
queue = [source]
|
||||||
|
dist[source] = 0
|
||||||
|
while queue != []:
|
||||||
|
nextQ = []
|
||||||
|
while queue != []:
|
||||||
|
s = queue.pop()
|
||||||
|
for v in voisins[s]:
|
||||||
|
if v in non_vus:
|
||||||
|
non_vus.remove(v)
|
||||||
|
nextQ.append(v)
|
||||||
|
dist[v] = dist[s]+1
|
||||||
|
queue = nextQ
|
||||||
|
|
||||||
|
dist_n = [0 for _ in range(n+1)]
|
||||||
|
dist_1 = [0 for _ in range(n+1)]
|
||||||
|
distance(n, dist_n)
|
||||||
|
distance(1, dist_1)
|
||||||
|
|
||||||
|
if special:
|
||||||
|
return dist_n[1] # On ne rajoute rien
|
||||||
|
|
||||||
|
# On regarde lequel ajouter
|
||||||
|
a.sort(key=lambda x: dist_1[x]-dist_n[x])
|
||||||
|
sp_count = len(a)
|
||||||
|
b = [dist_n[a[-1]] for i in range(sp_count)]
|
||||||
|
for sp in range(1, sp_count):
|
||||||
|
b[sp_count-1-sp] = max(b[sp_count-sp], dist_n[a[sp_count-sp]])
|
||||||
|
|
||||||
|
return min(
|
||||||
|
dist_1[n],
|
||||||
|
max((dist_1[a[i]]+b[i] for i in range(0, len(a)-1)))+1
|
||||||
|
)
|
||||||
|
|
||||||
|
print(main())
|
Loading…
x
Reference in New Issue
Block a user