同余最短路(P3403 跳楼机)

同余最短路

前置

给定m个数,这m个数可以重复取,问最大的这m个数不能拼成的数,或者给定一定范围,范围里有多少个数是这m个数可以拼成的,对于这种问题我们可以考虑同余最短路的算法。

P3403 跳楼机

同余最短路介绍

首先我们要选择一个 b a s e base base作为基底,之后所有的距离就可以描述成 a ∗ b a s e + b a * base + b abase+b

在这题中我们选定 x x x作为base。

d i s [ i ] = v a l u e dis[i] = value dis[i]=value有如下含义: v a l u e ≡ i ( m o d x ) value \equiv i \pmod x valuei(modx),也就是同余的条件下,我们可以到达的最小的楼层,这样我们剩下的楼层就可以通过 + x + x +x的操作去访问,所以我们最后的可以到达的楼层也就是 ∑ i = 0 x − 1 ( h − d i s [ i ] ) / x + 1 \sum _{i = 0} ^ {x - 1} (h - dis[i]) / x + 1 i=0x1(hdis[i])/x+1,这句应该稍加简单理解一下就好了。

接下来我们考虑如何建边,显然有 i − > ( i + y ) % x   c o s t = y , i − > ( i + z ) % x   c o s t = z i - > (i + y) \% x\ cost = y, i -> (i + z) \% x\ cost = z i>(i+y)%x cost=y,i>(i+z)%x cost=z

所以我们可以对所有的 x ∈ [ 0 , x − 1 ] x\in [0, x - 1] x[0,x1],分别同上建立一条边权为 y y y的,一条边权为 z z z的边。然后再去跑一遍最短路,我们就可以得到所有的 d i s dis dis了。

代码

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define endl '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
  ll f = 1, x = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    if(c == '-')    f = -1;
    c = getchar();
  }
  while(c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}

void print(ll x) {
  if(x < 10) {
    putchar(x + 48);
    return ;
  }
  print(x / 10);
  putchar(x % 10 + 48);
}

const int N = 1e5 + 10;

ll h, dis[N];

bool visit[N];

vector<pii> G[N];

void spfa() {
  memset(dis, 0x3f, sizeof dis);
  queue<int> q;
  q.push(1);
  dis[1] = 1, visit[1] = 1;
  while(q.size()) {
    int temp = q.front();
    q.pop();
    visit[temp] = 0;
    for(pii i : G[temp]) {
      if(dis[i.first] > dis[temp] + i.second) {
        dis[i.first] = dis[temp] + i.second;
        if(!visit[i.first]) {
          q.push(i.first);
          visit[i.first] = 1;
        }
      }
    }
  }
}

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  h = read();
  int x = read(), y = read(), z = read();
  if(x == 1 ||  y == 1 || z == 1) {//注意如果有一个数为1,必须特判,不然我们跑出来的最短路dis,将会重复计数。
    printf("%lld\n", h);
    return 0;
  }
  for(int i = 0; i < x; i++) {
    G[i].pb(mp((i + y) % x, y));
    G[i].pb(mp((i + z) % x, z));
  }
  spfa();
  ll ans = 0;
  for(int i = 0; i < x; i++) {
    if(dis[i] <= h) {
      ans += (h - dis[i]) / x + 1;
    }
  }
  printf("%lld\n", ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值