cf B. Mishka and trip (数学)
来源:互联网 发布:行知外国语学校电话 编辑:程序博客网 时间:2024/05/29 01:55
题意
Mishka想要去一个国家旅行,这个国家共有
注意:任何两个城市之间最多只有一条路。
思路
先考虑所有中心城市:每条路只能算一遍,那么我们可以得到如下公式:
其中,
最后再枚举环中的
注意:答案可能超出int,用long long
AC代码
#include <stdio.h>#include <string.h>typedef long long LL;const int maxn = 100000+5;int n, k;int c[maxn], d[maxn];bool cp[maxn];int main() { while(scanf("%d%d", &n, &k) == 2) { memset(cp, 0, sizeof(cp)); LL sum = 0; for(int i = 1; i <= n; i++) { scanf("%d", &c[i]); sum += c[i]; } int id; for(int i = 0; i < k; i++) { scanf("%d", &d[i]); cp[d[i]] = true; } //all capital cities int tol = 0; LL ans = 0; for(int i = 0; i < k; i++) { id = d[i]; tol += c[id]; ans += 1LL*c[id]*(sum-tol); } //the road bettwen two cities which aren't capital if(!cp[1] && !cp[n]) ans += c[1]*c[n]; for(int i = 1; i < n; i++) { if(!cp[i] && !cp[i+1]) ans += c[i]*c[i+1]; } printf("%lld\n", ans); } return 0;}
如有不当之处欢迎指出!
阅读全文
0 0
- cf B. Mishka and trip (数学)
- Mishka and trip(CF 703B)
- (模拟+数学)codeforces - 703B Mishka and trip
- CodeForces703B Mishka and trip 数学
- codeforces B. Mishka and trip
- 703B - Mishka and trip
- Mishka and trip(CF #365 Div. 2)
- 【打CF,学算法——二星级】Codeforces 703B Mishka and trip (统计)
- codeforces 703B Mishka and trip
- 【Codeforces 703B - Mishka and trip】
- Codeforces-703B Mishka and trip
- CodeForces 703B Mishka and trip(图论)
- CodeForces 703B Mishka and trip【水题】
- CodeForces 703B Mishka and trip
- CodeForces 703B Mishka and trip
- Coderforces 703B Mishka and trip
- codeforces 703B. Mishka and trip
- Codeforces 703B Mishka and trip
- 1017代码比较
- openstack学习笔记——各组件介绍
- Mysql数据库优化
- 使用Hashset使得字符串数组中不包含重复字符串
- 从快速排序中的递归想法
- cf B. Mishka and trip (数学)
- 刷题笔记:C/C++工程师能力评估1
- 欢迎使用CSDN-markdown编辑器
- 面经
- 6.6 solr和elasticsearch的区别
- 最小生成树---普里姆算法
- 产生随机数猜数字游戏。。。
- 动态规划:数字游戏
- Web前端笔试题