【BZOJ 2118】墨墨的等式
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】墨墨的等式的更多相关文章
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
- bzoj 2118 墨墨的等式 - 图论最短路建模
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. Input ...
- 数论+spfa算法 bzoj 2118 墨墨的等式
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1283 Solved: 496 Description 墨墨突然对等式很感兴 ...
- 【BZOJ 2118】 2118: 墨墨的等式 (最短路)
2118: 墨墨的等式 Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求 ...
- bzoj 2118: 墨墨的等式
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+-+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- bzoj 2118: 墨墨的等式 spfa
题目: 墨墨突然对等式很感兴趣,他正在研究\(a_1x_1+a_2y_2+ ... +a_nx_n=B\)存在非负整数解的条件,他要求你编写一个程序,给定\(N,\{a_n\}\)以及\(B\)的取值 ...
- [图论训练]BZOJ 2118: 墨墨的等式 【最短路】
Description 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在 ...
- BZOJ2118墨墨的等式[数论 最短路建模]
2118: 墨墨的等式 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1317 Solved: 504[Submit][Status][Discus ...
- 【BZOJ2118】墨墨的等式(最短路)
[BZOJ2118]墨墨的等式(最短路) 题面 BZOJ 洛谷 题解 和跳楼机那题是一样的. 只不过走的方式从\(3\)种变成了\(n\)种而已,其他的根本没有区别了. #include<ios ...
随机推荐
- 《Remus: High Availability via Asychronous Virtual Machine Replication》翻译
Abstract 想要让应用能够躲过硬件故障是一项非常昂贵的任务,因为这通常意味着对软件进行重构,使它包含复杂的恢复逻辑的同时需要部署专用的硬件,而这些对于提升大型的或者遗留的应用的可靠性是巨大的障碍 ...
- Vijos1053 Easy sssp[spfa 负环]
描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...
- iOS Block 内存管理的探讨
在很多情况下Block是造成程序循环引用内存泄漏的元凶.下面我们就讲解一下block对内存管理的影响.在讲解之前.希望大家对block有一定的了解.如果大家还不是太清楚block的实现原理.希望大家可 ...
- Tstringlist分割字符串的用法?
TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 1.CommaText 2.Delimiter &Delim ...
- ext 自带搜索功能
- mongodb.conf配置文件详解
mongod --config /etc/mongodb.conf 配置如下:verbose:日志信息冗余.默认false.提高内部报告标准输出或记录到logpath配置的日志文件中.要启用verbo ...
- (原创)解决远程桌面连接远程应用时,出现 '应用程序错误: '0x7c931780'指令引用的 '0x89abcdef' 内存。该内存不能为 'read'"
公司的部分应用为cs结构,没有web版的,这些应用的外部访问基本都是通过使用windows server 2008 r2的远程桌面服务来实现的. 个人感觉微软远程桌面服务问题很多,今天有同事使用Rem ...
- linux ISO/IMG make
sudo dd if=/PATH/*.ISO of=/dev/sdb 1.制作启动U盘需要sdb,不能sdb1,否则会提示isolinux.bin文件丢失 2.TF卡,设置sdb1?忘了 /* sy ...
- [TED] New video technology that reveals an objects hidden properties
通过视频中,即使1微米的震动,都能够还原成声音. 程序算法结合基础学科,能够发挥出接近无限的力量, 深入挖掘物理特性,形成你想都想不到的效果. 很多技术你都不知道,怎么和国家对抗?所以还是要遵纪守法 ...
- Logging的这点小事
真正做项目,才发觉Logging的好处和学问.自己胡搞的时候,常常使用System.out.println作为输出. 但实际的项目,尤其是library比较多的时候,好好配置好Logging,才能在后 ...