1 Star 0 Fork 0

ShawnBox / OI

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
CodeForces - 510D.cpp 2.13 KB
一键复制 编辑 原始数据 按行查看 历史
ShawnBox 提交于 2018-09-02 20:54 . 2018/9/2-20:54
//unordered_map 124ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<tr1/unordered_map>
using namespace std;
using namespace std::tr1;
const int MAXN=305;
int n,t[MAXN],a[MAXN];
unordered_map<int,int> vis,d;
struct node{
int u,val;
node(){}
node(int _u,int _val):u(_u),val(_val){}
bool operator < (const node &x) const{
return val>x.val;
}
};
int gcd(int x,int y){if(!y)return x;return gcd(y,x%y);}
void dj(int s){
priority_queue<node> q;
d[s]=0;
q.push(node(s,0));
while(!q.empty()){
int u=q.top().u; q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i=1;i<=n;i++){
int v=gcd(u,t[i]);
if(d.find(v)==d.end()||d[v]>d[u]+a[i]){
d[v]=d[u]+a[i];
q.push(node(v,d[v]));
}
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&t[i]);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
dj(0);
if(d.find(1)==d.end()) return puts("-1"),0;
printf("%d\n",d[1]);
}
/*
//ͨmap 140ms
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=305;
int n,t[MAXN],a[MAXN];
map<int,int> vis,d;
struct node{
int u,val;
node(){}
node(int _u,int _val):u(_u),val(_val){}
bool operator < (const node &x) const{
return val>x.val;
}
};
int gcd(int x,int y){if(!y)return x;return gcd(y,x%y);}
void dj(int s){
priority_queue<node> q;
d[s]=0;
q.push(node(s,0));
while(!q.empty()){
int u=q.top().u; q.pop();
if(vis[u]) continue;
vis[u]=1;
for(int i=1;i<=n;i++){
int v=gcd(u,t[i]);
if(d.find(v)==d.end()||d[v]>d[u]+a[i]){
d[v]=d[u]+a[i];
q.push(node(v,d[v]));
}
}
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&t[i]);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
dj(0);
if(d.find(1)==d.end()) return puts("-1"),0;
printf("%d\n",d[1]);
}
*/
C++
1
https://gitee.com/ShawnBox/OI.git
git@gitee.com:ShawnBox/OI.git
ShawnBox
OI
OI
master

搜索帮助