题意

  Mishka想要去一个国家旅行,这个国家共有个城市,城市通过道路形成一个环,即第i个城市和第个城市之间有一条道路,此外城市和之间有一条道路。这个城市中有个首中心城市,中心城市与每个城市(除了自己)之间有一条道路。第城市个城市有一个魅力值,经过一条连接第个和第个城市的道路的费用是,求所有道路的费用之和是多少?

  注意:任何两个城市之间最多只有一条路。

思路

  先考虑所有中心城市:每条路只能算一遍,那么我们可以得到如下公式:

  其中,代表所有城市的魅力值的和,代表已经计算过的中心城市的魅力值,那么就代表第个中心城市连接其他城市道路的魅力值之和(无重复计算)。

  最后再枚举环中的条边,如果某条边连接的两个城市中有一个是中心城市,说明这条路已经计算过,不要重复计算。

  注意:答案可能超出int,用long long

AC代码

  1. #include <stdio.h>
  2. #include <string.h>
  3. typedef long long LL;
  4. const int maxn = 100000+5;
  5. int n, k;
  6. int c[maxn], d[maxn];
  7. bool cp[maxn];
  8. int main() {
  9. while(scanf("%d%d", &n, &k) == 2) {
  10. memset(cp, 0, sizeof(cp));
  11. LL sum = 0;
  12. for(int i = 1; i <= n; i++) {
  13. scanf("%d", &c[i]);
  14. sum += c[i];
  15. }
  16. int id;
  17. for(int i = 0; i < k; i++) {
  18. scanf("%d", &d[i]);
  19. cp[d[i]] = true;
  20. }
  21. //all capital cities
  22. int tol = 0;
  23. LL ans = 0;
  24. for(int i = 0; i < k; i++) {
  25. id = d[i];
  26. tol += c[id];
  27. ans += 1LL*c[id]*(sum-tol);
  28. }
  29. //the road bettwen two cities which aren't capital
  30. if(!cp[1] && !cp[n]) ans += c[1]*c[n];
  31. for(int i = 1; i < n; i++) {
  32. if(!cp[i] && !cp[i+1]) ans += c[i]*c[i+1];
  33. }
  34. printf("%lld\n", ans);
  35. }
  36. return 0;
  37. }

如有不当之处欢迎指出!

cf B. Mishka and trip (数学)的更多相关文章

  1. codeforces 703B B. Mishka and trip(数学)

    题目链接: B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input stan ...

  2. 暑假练习赛 003 F Mishka and trip

    F - Mishka and trip Sample Output   Hint In the first sample test: In Peter's first test, there's on ...

  3. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

  4. cf703B Mishka and trip

    B. Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input  standard ...

  5. Codeforces 703B. Mishka and trip 模拟

    B. Mishka and trip time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  6. CodeForces 703A Mishka and trip

    Description Little Mishka is a great traveller and she visited many countries. After thinking about ...

  7. B. Mishka and trip

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  8. CF A.Mishka and Contest【双指针/模拟】

    [链接]:CF/4892 [题意]: 一个人解决n个问题,这个问题的值比k小, 每次只能解决最左边的或者最右边的问题 解决了就消失了.问这个人能解决多少个问题. [代码]: #include<b ...

  9. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

随机推荐

  1. linux_inotify

    什么是inotify? 拥有强大.粒细粒度.异步文件系统事件监控机制,监控文件系统中添加.删除.修改.移动等各种事件 版本支持: 内核 2.6.13以上版本,inotify-tools 是实施监控的软 ...

  2. scrapy-redis功能简介

    connection:连接redis最基本文件 default:默认值设置文件 dupefiler_key 保存指纹 dupefilter:替换scrapy默认的url去重器 piklecompat: ...

  3. Servlet--Servlet接口

    servlet主要数据结构 Servlet 接口:主要定义了servlet的生命周期方法 ServletConfig接口:为servlet提供了使用容器服务的若干重要对象和方法. ServletCon ...

  4. linkin大话数据结构--字符串,数组,list之间的互转

    在实际开发中,我们经常会用到字符串,字符数组,字符list,当然也会不可避免的进行这3者之间的互相转换. 在使用到Apache和Google下的common包,可以这样子实现: package tz. ...

  5. linkin大话面向对象--方法详解

    1,方法的参数传递机制:值传递. 首先弄懂2个概念:形参和实参. 形参(形式参数):相当于函数(Java中也把函数称之为方法)中的局部变量,在函数被调用时创建,并以传入的实参作为起始值,函数调用结束时 ...

  6. Oracle 视图 (待更新, 缓存)

    参考: 视图.索引.存储过程优缺点: http://www.cnblogs.com/SanMaoSpace/p/3147059.html oracle视图总结(转):http://tianwei013 ...

  7. JavaScript数据结构 (手打代码)

    array: 数组创建: ); //创建一个长度为6的数组 ,,,,,); 数组方法: var str="I love javascript"; var single=str.sp ...

  8. tensorflow Image 解码函数

    觉得有用的话,欢迎一起讨论相互学习~Follow Me tf.image.decode_png(contents, channels=None, name=None) Decode a PNG-enc ...

  9. 前端 js技术

    1.JavaScript代码存在形式 <!-- 方式一 --> <script type"text/javascript" src="JS文件" ...

  10. 如何将top命令输出重定向为文件

    命令: # top -b -n 2 -d 3 > /tmp/top.out 解析: -b: batch 模式,可以重定向到文件中 -n:一共取2次top数据 -d:每次top数据间隔为3秒