-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha64_q4_path_sum.py
97 lines (82 loc) · 1.39 KB
/
a64_q4_path_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#09:00
import sys,heapq,math
input = sys.stdin.readline
print = lambda s:sys.stdout.write(str(s)+"\n")
sys.setrecursionlimit(9999999)
def greedy( G,V,v,n ):
s = 0
newV = list(V)
while True:
maxi = 0
idx = -1
for nv,c in G[v]:
if c > maxi and newV[nv]==0:
maxi = c
idx = nv
if idx!=-1:
newV[idx]=1
s += maxi
else:
break
v = idx
return s
def DFS( G,V,v,sumW,ask,n ):
if sumW>ask:
return False
g = greedy(G,V,v,n)
if sumW+g < ask:
return False
V[v] = 1
if sumW==ask:
return True
for nv,c in G[v]:
if V[nv]==0:
if DFS( G,V,nv,sumW+c,ask,n ):
return True
V[v] = 0
return False
n,m = [ int(i) for i in input().split() ]
ASK = [ int(i) for i in input().split() ]
G = [ [] for i in range(n) ]
for i in range(m):
a,b,c = [int(j) for j in input().split()]
G[a].append((b,c))
G[b].append((a,c))
for ask in ASK:
f = False
for i in range(n):
V = [0]*n
if DFS(G,V,i,0,ask,n):
f = True
break
if f:
print("YES")
else:
print("NO")
'''
4 4
10 20 30 40 50 60 70 80
0 1 10
0 2 15
1 2 5
0 3 35
4 4
50
0 1 10
0 2 15
1 2 5
0 3 35
4 5
65
0 1 10
0 2 15
1 2 5
0 3 35
2 3 5
4 4
50
0 1 10
0 2 15
1 2 5
0 3 35
'''