牛客练习赛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\n", res[1], res[2], res[3]); return 0; }
一步一步,永不停息