Codeforces-703B Mishka and trip
来源:互联网 发布:大数据分析的作用 编辑:程序博客网 时间:2024/05/29 15:27
题目大意:
给你n个点,标号为1-n,并且相邻的两个点之间存在路径,其路径权值为路径两端点权值的乘积,现在在1-n中有k个点,这k个点对其它任意一点都需要存在路径,但是两个点之间的路径不能超过一条。也就是没有重边,现在问你整个图的路径权值之和是多少
解题思路:
因为这题的数据为10w所以不可能用两两之间路径来枚举,那么有个简单的思路就是只需要知道与i点相连的路径权值之和就可以了。
而对于k个点来说,直接计算与整个图的点的路径权值之和就行了。
对于第i个点与它相连的只存在两种情况,k中的点是i的邻点或不是
这样先预处理出所有点的权值和,以及k个点的权值和再
代码:
#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5 + 5;int c[maxn], id[maxn];int main(){ int a, b, n, k, sum = 0, sumk = 0; scanf("%d%d", &n, &k); for(int i = 0; i < n; ++i){ scanf("%d", &c[i]); sum += c[i]; } for(int i = 0; i < k; ++i) { scanf("%d", &id[i]); id[i] -= 1; sumk += c[id[i]]; } LL ans = 0, tmp; for(int i = 0, j = 0; i < n; ++i){ a = (i + 1) % n; b = (i - 1 + n) % n; if(i == id[j]){ tmp = sum - c[i]; ++j; }else{ tmp = c[a] + c[b] + sumk; int pos = lower_bound(id, id + k, a) - id; if(id[pos] == a) tmp -= c[a]; pos = lower_bound(id, id + k, b) - id; if(id[pos] == b) tmp -= c[b]; } ans += tmp * c[i]; } printf("%I64d\n", ans >> 1); return 0;}
0 0
- 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
- Codeforces 703B Mishka and trip
- codeforces B. Mishka and trip
- 703B - Mishka and trip
- (模拟+数学)codeforces - 703B Mishka and trip
- Mishka and trip(CF 703B)
- Coderforces 703B Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) -- B. Mishka and trip
- Codeforces Round #365 (Div. 2) [B] Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- MPEG4 (ISO/IEC 14496) 文档内容 简介
- HDU 2546 饭卡
- 容器
- hdoj 2389 二分图 Hopcroft-Carp 模板
- 几天几夜,终解移动MM计费SDK接入bug
- Codeforces-703B Mishka and trip
- HDU Ball
- 扩展欧几里德算法详解以及乘法逆元
- HDU 4115(Eliminate the Conflict-石头剪刀布,经典2sat问题)
- 深入浅出RxJava(一:基础篇)
- 活点地图
- winform 分页控件,源码下载
- iOS5学习笔记_GLKit_Part1_王云鹏
- adjacent_find 源码剖析