题意

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

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

思路

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

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

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

  注意:答案可能超出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;
} 

如有不当之处欢迎指出!

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. CSS深入理解学习笔记之z-index

    1.z-index基础 z-index含义:指定了元素及其子元素的"z顺序",而"z顺序"可以决定元素的覆盖顺序.z-index值越大越在上面. z-index ...

  2. java1.8--1.8入门介绍

    在我之前的工作中,一直使用的是java6.所以即使现在java已经到了1.8,对于1.7增加的新特性我也基本没使用的.在整理一系列1.8的新特性的过程中,我也会添加上1.7增加的特性. 下面的整理可能 ...

  3. POI--HSSFCellStyle类

    通过POI来进行单元格格式的设定 设定格式使用「HSSFCellStyle」类.它有一个构造方法: protected HSSFCellStyle(short index, ExtendedForma ...

  4. IDEA新建Maven项目

    Maven是什么? 当我们在开发一个项目的时候,不可避免地会使用到第三方的类库,而它们又可能依赖着另外的Jar包,又得引入其他Jar包,而且我们很容易就会引漏掉~然后就会报错,有时候报的错会让我们花掉 ...

  5. Mysql基本命令二

    删除id>10的记录:delete from user where id>10; 设置user表的自增字段起始值为10:alter table user anto_increment=10 ...

  6. CSS 自动隐藏文字并添加省略号

    .cut { overflow:hidden; white-space:nowrap; text-overflow:ellipsis; -o-text-overflow:ellipsis; -icab ...

  7. convert图像格式批量转换

    问题:利用GMT绘制生成了eps格式的图像,为了将图像插入到word中,且保持较高的分辨率,利用convert进行图像格式转换,将eps转换成tiff格式. code:  $i ${name}.tif ...

  8. 【转】linux shell ${}简单用法

    为了完整起见,我这里再用一些例子加以说明 ${ } 的一些特异功能: 假设我们定义了一个变量为: file=/dir1/dir2/dir3/my.file.txt 我们可以用 ${ } 分别替换获得不 ...

  9. maps.reg

    ^/(.*\.miaopai.com/stream/.*\.mp4\?.*) http://$1 ^/([[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{ ...

  10. datatables里面的search怎么去掉?

    今天使用datatables插件的时候,由于需求是把自带的search去掉,并且给输入框加上placeholder="Search",使其看起来更简洁美观,于是乎简单粗暴的把代码改 ...