E - Elevator
E - Elevator
http://codeforces.com/gym/241680/problem/E
同余最短路,从0~a-1中每一个i向(i+b)%a连一条权值为b的边,向(i+c)%a连一条权值为c的边,然后跑spfa最短路,此时d[i]表示达到x%a花费的最小的距离,这里放的只有b,c,(解释一下,b,c组合出的实际大小为x,因为x过大,数组存不下,所以表示为x%a
最后统计答案的时候ans+=1+(h-d[i])/a;当前只有b,c组合出的d[i]算一个答案,然后剩下可以用a来填充
//用最小的来做同余系比较快
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cmath> 6 #include<ctime> 7 #include<set> 8 #include<map> 9 #include<stack> 10 #include<cstring> 11 #define inf 2147483647 12 #define INF 9187201950435737471 13 #define ls rt<<1 14 #define rs rt<<1|1 15 #define lson ls,nl,mid,l,r 16 #define rson rs,mid+1,nr,l,r 17 #define N 100010 18 #define For(i,a,b) for(long long i=a;i<=b;i++) 19 #define p(a) putchar(a) 20 #define g() getchar() 21 22 using namespace std; 23 long long h; 24 long long a,b,c,ans; 25 long long d[1000010]; 26 queue<long long>q; 27 bool vis[1000010]; 28 29 struct node{ 30 long long v; 31 long long n; 32 node *next; 33 }*e[10000010]; 34 35 void in(long long &x){ 36 long long y=1; 37 char c=g();x=0; 38 while(c<'0'||c>'9'){ 39 if(c=='-')y=-1; 40 c=g(); 41 } 42 while(c<='9'&&c>='0'){ 43 x=(x<<1)+(x<<3)+c-'0';c=g(); 44 } 45 x*=y; 46 } 47 void o(long long x){ 48 if(x<0){ 49 p('-'); 50 x=-x; 51 } 52 if(x>9)o(x/10); 53 p(x%10+'0'); 54 } 55 56 void push(long long x,long long y,long long v){ 57 node *p; 58 p=new node(); 59 p->n=y; 60 p->v=v; 61 if(e[x]==0) 62 e[x]=p; 63 else{ 64 p->next=e[x]->next; 65 e[x]->next=p; 66 } 67 } 68 69 void spfa(){ 70 For(i,0,a) 71 d[i]=INF; 72 q.push(1%a); 73 d[1%a]=1; 74 while(!q.empty()){ 75 long long t=q.front(); 76 q.pop(); 77 vis[t]=true; 78 79 for(node *i=e[t];i;i=i->next){ 80 if(d[i->n]>d[t]+i->v){ 81 d[i->n]=d[t]+i->v; 82 if(!vis[i->n]){ 83 q.push(i->n); 84 vis[i->n]=true; 85 } 86 } 87 } 88 vis[t]=false; 89 } 90 } 91 92 int main(){ 93 freopen("elevator.in","r",stdin); 94 freopen("elevator.out","w",stdout); 95 in(h); 96 in(a);in(b);in(c); 97 if(a>b) swap(a,b); 98 if(a>c) swap(a,c); 99 For(i,0,a-1){ 100 push(i,(i+b)%a,b); 101 push(i,(i+c)%a,c); 102 } 103 spfa(); 104 For(i,0,a-1) 105 if(h>=d[i]) 106 ans+=1+(h-d[i])/a; 107 o(ans); 108 return 0; 109 }