P8646 [蓝桥杯 2017 省 AB] 包子凑数 题解
来一些不一样的做法。同余最短路。
以模
则
求出
注意到能凑出
则每个
特别地,若 INF
。
复杂度
#include <queue>
#include <cstdio>
#include <cstring>
#define P pair<int, int>
using namespace std;
struct E
{
int v, w, t;
} e[10050];
int n, c, z, a[150], d[150], h[150];
bool b[150];
priority_queue<P, vector<P>, greater<P>> q;
void A(int u, int v, int w)
{
e[++c] = {v, w, h[u]};
h[u] = c;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", a + i);
for (int i = 0; i < a[1]; ++i)
for (int j = 2; j <= n; ++j)
A(i, (i + a[j]) % a[1], a[j]);
memset(d, 0x3f, sizeof d);
q.emplace(d[0] = 0, 0);
while (!q.empty())
{
int u = q.top().second;
q.pop();
if (!b[u])
{
b[u] = 1;
for (int i = h[u], v; i; i = e[i].t)
if (d[v = e[i].v] > d[u] + e[i].w)
q.emplace(d[v] = d[u] + e[i].w, v);
}
}
for (int i = 0; i < a[1]; ++i)
{
if (d[i] == 0x3f3f3f3f)
return !puts("INF");
z += d[i] / a[1];
}
printf("%d", z);
return 0;
}
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具