ABC007D Small Multiple[最短路]
题意:求$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[最短路]的更多相关文章
- AtCoder Beginner Contest 077 D Small Multiple(最短路)
水过前三道题之后,一直在写这个题,做不对.总有那么几组数据过不去... 看了看题解是最短路,这思路感觉很神奇.看了下唯一做出来这题的那人的代码,是搜索做的. 标程: 对每个数字x,向x+1建一条花费为 ...
- [USACO14OPEN] Dueling GPS's[最短路建模]
题目描述 Farmer John has recently purchased a new car online, but in his haste he accidentally clicked t ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- hdu 4960 Another OCD Patient (最短路 解法
http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...
- CF449B Jzzhu and Cities (最短路)
CF449B CF450D http://codeforces.com/contest/450/problem/D http://codeforces.com/contest/449/problem/ ...
- HDU 5876 Sparse Graph BFS 最短路
Sparse Graph Problem Description In graph theory, the complement of a graph G is a graph H on the ...
- hdu-5521 Meeting(最短路)
题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- HDU 5521 Meeting(虚拟节点+最短路)
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- 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 ...
随机推荐
- Tei-Wei Kuo
一. A Commitment-based Management Strategy for the Performance and Reliability Enhancement of Flash-m ...
- 阿里redis
参考: 连接: https://help.aliyun.com/document_detail/43848.html?spm=a2c4g.11186623.2.29.295542efrNOQy0 同R ...
- Redis(1.2)Redis的数据结构与基本操作
Redis的数据结构,其本身大方向是键值对 [0]大概特点 相关产品:Redis.Riak.SimpleDB.Chordless.Scalaris.Memcached 形式:Key 指向 Value ...
- getBoundingClientRect()方法
是在<javascript高级程序设计>中看到了这个方法.getBoundingClientRect在IE5中就有,但似乎不怎么引起我们注意. 返回值:它返回一个clientRect对象, ...
- MySql常用字符集
常用字符集 位(bit):是计算机 内部数据 储存的最小单位,11001100是一个八位二进制数. 字节(byte):是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示,1B(byte,字节) ...
- 26-Perl 包和模块
1.Perl 包和模块Perl 中每个包有一个单独的符号表,定义语法为:package mypack;此语句定义一个名为 mypack 的包,在此后定义的所有变量和子程序的名字都存贮在该包关联的符号表 ...
- 修改hosts文件 解决coursera可以登录但无法播放视频的问题
我们经常为了学习或者了解一些领域的知识为访问国外的网站,但是在国内,很多优秀的网站都被封锁了.在GFW(墙)的几种封锁方式中,有一种就是DNS污染,GFW会对域名解析过程进行干扰,使得某些被干扰的域名 ...
- 【原创】大叔经验分享(63)kudu vs parquet
一 对比 存储空间对比: 查询性能对比: 二 设计方案 将数据拆分为:历史数据(hdfs+parquet+snappy)+ 近期数据(kudu),可以兼具各种优点: 1)整体低于10%的磁盘占用: 2 ...
- spring boot 使用RedisTemplate
1导入包 <!-- redis --> <dependency> <groupId>org.springframework.boot</groupId> ...
- 4.Struts2-OGNL
/*ognl 是 strut2 特有的表达式,使用 ognl,struts2 就无需将对象手动放值进request等范围,页面(从值栈中)直接传值*/ OGNL <?xml version=&q ...