LeetCode 2045. 到达目的地的第二短时间 困难 次短路

原题链接

class Solution {
public:
    struct Node
    {
        int id,type,d;
        bool operator >(const Node &W)const//小根堆重载大于号 因为标准库没有<
        {
            return d>W.d;
        }
    };
    void bfs(vector<vector<int>>& e,vector<vector<int>>& d,vector<vector<bool>>& st)
    {   
        priority_queue<Node,vector<Node>,greater<Node>>heap;
        d[1][0]=0; heap.push({1,0,0});
        int x=0;
        while(heap.size()){
            Node t=heap.top();
            heap.pop();

            int ver=t.id,type=t.type,distance=t.d;
            if(st[ver][type])continue;//dijkstra 第一次弹出
            st[ver][type]=true;
//cout<<ver<<endl;
            for(int i=0;i<e[ver].size();i++)
            {
                
                int j=e[ver][i];
                if(d[j][0]>distance+1){
                    d[j][1]=d[j][0];
                    heap.push({j,1,d[j][1]});
                    d[j][0]=distance+1;
                    heap.push({j,0,d[j][0]});
                }
                else if(d[j][1]>distance+1)
                {
                    if(distance+1==d[j][0])continue;//严格小于 两小时耗在这里
                    d[j][1]=distance+1;
                    heap.push({j,1,d[j][1]});
                }
            }
        }
        return ;

    }
    int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
          vector<int>w1(2,1e4+1);
         vector<vector<int>>d(n+1,w1);
         
         vector<bool>w2(2,false);
         vector<vector<bool>>st(n+1,w2);

         vector<int>w;
        vector<vector<int>>e(n+1,w) ;
        for(int i=0;i<edges.size();i++){
            e[edges[i][0]].push_back(edges[i][1]);
             e[edges[i][1]].push_back(edges[i][0]);
        }
     bfs(e,d,st);
     if(d[n][1]==1e4+1)d[n][1]=d[n][0]+2;

         int res=0; 
for (int i = 0; i < d[n][1]; i++){     
            if ((res / change) % 2){
                res +=  + change- res % change  ;
            }  
              res += time;
        }
        return res;
    }
};
posted @ 2021-10-30 10:31  liv_vil  阅读(47)  评论(0)    收藏  举报