返回介绍

solution / 2200-2299 / 2203.Minimum Weighted Subgraph With the Required Paths / README_EN

发布于 2024-06-17 01:03:08 字数 6090 浏览 0 评论 0 收藏 0

2203. Minimum Weighted Subgraph With the Required Paths

中文文档

Description

You are given an integer n denoting the number of nodes of a weighted directed graph. The nodes are numbered from 0 to n - 1.

You are also given a 2D integer array edges where edges[i] = [fromi, toi, weighti] denotes that there exists a directed edge from fromi to toi with weight weighti.

Lastly, you are given three distinct integers src1, src2, and dest denoting three distinct nodes of the graph.

Return _the minimum weight of a subgraph of the graph such that it is possible to reach_ dest _from both_ src1 _and_ src2 _via a set of edges of this subgraph_. In case such a subgraph does not exist, return -1.

A subgraph is a graph whose vertices and edges are subsets of the original graph. The weight of a subgraph is the sum of weights of its constituent edges.

 

Example 1:

Input: n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5
Output: 9
Explanation:
The above figure represents the input graph.
The blue edges represent one of the subgraphs that yield the optimal answer.
Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.

Example 2:

Input: n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
Output: -1
Explanation:
The above figure represents the input graph.
It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.

 

Constraints:

  • 3 <= n <= 105
  • 0 <= edges.length <= 105
  • edges[i].length == 3
  • 0 <= fromi, toi, src1, src2, dest <= n - 1
  • fromi != toi
  • src1, src2, and dest are pairwise distinct.
  • 1 <= weight[i] <= 105

Solutions

Solution 1

class Solution:
  def minimumWeight(
    self, n: int, edges: List[List[int]], src1: int, src2: int, dest: int
  ) -> int:
    def dijkstra(g, u):
      dist = [inf] * n
      dist[u] = 0
      q = [(0, u)]
      while q:
        d, u = heappop(q)
        if d > dist[u]:
          continue
        for v, w in g[u]:
          if dist[v] > dist[u] + w:
            dist[v] = dist[u] + w
            heappush(q, (dist[v], v))
      return dist

    g = defaultdict(list)
    rg = defaultdict(list)
    for f, t, w in edges:
      g[f].append((t, w))
      rg[t].append((f, w))
    d1 = dijkstra(g, src1)
    d2 = dijkstra(g, src2)
    d3 = dijkstra(rg, dest)
    ans = min(sum(v) for v in zip(d1, d2, d3))
    return -1 if ans >= inf else ans
class Solution {
  private static final Long INF = Long.MAX_VALUE;

  public long minimumWeight(int n, int[][] edges, int src1, int src2, int dest) {
    List<Pair<Integer, Long>>[] g = new List[n];
    List<Pair<Integer, Long>>[] rg = new List[n];
    for (int i = 0; i < n; ++i) {
      g[i] = new ArrayList<>();
      rg[i] = new ArrayList<>();
    }
    for (int[] e : edges) {
      int f = e[0], t = e[1];
      long w = e[2];
      g[f].add(new Pair<>(t, w));
      rg[t].add(new Pair<>(f, w));
    }
    long[] d1 = dijkstra(g, src1);
    long[] d2 = dijkstra(g, src2);
    long[] d3 = dijkstra(rg, dest);
    long ans = -1;
    for (int i = 0; i < n; ++i) {
      if (d1[i] == INF || d2[i] == INF || d3[i] == INF) {
        continue;
      }
      long t = d1[i] + d2[i] + d3[i];
      if (ans == -1 || ans > t) {
        ans = t;
      }
    }
    return ans;
  }

  private long[] dijkstra(List<Pair<Integer, Long>>[] g, int u) {
    int n = g.length;
    long[] dist = new long[n];
    Arrays.fill(dist, INF);
    dist[u] = 0;
    PriorityQueue<Pair<Long, Integer>> q
      = new PriorityQueue<>(Comparator.comparingLong(Pair::getKey));
    q.offer(new Pair<>(0L, u));
    while (!q.isEmpty()) {
      Pair<Long, Integer> p = q.poll();
      long d = p.getKey();
      u = p.getValue();
      if (d > dist[u]) {
        continue;
      }
      for (Pair<Integer, Long> e : g[u]) {
        int v = e.getKey();
        long w = e.getValue();
        if (dist[v] > dist[u] + w) {
          dist[v] = dist[u] + w;
          q.offer(new Pair<>(dist[v], v));
        }
      }
    }
    return dist;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文