Codeforces Round #290 (Div. 2)D - Fox And Jumping——数论gcd=1
来源:互联网 发布:格罗兹尼巷战 知乎 编辑:程序博客网 时间:2024/05/15 21:54
http://codeforces.com/contest/510/problem/D
给你n个卡片和每个卡片的花费,求用最少的花费使得用所买的卡片可以到达任意的位置
只要所取的卡片的最大公约数为1就可以到达任意的位置,贪心维护最小值,最后结果为mp[1]
#include<bits/stdc++.h>using namespace std;int n;int l[310],c[310];map<int,int> mp;priority_queue<int> q;int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int main(){ cin>>n; for(int i=0;i<n;++i) scanf("%d",l+i); for(int i=0;i<n;++i) scanf("%d",c+i); for(int i=0;i<n;++i){ if(mp.count(l[i])) mp[l[i]]=min(mp[l[i]],c[i]); else{ mp[l[i]]=c[i]; q.push(l[i]); } } while(!q.empty()){ int k=q.top();q.pop(); for(int i=0;i<n;++i){ int t=gcd(k,l[i]); int cost=mp[k]+mp[l[i]]; if(mp.find(t)==mp.end()){ mp[t]=cost; q.push(t); } else if(mp[t]>cost){ mp[t]=cost; } } } if(!mp[1]) printf("-1\n"); else printf("%d\n",mp[1]); return 0;}
0 0
- Codeforces Round #290 (Div. 2)D - Fox And Jumping——数论gcd=1
- Codeforces Round #290 (Div. 2) D. Fox And Jumping GCD问题
- Codeforces Round #290 (Div. 2) D Fox And Jumping
- Codeforces Round #290 (Div. 2)D. Fox And Jumping
- Codeforces Round #290 (Div. 2)-D. Fox And Jumping
- Codeforces Round #290 (Div. 2) C. Fox And Names && D. Fox And Jumping
- Codeforces Round #290 D. Fox And Jumping
- Codeforces Round #290 (Div. 1)B. Fox And Jumping
- Codeforces Round #290 (Div. 1) C Fox And Jumping
- Codeforces Round #290 (Div. 1) B. Fox And Jumping
- #290 (div.2) D. Fox And Jumping
- codeforces 510D D. Fox And Jumping(dp+数论)
- codeforces 510D D. Fox And Jumping(dp+数论)
- Codeforces Round #228 (Div. 2) D - Fox and Minimal path
- 510D Fox And Jumping(dp+gcd)
- Codeforces Round #290 (Div. 2)C - Fox And Names——拓扑排序
- Codeforces Round #290 (Div. 2)B - Fox And Two Dots——并查集/搜索
- Codeforces Round #290 (Div. 2)E. Fox And Dinner——最大流 奇偶建图
- struts2中页面跳转问题个人小结(一)
- NGUI控件说明(中文) UIToggle
- 执行计划查看方法(Explain plan)
- odoo8下新增时初始化明细项目的新增
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- Codeforces Round #290 (Div. 2)D - Fox And Jumping——数论gcd=1
- 《HTTP权威指南》读书笔记(1)-HTTP简介与消息结构
- [UVA10245] The Closest Pair Problem && 暴力版本
- leetcode_143_Reorder List
- iOS开发/设计面试题汇总
- WebView基本使用
- 关系型数据库与NOSQL数据库的区别
- 在PHPStorm中调试PHP代码
- Keepalived+nginx实现双主高可用负载均衡