C. The Child and Toy
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.

The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as vi. The child spend vf1 + vf2 + ... + vfk energy for removing part i where f1, f2, ..., fk are the parts that are directly connected to the i-th and haven't been removed.

Help the child to find out, what is the minimum total energy he should spend to remove all n parts.

Input

The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v1, v2, ..., vn (0 ≤ vi ≤ 105). Then followed m lines, each line contains two integers xi and yi, representing a rope from part xi to part yi (1 ≤ xi, yi ≤ nxi ≠ yi).

Consider all the parts are numbered from 1 to n.

Output

Output the minimum total energy the child should spend to remove all n parts of the toy.

Sample test(s)
input
4 3
10 20 30 40
1 4
1 2
2 3
output
40
input
4 4
100 100 100 100
1 2
2 3
2 4
3 4
output
400
input
7 10
40 10 20 10 20 80 40
1 5
4 7
4 5
5 2
5 7
6 4
1 6
1 3
4 3
1 4
output
160
Note

One of the optimal sequence of actions in the first sample is:

  • First, remove part 3, cost of the action is 20.
  • Then, remove part 2, cost of the action is 10.
  • Next, remove part 4, cost of the action is 10.
  • At last, remove part 1, cost of the action is 0.

So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum.

In the second sample, the child will spend 400 no matter in what order he will remove the parts.

打cf开黑就是爽……风骚的结构体……

就是每次找个最大的搞掉

#include <vector>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <bitset>
#include <map>
#include <ctime>
#include <cassert>
#include <set> using namespace std;
typedef pair<int, int> pi;
typedef long long ll; const int N = 1005; int cost[N];
int A[N];
bool done[N]; vector< int > adj[N]; int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, a, b;
cin >> n >> m; for(int i = 1; i <= n; i++){
cin >> A[i];
} for(int i = 0; i < m; i++){
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
set< pi, greater< pi > > S;
for(int i = 1; i <= n; i++){
int& cur = cost[i];
for(int j = 0; j < adj[i].size(); j++){
int to = adj[i][j];
cur += A[to];
}
S.insert(pi(A[i], i));
}
set< pi, greater< pi > >::iterator it;
ll tot = 0;
while(!S.empty()){
pi cur = *S.begin();
done[cur.second] = true;
tot += cost[cur.second];
S.erase(S.begin());
for(int i = 0; i < adj[cur.second].size(); i++){
int to = adj[cur.second][i];
if(done[to]) continue;
it = S.find(pi(A[to], to));
assert(it != S.end());
S.erase(it);
cost[to] -= A[cur.second];
S.insert(pi(A[to], to));
}
} cout << tot << endl; return 0;
}

  

其实最简单的代码是这样的:

我当时就跪下了……

cf437C The Child and Toy的更多相关文章

  1. Codeforces Round #250 (Div. 1) A. The Child and Toy 水题

    A. The Child and Toy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  2. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  3. Codeforces Round #250 Div. 2(C.The Child and Toy)

    题目例如以下: C. The Child and Toy time limit per test 1 second memory limit per test 256 megabytes input ...

  4. Codeforces The Child and Toy

    The Child and Toy time limit per test1 second On Children's Day, the child got a toy from Delayyy as ...

  5. 【CF437C】The Child and Toy

    题目大意:给定一个有 N 个点,M 条边的无向图,点有点权,删除一个点就要付出所有与之有联系且没有被删除的点的点权之和的代价,求将所有点删除的最小代价是多少. 题解:从图连通性的角度出发,删除所有点就 ...

  6. codeforces 437C The Child and Toy

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

  7. Codeforces Round #250 (Div. 2) C、The Child and Toy

    注意此题,每一个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],其中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部 ...

  8. Codeforces #250 (Div. 2) C.The Child and Toy

    之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小 ...

  9. The Child and Toy

    Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C 题意:给以一个无向图,每个点都有一点的权 ...

随机推荐

  1. 关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌!

    关于我们 HerlMax(赫马克斯),奢侈品顶级服装高级定制品牌! HerlMax品牌拥有着纯正悠远的意大利高级定制文化,带着对神秘东方的向往,荣耀登入中国市场.

  2. Java枚举7常见种用法

    DK1.5引入了新的类型——枚举.在Java中它虽然算个“小”功能,却给我的开发带来了“大”方便. 方法/步骤 用法一:常量 在JDK1.5之前,我们定义常量都是:publicstaticfianl. ...

  3. 从此走上一条iOS程序猿不归路。。。

    新的城市,新的生活!前不久刚刚结束了苦逼的面试找工作之旅,期间也小有收货,如今正处年底工作闲暇之余,将前一阵子陆陆续续的总结整理了一下,本人菜鸟程序猿一只,水平有限,本文总结的知识不算深入,比较浅显, ...

  4. memcached学习——常用命令+基于java客户端的3种简单实现(二)

    常用命令: memcached设计的原则就是简单,所以支持的命令也不是特别多~ 1.查看memcached的状态,主要用于分析内存的使用状况.优化内存分配等 stats 查看memcached的运行状 ...

  5. i&1、负数二进制

    if(i&1==1) 表示 如果是 奇数 则...i&1 -- 按位与运算,取 2进制整数 i 的最低位,如果最低位是1 则得1,如果最低位是0 则得0. 奇数 i 的最低位 是1,偶 ...

  6. 魔方公式xyz

    x:(整个魔方以R的方向转动),x':(整个魔方以R'的方向转动)  y:(整个魔方以U的方向转动),y':(整个魔方以U'的方向转动)  z:(整个魔方以F的方向转动),z':(整个魔方以F'的方向 ...

  7. TO DO NOW——送给奋斗着的程序“猿”们

    大家在我们的日常生活中是不是经常会遇到学习和工作效率低,不能够按照自己的计划有条不紊地按时.按点儿的完成自己的任务呢?是不是还在为此而头疼不堪呢?好吧, 那是你执行力有问题.那么究竟什么是执行力?怎样 ...

  8. Android原理揭秘系列之一动态墙纸

    Livewallpaper,即动态墙纸,是Android的一大3D特色功能,用户可以在桌面选择加载动态墙纸,让自己的手机桌面背景旋动起来. 相对于静态桌面壁纸,动态墙纸可以展示各种动态变化的背景,而与 ...

  9. qemu-img 快照的一些总结

    qemu-img 快照的一些总结 http://www.openext.org/2014/06/qemu-img-snapshot-re http://blog.csdn.net/muge0913/a ...

  10. wed网页开发面试笔试必备小知识

    HTML中行内元素与块级元素的区别: 在标准文档流里面,块级元素具有以下特点: ①总是在新行上开始,占据一整行: ②高度,行高以及外边距和内边距都可控制: ③宽带始终是与浏览器宽度一样,与内容无关: ...