-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2203. Minimum Weighted Subgraph With the Required Paths.java
More file actions
150 lines (116 loc) · 4.45 KB
/
2203. Minimum Weighted Subgraph With the Required Paths.java
File metadata and controls
150 lines (116 loc) · 4.45 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Solution {
class Vertex
{
public int value;
public long weight;
public Vertex(int node, long cost)
{
value = node;
weight = cost;
}
}
public long minimumWeight(int n, int[][] edges, int src1, int src2, int dest) {
Map<Integer, List<Vertex>> graph = new HashMap();
for(int i = 0; i < n; i++)
{
graph.putIfAbsent(i, new ArrayList());
}
for(int i = 0; i < edges.length; i++)
{
int src = edges[i][0], destination = edges[i][1], cost = edges[i][2];
graph.get(src).add(new Vertex(destination, cost));
}
Map<Integer, Map<Integer, Long>> distanceMap = new HashMap();
distanceMap.put(src1, djikstra(src1, graph, n));
distanceMap.put(src2, djikstra(src2, graph, n));
long first = distanceMap.get(src1).get(src2);
long second = distanceMap.get(src2).get(src1);
long third = distanceMap.get(src1).get(dest);
long fourth = distanceMap.get(src2).get(dest);
long fifth, sixth = Long.MAX_VALUE;
for(Map.Entry<Integer, Long> firstNode : distanceMap.get(src1).entrySet())
{
if(distanceMap.get(src2).containsKey(firstNode.getKey()))
{
Long secondNode = distanceMap.get(src2).get(firstNode.getKey());
if(firstNode.getValue() != Long.MAX_VALUE && secondNode != Long.MAX_VALUE)
{
fifth = firstNode.getValue() + secondNode;
//System.out.println(firstNode.getKey() + " " + firstNode.getValue());
//System.out.println(secondNode.getKey() + " " + secondNode.getValue());
long temp = djikstra(firstNode.getKey(), graph, n).get(dest);
if(temp != Long.MAX_VALUE)
{
return fifth + temp;
}
}
}
}
//System.out.println(first);
//System.out.println(second);
//System.out.println(third);
//System.out.println(fourth);
//return -1;
if(third != Long.MAX_VALUE)
{
if(fourth != Long.MAX_VALUE)
{
if(first != Long.MAX_VALUE)
{
if(second != Long.MAX_VALUE)
{
return Math.min(first + fourth, second + third);
}
return Math.min(first + fourth, third + fourth);
}
if(second != Long.MAX_VALUE)
{
return Math.min(second + third, third + fourth);
}
return third + fourth;
}
if(second != Long.MAX_VALUE)
{
return second + third;
}
return -1;
}
if(fourth != Long.MAX_VALUE)
{
if(first != Long.MAX_VALUE)
{
return first + fourth;
}
return -1;
}
return -1;
}
Map<Integer, Long> djikstra(int src, Map<Integer, List<Vertex>> graph, int n)
{
Map<Integer, Long> distanceMap = new HashMap();
for(int i = 0; i < n; i++)
{
distanceMap.put(i, Long.MAX_VALUE);
}
Queue<Vertex> queue = new PriorityQueue<Vertex>((a, b) -> Long.compare(a.weight, b.weight));
queue.offer(new Vertex(src, 0));
distanceMap.put(src, 0l);
while(!queue.isEmpty())
{
Vertex top = queue.poll();
//System.out.println(top.value);
for(Vertex nei : graph.get(top.value))
{
Long a = distanceMap.get(nei.value);
Long b = distanceMap.get(top.value);
Long c = a + 1;
if(distanceMap.get(nei.value) > distanceMap.get(top.value) + nei.weight)
{
distanceMap.put(nei.value, distanceMap.get(top.value) + nei.weight);
queue.offer(new Vertex(nei.value, distanceMap.get(nei.value)));
}
}
}
return distanceMap;
}
}