http://www.lydsy.com/JudgeOnline/problem.php?id=2118

最短路就是为了找到最小的$x$满足$x=k×a_{min}+d,0≤d<a_{min}$

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 500003;
int in() {
int k = 0, fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
}
ll inll() {
ll k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 3) + (k << 1) + c - '0';
return k * fh;
} struct node {
int nxt, to, w;
} E[N * 12];
struct Point {
int id; ll dist;
Point(int _id = 0, ll _dist = 0) : id(_id), dist(_dist) {}
bool operator < (const Point &A) const {
return dist > A.dist;
}
};
ll dist[N], Bmin, Bmax;
bool inq[N];
int p, n, a[13], cnt = 0, point[N]; void ins(int u, int v, int w) {E[++cnt] = (node) {point[u], v, w}; point[u] = cnt;} void dijkstra(int s) {
for(int i = 1; i < p; ++i) dist[i] = 2500000000000ll;
dist[s] = 0;
priority_queue <Point> q;
q.push(Point(s, 0));
Point u;
while (!q.empty()) {
u = q.top(); q.pop();
if (inq[u.id]) continue;
inq[u.id] = true;
for(int i = point[u.id]; i; i = E[i].nxt)
if (u.dist + E[i].w < dist[E[i].to]) {
dist[E[i].to] = u.dist + E[i].w;
q.push(Point(E[i].to, dist[E[i].to]));
}
}
} ll solve(ll x) {
ll ret = 0;
for(int i = 0; i < p; ++i)
if (x >= dist[i]) ret += (x - dist[i]) / p + 1;
return ret;
} int main() {
n = in(); Bmin = inll(); Bmax = inll();
for(int i = 1; i <= n; ++i) a[i] = in();
sort(a + 1, a + n + 1);
p = a[1];
for(int i = 0; i < p; ++i)
for(int j = 2; j <= n; ++j)
ins(i, (i + a[j]) % p, a[j]);
dijkstra(0);
printf("%lld\n", solve(Bmax) - solve(Bmin - 1));
return 0;
}

堆优化dijkstra模板↑

【BZOJ 2118】墨墨的等式的更多相关文章

  1. 【BZOJ 2118】 墨墨的等式(Dijkstra)

    BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...

  2. bzoj 2118 墨墨的等式 - 图论最短路建模

    墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...

  3. 数论+spfa算法 bzoj 2118 墨墨的等式

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1283  Solved: 496 Description 墨墨突然对等式很感兴 ...

  4. 【BZOJ 2118】 2118: 墨墨的等式 (最短路)

    2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...

  5. bzoj 2118: 墨墨的等式

    Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...

  6. bzoj 2118: 墨墨的等式 spfa

    题目: 墨墨突然对等式很感兴趣,他正在研究\(a_1x_1+a_2y_2+ ... +a_nx_n=B\)存在非负整数解的条件,他要求你编写一个程序,给定\(N,\{a_n\}\)以及\(B\)的取值 ...

  7. [图论训练]BZOJ 2118: 墨墨的等式 【最短路】

    Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...

  8. BZOJ2118墨墨的等式[数论 最短路建模]

    2118: 墨墨的等式 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1317  Solved: 504[Submit][Status][Discus ...

  9. 【BZOJ2118】墨墨的等式(最短路)

    [BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...

随机推荐

  1. GTAC 2015 的视频和文档已经放出

    视频见https://developers.google.com/google-test-automation-conference/2015/stream,文档见https://developers ...

  2. 网络之OSI七层模型

    1.  物理层:设备之间的比特流的传输,物理接口,电气特性等 2.  数据链路层:成帧,用MAC地址访问媒介,错误检测与修正 3.  网络层:提供逻辑地址(IP),选路 4.  传输层:可靠与不可靠的 ...

  3. 使用javascript实现的雪花飞舞的效果

    原作者是在body中不停的插入多个小div雪花来向下慢慢飘,一直飘到body的底部后,将雪花移除,于是,将原来的代码稍加修改,让他只是从屏幕的顶部飘落到屏幕底部(不是body的底部)后,就将雪花移除, ...

  4. Android保存ArrayList至SharedPreferences

    保存ArrayList至SharedPreferences 其中ArrayList中每个元素为String List<String> environmentList = new Array ...

  5. webpack常用加载器和插件

    css文件加载器: style-loader,css-loader,sass-loader,less-loader //style和css加载器必须放在一起使用,且style必须放前面(style!c ...

  6. http协议(三)几种数据传输方式

    说说http协议的一些特点: 1)无状态 http协议是一种自身不对请求和响应之间的通信状态进行保存的协议,即无状态协议. 这种设置的好处是:更快的处理更多的请求事务,确保协议的可伸缩性 不过随着we ...

  7. React 学习笔记(一)

    React + es6 一.createClass 与 component 的区别 The API (via 'extends React.Component') is similar to Reac ...

  8. linux下截取给定路径中的目录部分

    在日常运维中,有时会要求截取一个路径中的目录部分.截取目录的方法,有以下两种:1)dirname命令(最常用的方法):用于取给定路径的目录部分.很少直接在shell命令行中使用,一般把它用在shell ...

  9. Mysql备份系列(2)--mysqldump备份(全量+增量)方案操作记录

    在日常运维工作中,对mysql数据库的备份是万分重要的,以防在数据库表丢失或损坏情况出现,可以及时恢复数据. 线上数据库备份场景:每周日执行一次全量备份,然后每天下午1点执行MySQLdump增量备份 ...

  10. 关于调试日志Log

    __VA_ARGS__  是一个可变参数的宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).宏前面加上##的作用在于,当可变参数的个数为0时,这里的## ...