题解 | #旅行Ⅰ#

旅行Ⅰ

http://www.nowcoder.com/practice/ecc6e261da68412caad810669b1e891f

// 什么压行狂魔(
class Solution {
public:
    using pii = pair<int,int>;
    vector<int> ed[100010];
    int d[100010],ans=0;
    int Travel(int N, int V, vector<int>& A, vector<Point>& list) {
        for(auto [x,y]:list)  ed[x].push_back(y),d[y]++;
        auto cmp = [&](pii a,pii b) -> bool{
            if(a.first == b.first) return a.second < b.second;
            else return a.first < b.first;
        };
        priority_queue<pii,vector<pii>,decltype(cmp)> pq(cmp);
        for(int i=1;i<=N;i++) if(!d[i]) pq.push({i,A[i]});
        while(!pq.empty())
        {
            auto [x,y] = pq.top(); pq.pop();
            if(V<y) break; V-=y;
            ans++;
            for(auto v:ed[x]) if(--d[v] == 0)  pq.push({v,A[v]});
        }
        return ans;
    }
};
全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务