日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

JZOJ #4722 跳楼机 (最短路模型的完美转化)

發(fā)布時(shí)間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JZOJ #4722 跳楼机 (最短路模型的完美转化) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述:

給出$h,x,y,z$,求在$h$以內(nèi),$x,y,z$可以湊出多少個(gè)不同的數(shù)。$(1\leq{h}\leq{10^{18}},1\leq{x,y,z}\leq{10^5})$

解題思路:

直接做顯然不好做。我們考慮取$n$個(gè)$y$和$m$個(gè)$z$,然后再加上$x,2*x,3*x\cdots$,顯然地,只要對(duì)于每種取法,$(ny+mz)\%x$的值不同的話,就不會(huì)有重復(fù)。所以我們先求出$d_{i}=c$表示通過選$y$和$z$使得和模$x$等于$i$的最小和$c$。然后答案就是$\sum_{i=0}^{x-1}(\lfloor\frac{h-d_{i}}{x}\rfloor+1)$。至于怎么求$d_{i}$,可以發(fā)現(xiàn)$d_{(i+y)\%x}=d_{i}+y$,$d_{(i+z)\%x}=d_{i}+z$,明顯的最短路模型。

代碼:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define i64 long long
 6 using namespace std;
 7 
 8 const int N = 1e5 + 10;
 9 const i64 inf = 1e18 + 1;
10 int x, y, z, inq[N];
11 i64 h, ans, d[N];
12 
13 queue <int> q;
14 
15 void spfa() {
16     for (int i = 0; i < x; i ++) d[i] = inf;
17     d[1] = 1 % x;
18     q.push(1);
19     inq[1] = 1;
20     while (!q.empty()) {
21         int i = q.front(); q.pop();
22         if (d[i] + y < d[(i + y) % x]) {
23             d[(i + y) % x] = d[i] + y;
24             if (!inq[(i + y) % x]) {
25                 inq[(i + y) % x] = 1;
26                 q.push((i + y) % x);
27             }
28         }
29         if (d[i] + z < d[(i + z) % x]) {
30             d[(i + z) % x] = d[i] + z;
31             if (!inq[(i + z) % x]) {
32                 inq[(i + z) % x] = 1;
33                 q.push((i + z) % x);
34             }
35         }
36         inq[i] = 0;
37     }
38 }
39 
40 int main() {
41     scanf("%lld", &h);
42     scanf("%d %d %d", &x, &y, &z);
43     spfa();
44     for (int i = 0; i < x; i ++) if (h > d[i]) ans += (h - d[i]) / x + 1;
45     printf("%lld", ans);
46     return 0;
47 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/awner/p/5796948.html

總結(jié)

以上是生活随笔為你收集整理的JZOJ #4722 跳楼机 (最短路模型的完美转化)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。