题意:求$K$的倍数中数位和的最小值。

一开始有一种思路:由于产生答案的数字可能非常大,不便枚举,考虑转化为构造一个数字可以有$x\mod k=0$。然后二分答案数位和,数位DP检验是否存在,但是由于数位DP还是局限于有限大小的数字,所以并不可行。`````

套路见少了。实际上,这种构造数字满足某些条件的,一定可以把目标数字看成是一位一位写下来的,比如$114514$,可以看成逐步写下$1,1,4,5,1,4$不断$\times 10$做加法的结果。所以考虑设$dis_i$为所有$x\mod k=i$的同余类中数位和最小的,然后考虑从当前的这个状态开始写下一位,即连边$i\to (10i+j)\mod k$,边权$j$,然后最初从$dis_{1\sim 9}$(首位)为源点跑最短路,求$dis_0$即可。

atcoder的公式题解给的思路差不多,对于每个数,可以选择加$1$或者乘$10$,前者连边权为$1$,后者连边权$0$,和上面差不多,然后跑$01$最短路。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define dbg(x) cerr << #x << " = " << x <<endl
#define dbg2(x,y) cerr<< #x <<" = "<< x <<" "<< #y <<" = "<< y <<endl
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
template<typename T>inline T _min(T A,T B){return A<B?A:B;}
template<typename T>inline T _max(T A,T B){return A>B?A:B;}
template<typename T>inline bool MIN(T&A,T B){return A>B?(A=B,):;}
template<typename T>inline bool MAX(T&A,T B){return A<B?(A=B,):;}
template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
template<typename T>inline T read(T&x){
x=;int f=;char c;while(!isdigit(c=getchar()))if(c=='-')f=;
while(isdigit(c))x=x*+(c&),c=getchar();return f?x=-x:x;
}
const int N=1e5+;
struct thxorz{int nxt,to,w;}G[N*];
int Head[N],tot;
int n;
inline void Addedge(int x,int y,int z){G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot,G[tot].w=z;}
int dis[N];
priority_queue<pii,vector<pii>,greater<pii> > pq;
#define y G[j].to
inline void dij(){
memset(dis,0x3f,sizeof dis);for(register int i=;i<;++i)MIN(dis[i%n],i),pq.push(make_pair(dis[i%n],i%n));
while(!pq.empty()){
int d=pq.top().first,x=pq.top().second;pq.pop();
if(d^dis[x])continue;//dbg2(x,d);
if(x==)return;
for(register int j=Head[x];j;j=G[j].nxt)if(MIN(dis[y],d+G[j].w))pq.push(make_pair(dis[y],y));
}
}
#undef y
int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
read(n);
for(register int i=;i<n;++i)for(register int j=;j<;++j)Addedge(i,(*i+j)%n,j);
dij();
return printf("%d\n",dis[]),;
}

总结:构造数字题常用思路or最短路常用思路:逐步拆分、分解“代价”为每一步的边的边权。

ABC007D Small Multiple[最短路]的更多相关文章

  1. AtCoder Beginner Contest 077 D Small Multiple(最短路)

    水过前三道题之后,一直在写这个题,做不对.总有那么几组数据过不去... 看了看题解是最短路,这思路感觉很神奇.看了下唯一做出来这题的那人的代码,是搜索做的. 标程: 对每个数字x,向x+1建一条花费为 ...

  2. [USACO14OPEN] Dueling GPS's[最短路建模]

    题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...

  3. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  4. hdu 4960 Another OCD Patient (最短路 解法

    http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...

  5. CF449B Jzzhu and Cities (最短路)

    CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...

  6. HDU 5876 Sparse Graph BFS 最短路

    Sparse Graph Problem Description   In graph theory, the complement of a graph G is a graph H on the ...

  7. hdu-5521 Meeting(最短路)

    题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  8. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  9. LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏

    I am going to my home. There are many cities and many bi-directional roads between them. The cities ...

随机推荐

  1. [转帖]彻底理解cookie,session,token

    彻底理解cookie,session,token https://www.cnblogs.com/moyand/p/9047978.html 发展史 1.很久很久以前,Web 基本上就是文档的浏览而已 ...

  2. 【转帖】 解开龙芯与mips4000的关系

    -- 苏联给的套件,我们只要把电子管插上就好. -- 千万次机器,不晓得来源 DJS-130系列,16位小型机,仿造美国NOVA DJS-180系列,超级小型机,仿造美国DEC VAX, 能跑DEC的 ...

  3. 初学K3Cloud开发

    1.BOS中在新建的空白对象中添加一个下推按钮 1.点击“菜单集合”属性 2.在打开的窗体中,点中“工具条”,新增一个按钮 3.将新增的按钮标题改为“下推”,并配置点击事件 列表菜单增加“下推”类似, ...

  4. Yii2.0 组件

    框架之所以是框架,是因为其强大,其封装了很多实用的功能,开发者可以开箱即用. 下边列举Yii2.0的部分组件: var_dump(Yii::$app->session->getId()); ...

  5. vue 评论 computed watch 分隔符 局部组件 全局组件 子传父消息|父传子消息

    评论案例 splice: (start 几位,替换(新增)内容) splice(0,0,内容)在头部插入内容 splice(0,1) 把索引为0的往后删除1位  splice(0,1,内容)把索引为0 ...

  6. crm--rbac权限组件使用步骤

    本人的权限组件码云地址:https://gitee.com/shiguanggege/rbac 里面有文档详细介绍权限组件的使用步骤

  7. 进阶Java编程(5)基础类库

    Java基础类库 1,StringBuffer类 String类是在所有项目开发之中一定会使用到的一个功能类,并且这个类拥有如下的特点: ①每一个字符串的常量都属于一个String类的匿名对象,并且不 ...

  8. C C语言中 *.c和*.h文件的区别!

    一个简单的问题:.c和.h文件的区别学了几个月的C语言,反而觉得越来越不懂了.同样是子程序,可以定义在.c文件中,也可以定义在.h文件中,那这两个文件到底在用法上有什么区别呢? 2楼:子程序不要定义在 ...

  9. html homework27

    1. 使用框架完成如下功能 将框架先上下分割成两部分(上半部分的为TopFrame).再将下半部分垂直分割为两部分(左侧为BottomLeftFrame,右侧为BottomRightFrame),为T ...

  10. C#中判断文件夹或文件是否存在的方法

    一.根据虚拟路径获取文件物理路径: string savePath = Server.MapPath("~/Uploads/RemoteDatum/"); 二.判断文件夹是否存在 ...