E - Elevator

时间:2023-03-08 22:59:55
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来填充

//用最小的来做同余系比较快

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define INF 9187201950435737471
#define ls rt<<1
#define rs rt<<1|1
#define lson ls,nl,mid,l,r
#define rson rs,mid+1,nr,l,r
#define N 100010
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar() using namespace std;
long long h;
long long a,b,c,ans;
long long d[];
queue<long long>q;
bool vis[]; struct node{
long long v;
long long n;
node *next;
}*e[]; void in(long long &x){
long long y=;
char c=g();x=;
while(c<''||c>''){
if(c=='-')y=-;
c=g();
}
while(c<=''&&c>=''){
x=(x<<)+(x<<)+c-'';c=g();
}
x*=y;
}
void o(long long x){
if(x<){
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void push(long long x,long long y,long long v){
node *p;
p=new node();
p->n=y;
p->v=v;
if(e[x]==)
e[x]=p;
else{
p->next=e[x]->next;
e[x]->next=p;
}
} void spfa(){
For(i,,a)
d[i]=INF;
q.push(%a);
d[%a]=;
while(!q.empty()){
long long t=q.front();
q.pop();
vis[t]=true; for(node *i=e[t];i;i=i->next){
if(d[i->n]>d[t]+i->v){
d[i->n]=d[t]+i->v;
if(!vis[i->n]){
q.push(i->n);
vis[i->n]=true;
}
}
}
vis[t]=false;
}
} int main(){
freopen("elevator.in","r",stdin);
freopen("elevator.out","w",stdout);
in(h);
in(a);in(b);in(c);
if(a>b) swap(a,b);
if(a>c) swap(a,c);
For(i,,a-){
push(i,(i+b)%a,b);
push(i,(i+c)%a,c);
}
spfa();
For(i,,a-)
if(h>=d[i])
ans+=+(h-d[i])/a;
o(ans);
return ;
}