【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 ...
随机推荐
- Android开发中上线后修改应用名称的若干问题
一.在Android Studio 1.3中修改app的包名: 需求来源: 之前开发的app已经在腾讯的应用宝上线,应客户要求,app需要改名字,这个就有点麻烦了.如果申请改名字,要求如下: 截图上图 ...
- URL和URI的区别和联系
URI:Universal Resource Identifier,通用资源标识符: URL:Uniform Resource Locator,统一资源定位符: 其中,URL ...
- flex sdk中mx_internal function getTextField() 这种函数如何调用?
在用flex 开发中,一些函数前打上了 mx_internal 外部调用不了,其实这样写就可以了 xxx.mx_internal::getTextField() 而 xxx.getTextField( ...
- sql将同一个表中的两列Int数据相加,有些数据是空的
不能这样加! 有些数据为null吧,null+任何值=null 如这句话就不行 update HY_MYGGCYSWCQKB set HY_MYGGCYSWCQKB.Sj_By_Sr=HY_MYGGC ...
- C语言操作内存
炒了个栗子 #include <stdio.h> #define N1 (5) #define N2 (7) #define N3 (3) int main(){ int *ip; int ...
- java工程中的.classpathaaaaaaaaaaaaaaaa<转载>
第一部分:classpath是系统的环境变量,就是说JVM加载类的时候要按这个路径下去找,当然这个路径下可以有jar包,那么就是jar包里所有的class. eclipse build path是ec ...
- Linux中 groupadd 和 useradd 的命令说明
groupadd [options] group 说明The groupadd command creates a new group account using the values specifi ...
- BZOJ 1096 【ZJOI2007】 仓库建设
Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L公司一般把产品直接堆放在露天,以节省费用.突然有一天, ...
- Html5 Egret游戏开发 成语大挑战(二)干净的eui项目和资源准备
现在我们使用egret来起步开发一个名叫<成语大挑战>的小游戏,关于egret的开发环境就不在这里啰嗦了,直接去官方下载安装就可,egret是我见过开发环境部署最简单的解决方案,这个系列教 ...
- C#中使用Log4net日志输出到本地文件、Textbox或Listview
网上很多配置log4net的方法,但是排行靠前的 根本就没有说明清除,导致浪费了两个小时来搞清楚如何配置,真是无语,特写此文,给那些刚接触log4net的朋友 1.参考链接:http://blog.s ...