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

#include <leetcode.h>

静态 Public 成员函数

static int secondMinimum (int n, vector< vector< int > > &edges, int time, int change)
 

详细描述

在文件 leetcode.h721 行定义.

成员函数说明

◆ secondMinimum()

int leetcode::second_minimum_time_to_reach_destination::Solution::secondMinimum ( int  n,
vector< vector< int > > &  edges,
int  time,
int  change 
)
static

在文件 leetcode.cpp1629 行定义.

1629 {
1630 auto record = unordered_map<int, vector<int>>();
1631 for(int i = 1; i <= n; i++) {
1632 record.insert(pair(i, vector<int>()));
1633 }
1634 auto um = unordered_map<int, unordered_set<int>>();
1635 for(auto edge: edges) {
1636 if(!um.contains(edge[0])) {
1637 auto us = unordered_set<int>();
1638 us.insert(edge[1]);
1639 um.insert(pair(edge[0], us));
1640 } else {
1641 um[edge[0]].insert(edge[1]);
1642 }
1643 if(!um.contains(edge[1])) {
1644 auto us = unordered_set<int>();
1645 us.insert(edge[0]);
1646 um.insert(pair(edge[1], us));
1647 } else {
1648 um[edge[1]].insert(edge[0]);
1649 }
1650 }
1651 auto pq = priority_queue<status>();
1652 pq.push(status(1, 0));
1653 bool flag = false;//已经到达一次终点
1654 int prev = 0;
1655 while(!pq.empty()) {
1656 status current = pq.top();
1657 pq.pop();
1658 if(current.position == n) {
1659 if(!flag) {
1660 flag = true;
1661 prev = current.time;
1662 } else {
1663 if(current.time != prev) {
1664 return current.time;
1665 }
1666 }
1667 }
1668
1669 if(record[current.position].empty() || record[current.position].size() == 1 && current.time != *record[current.position].rbegin()) {
1670 //可以继续
1671 record[current.position].push_back(current.time);
1672 } else {
1673 continue;
1674 }
1675
1676 for(int next: um[current.position]) {
1677 int next_time;
1678 if(current.time / change % 2 == 1) {
1679 //当前为红灯
1680 next_time = (current.time / change + 1) * change + time;
1681 } else {
1682 next_time = current.time + time;
1683 }
1684 auto next_status = status(next, next_time);
1685 if(record[next_status.position].empty() || record[next_status.position].size() == 1 && next_status.time != *record[next_status.position].rbegin()) {
1686 //可以继续
1687 pq.push(next_status);
1688 }
1689 }
1690 }
1691 return 0;
1692 }

引用了 leetcode::second_minimum_time_to_reach_destination::status::position , 以及 leetcode::second_minimum_time_to_reach_destination::status::time.

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


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