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. HDU4003Find Metal Mineral[树形DP 分组背包]

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  2. AC日记——找最大数序列 openjudge 1.9 10

    10:找最大数序列 总时间限制:  1000ms 内存限制:  65536kB 描述 输入n行,每行不超过100个无符号整数,无符号数不超过4位.请输出最大整数以及最大整数所在的行号(行号从1开始). ...

  3. ACCP7.0优化Myschool内侧题

    1) 在SQL Server 中,为数据库表建立索引能够(C ). 索引:是SQL SERVER编排数据的内部方法,是检索表中数据的直接通道 建立索引的作用:大大提高了数据库的检索速度,改善数据库性能 ...

  4. SQL连接查询

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union 在关系数据库 ...

  5. BigDecimal.ROUND_HALF_XXX的各种用法

    在银行.帐户.计费等领域,BigDecimal提供了精确的数值计算.其中8种舍入方式值得掌握. 1.ROUND_UP 舍入远离零的舍入模式. 在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数 ...

  6. u3d_shader_surface_shader_4

    Rim Lighting  轮廓自发光 一:疑问:1.总感觉在编辑器Scene状态下,脚本计算的ViewDir是我漫游的Cam,而不是项目中的MainCam啊! 然后就会造成Scene状态下轮廓自发光 ...

  7. Ubuntu下初学ROS时所遇小问题

    [1]运行命令$ rospack depends1 beginner_tutorials 时,提示 : [rospack] Error: no such package beginner_tutori ...

  8. [No00000E]PPT快捷键大全 PowerPoint2013/2010/2007/2003常用快捷

    熟练掌握PowerPoint快捷键可以让我们更快速的制作PPT模板,大大的节约时间成本.想提高工作效率吗?请熟悉PowerPoint快捷键吧!想成为高手吗?请先了解PPT快捷键吧!想制作出一个优秀的P ...

  9. [No000003]现代版三十六计,计计教你如何做人

    <现代版三十六计,计计教你如何做人> …………………………………………………………………………………… 第1计施恩计 在人际交往中,见到给人帮忙的机会,要立马扑上去,像一只饥饿的松鼠扑向地 ...

  10. Android系统自带APP分析——短信app

    Android操作系统本身就是一个巨大的开源软件仓库,熟悉它既可以了解到Android系统的设计框架,也可以获得高效的应用程序编写方式.本文所分析的源码来自于Google官方的AOSP源码4.0.1_ ...