• 牛客练习赛60 D.斩杀线计算大师 (同余最短路)


    题目:传送门

    题解:https://ac.nowcoder.com/discuss/394080?type=101&order=0&pos=1&page=1

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<utility>
    #include<cstring>
    #include<functional>
    #include<queue>
     
    using namespace std;
     
    typedef long long ll;
    typedef pair<int, int> pii;
     
    const int maxn = 1e5;
    const int INF = 0x7fffffff;
     
    ll dis[maxn + 5], k;
     
    pii edge[maxn + 5];
    int a, b, c;
    ll res[10];
     
    void dijkstra() {
        for (int i = 1; i <= c; ++i)dis[i] = INF;
        priority_queue<pii, vector<pii>, greater<pii>> q;
        q.push(pii(0, 0));
        while (!q.empty()) {
            pii u = q.top(); q.pop();
            if (u.first > dis[u.second])continue;
            if (dis[(u.second + a) % c] > dis[u.second] + a){
                dis[(u.second + a) % c] = dis[u.second] + a;
                edge[(u.second + a) % c] = pii(u.second, 1);
                q.push(pii(dis[(u.second + a) % c], (u.second + a) % c));
            }
            if (dis[(u.second + b) % c] > dis[u.second] + b) {
                dis[(u.second + b) % c] = dis[u.second] + b;
                edge[(u.second + b) % c] = pii(u.second, 2);
                q.push(pii(dis[(u.second + b) % c], (u.second + b) % c));
            }
        }
    }
    void dfs(int now) {
        if (now == 0)return;
        res[edge[now].second] += 1;
        dfs(edge[now].first);
    }
     
    int main()
    {
        scanf("%d %d %d %lld", &a, &b, &c, &k);
        dijkstra();
        dfs(k % c);
        ll sum = a * res[1] + b * res[2];
        res[3] = (k - sum) / c;
        printf("%lld %lld %lld
    ", res[1], res[2], res[3]);
        return 0;
    }
    一步一步,永不停息
  • 相关阅读:
    Django系列6:Model简介,ORM,字段类型,约束
    Django系列4:数据常规操作级联数据
    Django系列5:脑图总结
    Sorted Adjacent Differences
    C
    E. Sleeping Schedule
    D. Ehab the Xorcist
    C. Game with Chips
    D. Walk on Matrixv
    Circle of Monsters
  • 原文地址:https://www.cnblogs.com/Willems/p/12622445.html
Copyright © 2020-2023  润新知