problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::minimum_weighted_subgraph_with_the_required_paths::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static void calc_dist (int s, vector< pair< int, int > > *graph, vector< long long int > &dist)
 
static long long minimumWeight (int n, vector< vector< int > > &edges, int src1, int src2, int dest)
 

详细描述

在文件 leetcode.h1714 行定义.

成员函数说明

◆ calc_dist()

void leetcode::minimum_weighted_subgraph_with_the_required_paths::Solution::calc_dist ( int  s,
vector< pair< int, int > > *  graph,
vector< long long int > &  dist 
)
static

在文件 leetcode.cpp4444 行定义.

4444 {
4445 priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
4446 dist[s] = 0;
4447 pq.emplace(0, s);
4448 while(!pq.empty()) {
4449 auto [weight, node] = pq.top();
4450 pq.pop();
4451 if(weight > dist[node]) {
4452 continue;
4453 }
4454 for(auto [next_node, next_weight]: graph[node]) {
4455 if(weight + next_weight < dist[next_node]) {
4456 dist[next_node] = weight + next_weight;
4457 pq.emplace(weight + next_weight, next_node);
4458 }
4459 }
4460 }
4461 }

被这些函数引用 minimumWeight().

◆ minimumWeight()

long long leetcode::minimum_weighted_subgraph_with_the_required_paths::Solution::minimumWeight ( int  n,
vector< vector< int > > &  edges,
int  src1,
int  src2,
int  dest 
)
static

在文件 leetcode.cpp4405 行定义.

4405 {
4406 auto *graph = new vector<pair<int, int>>[n];
4407 auto *graph_rev = new vector<pair<int, int>>[n];
4408 for(int i = 0; i < n; i++) {
4409 graph[i] = vector<pair<int, int>>();
4410 graph_rev[i] = vector<pair<int, int>>();
4411 }
4412 for(auto &edge: edges) {
4413 auto from = edge[0];
4414 auto to = edge[1];
4415 auto weight = edge[2];
4416 graph[from].emplace_back(to, weight);
4417 graph_rev[to].emplace_back(from, weight);
4418 }
4419
4420 long long a = LLONG_MAX;
4421 vector dist_src1(n, LLONG_MAX);
4422 vector dist_src2(n, LLONG_MAX);
4423 vector dist_dest(n, LLONG_MAX);
4424
4425 calc_dist(src1, graph, dist_src1);
4426 calc_dist(src2, graph, dist_src2);
4427 calc_dist(dest, graph_rev, dist_dest);
4428
4429 long long ans = LLONG_MAX;
4430
4431 for(int i = 0; i < n; ++i) {
4432 if(dist_src1[i] != LLONG_MAX && dist_src2[i] != LLONG_MAX && dist_dest[i] != LLONG_MAX) {
4433 ans = min(ans, dist_src1[i] + dist_src2[i] + dist_dest[i]);
4434 }
4435 }
4436 if(ans == LLONG_MAX) {
4437 ans = -1;
4438 }
4439 delete[] graph;
4440 delete[] graph_rev;
4441 return ans;
4442 }
static void calc_dist(int s, vector< pair< int, int > > *graph, vector< long long int > &dist)
Definition: leetcode.cpp:4444

引用了 calc_dist().

被这些函数引用 leetcode::minimum_weighted_subgraph_with_the_required_paths::TEST().


该类的文档由以下文件生成: