Skip to content

Commit 157bccd

Browse files
committed
2021.01.04
1 parent 4681257 commit 157bccd

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

python/plat5/1948.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
input = sys.stdin.readline
3+
n = int(input())
4+
m = int(input())
5+
adj_f = [[] for _ in range(n+1)]
6+
adj_b = [[] for _ in range(n+1)]
7+
visited = [False]*(n+1)
8+
indegree = [0]*(n+1)
9+
for _ in range(m):
10+
a,b,c = map(int,input().split())
11+
adj_f[a].append([b,c])
12+
adj_b[b].append([a,c])
13+
indegree[b]+=1
14+
dist = [0]*(n+1)
15+
16+
start,end = map(int,input().split())
17+
18+
st = [start]
19+
while st:
20+
n = st.pop()
21+
for nx, c in adj_f[n]:
22+
if dist[nx]<dist[n]+c:
23+
dist[nx]=dist[n]+c
24+
indegree[nx]-=1
25+
if indegree[nx]==0:
26+
st.append(nx)
27+
ans = 0
28+
print(dist[end])
29+
st = [end]
30+
while st:
31+
n = st.pop()
32+
dis = dist[n]
33+
for i,c in adj_b[n]:
34+
if dis == dist[i]+c:
35+
ans+=1
36+
if not visited[i]:
37+
visited[i]=True
38+
st.append(i)
39+
print(ans)

python/plat5/2842.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
dx = [1, -1, 0, 0, 1, 1, -1, -1]
6+
dy = [0, 0, 1, -1, 1, -1, 1, -1]
7+
8+
def bfs(l,r):
9+
st = deque()
10+
visited = [[False]*(p) for _ in range(p)]
11+
visited[sty][stx]=True
12+
st.append([sty,stx])
13+
while st:
14+
y,x = st.popleft()
15+
for i in range(8):
16+
ny,nx = y+dy[i],x+dx[i]
17+
if ny<0 or ny>=p or nx<0 or nx>=p:continue
18+
if visited[ny][nx]:continue
19+
if l<=arr2[ny][nx]<=r:
20+
visited[ny][nx]=True
21+
st.append([ny,nx])
22+
for y,x in start_list:
23+
if not visited[y][x]:return False
24+
return True
25+
26+
p = int(input())
27+
arr = []
28+
start_list = []
29+
for i in range(p):
30+
temp = list(input().strip())
31+
for j,t in enumerate(temp):
32+
if t=='K' or t=='P':
33+
start_list.append([i,j])
34+
arr.append(temp)
35+
sty,stx=start_list[0]
36+
37+
arr2 = []
38+
39+
temp_set = set()
40+
for _ in range(p):
41+
temp = list(map(int,input().split()))
42+
for t in temp:
43+
temp_set.add(t)
44+
arr2.append(temp)
45+
46+
temp_list = sorted(temp_set)
47+
l_min = temp_list[0]
48+
r_max = temp_list[-1]
49+
50+
l_max, r_min = sys.maxsize, 0
51+
52+
for y,x in start_list:
53+
l_max = min(l_max,arr2[y][x])
54+
r_min = max(r_min,arr2[y][x])
55+
56+
left_list = []
57+
right_list = []
58+
59+
for k in temp_list:
60+
if l_min<=k<=l_max:
61+
left_list.append(k)
62+
if r_min<=k<=r_max:
63+
right_list.append(k)
64+
65+
left=right=0
66+
ans = sys.maxsize
67+
l_flag,r_flag = 0,0
68+
while left<len(left_list) and right<len(right_list):
69+
if bfs(left_list[left],right_list[right]):
70+
ans = min(ans,right_list[right]-left_list[left])
71+
left+=1
72+
l_flag=1
73+
else:
74+
if l_flag and r_flag:
75+
left+=1;right+=1
76+
l_flag=0
77+
r_flag=0
78+
else:
79+
r_flag=1
80+
right+=1
81+
print(ans)

python/plat5/5719.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
import heapq
3+
input = sys.stdin.readline
4+
5+
while True:
6+
N, M = map(int,input().split())
7+
if N==0 and M==0:break
8+
start, end = map(int,input().split())
9+
10+
adj_f = [[] for _ in range(N+1)]
11+
adj_b = [[] for _ in range(N+1)]
12+
for _ in range(M):
13+
a,b,c = map(int,input().split())
14+
adj_f[a].append([b,c])
15+
adj_b[b].append([a,c])
16+
st = [[0,start]]
17+
dist = [sys.maxsize]*(N+1)
18+
dist[start]=0
19+
ans = 0
20+
while st:
21+
dis, n = heapq.heappop(st)
22+
for i,c in adj_f[n]:
23+
if dist[i]>dist[n]+c:
24+
dist[i]=dist[n]+c
25+
heapq.heappush(st,[dist[i],i])
26+
st = [end]
27+
visited = [False]*(N+1)
28+
while st:
29+
n = st.pop()
30+
for i,c in adj_b[n]:
31+
if dist[n]==dist[i]+c:
32+
for idx in range(len(adj_f[i])):
33+
if adj_f[i][idx][0]==n:
34+
adj_f[i].pop(idx)
35+
break
36+
if not visited[i]:
37+
visited[i]=True
38+
st.append(i)
39+
dist = [sys.maxsize]*(N+1)
40+
dist[start] = 0
41+
st = [[0,start]]
42+
while st:
43+
dis, n = heapq.heappop(st)
44+
for i,c in adj_f[n]:
45+
if dist[i]>dist[n]+c:
46+
dist[i]=dist[n]+c
47+
heapq.heappush(st,[dist[i],i])
48+
print(dist[end] if dist[end]!=sys.maxsize else -1)

0 commit comments

Comments
 (0)