CodeForces-510D

https://vjudge.net/problem/CodeForces-510D
topic can be converted to spend a minimum cost to choose some number, then these numbers can be obtained through addition and subtraction 1 or -1, or 1 you can scrape it out, once the Minato out 1 , others have been. Bezu the Theorem, ax + by = gcd (a , b) = 1, Bezu theorems can be extended to n, ax + by + cz + ... = gcd (a, b, c, ...). Order m [i] is obtained as a minimum cost gcd of i, SPFA violence update, the final output m [1]

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <unordered_map>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)

using namespace std;
int n,k;
int L[N],c[N];
queue<int>q;
unordered_map<int,int>m;
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

int gcd(int a,int b){
    return (a%b==0?b:gcd(b,a%b));
}

void spfa(){
    while(!q.empty()){
        int t=q.front();q.pop();
        For(i,1,n){
            k=gcd(L[i],t);
            if(!m[k]){
                q.push(k);
                m[k]=m[t]+m[L[i]];
            }
            else
                m[k]=min(m[k],m[t]+m[L[i]]);
        }
    }
}

signed main(){
    in(n);
    For(i,1,n)
        in(L[i]);
    For(i,1,n)
        in(c[i]);
    For(i,1,n)
        if(!m[L[i]])
            m[L[i]]=min(m[L[i]],c[i]);
        else{
            q.push(L[i]);
            m[L[i]]=c[i];
        }
    spfa();
    if(m[1])
        o(m[1]);
    else
        o(-1);
    return 0;
    
}

 

Guess you like

Origin www.cnblogs.com/war1111/p/11315572.html