-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.go
67 lines (66 loc) · 1.11 KB
/
Solution.go
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
func modifiedGraphEdges(n int, edges [][]int, source int, destination int, target int) [][]int {
const inf int = 2e9
dijkstra := func(edges [][]int) int {
g := make([][]int, n)
dist := make([]int, n)
vis := make([]bool, n)
for i := range g {
g[i] = make([]int, n)
for j := range g[i] {
g[i][j] = inf
}
dist[i] = inf
}
dist[source] = 0
for _, e := range edges {
a, b, w := e[0], e[1], e[2]
if w == -1 {
continue
}
g[a][b], g[b][a] = w, w
}
for i := 0; i < n; i++ {
k := -1
for j := 0; j < n; j++ {
if !vis[j] && (k == -1 || dist[j] < dist[k]) {
k = j
}
}
vis[k] = true
for j := 0; j < n; j++ {
dist[j] = min(dist[j], dist[k]+g[k][j])
}
}
return dist[destination]
}
d := dijkstra(edges)
if d < target {
return [][]int{}
}
ok := d == target
for _, e := range edges {
if e[2] > 0 {
continue
}
if ok {
e[2] = inf
continue
}
e[2] = 1
d := dijkstra(edges)
if d <= target {
ok = true
e[2] += target - d
}
}
if ok {
return edges
}
return [][]int{}
}
func min(a, b int) int {
if a < b {
return a
}
return b
}