cf437C The Child and Toy
1 second
256 megabytes
standard input
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.
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 ≤ n; xi ≠ yi).
Consider all the parts are numbered from 1 to n.
Output the minimum total energy the child should spend to remove all n parts of the toy.
4 3
10 20 30 40
1 4
1 2
2 3
40
4 4
100 100 100 100
1 2
2 3
2 4
3 4
400
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
160
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的更多相关文章
- 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/ ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- 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 ...
- 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 ...
- 【CF437C】The Child and Toy
题目大意:给定一个有 N 个点,M 条边的无向图,点有点权,删除一个点就要付出所有与之有联系且没有被删除的点的点权之和的代价,求将所有点删除的最小代价是多少. 题解:从图连通性的角度出发,删除所有点就 ...
- codeforces 437C The Child and Toy
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 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直接相连(且还未被移除)的部 ...
- Codeforces #250 (Div. 2) C.The Child and Toy
之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小 ...
- The Child and Toy
Codeforces Round #250 (Div. 2) C:http://codeforces.com/problemset/problem/437/C 题意:给以一个无向图,每个点都有一点的权 ...
随机推荐
- JVM内存堆布局图解分析
JAVA能够实现跨平台的一个根本原因,是定义了class文件的格式标准,凡是实现该标准的JVM都能够加载并解释该class文件,据此也可以知道,为啥Java语言的执行速度比C/C++语言执行的速度要慢 ...
- java笔记3之赋值运算符
赋值运算符: 基本的赋值运算符:= 把=右边的数据赋值给左边. 扩展的赋值运算符:+=,-=,*=,/=,%= += 把左边 ...
- INSERT command denied to user
问题分析: 网站运行突然出现下面的 MySQL 错误: 645297 [http-88-13] ERROR com.mes.cart.dao.impl.CartDAOImpl - CartDAOImp ...
- 加上固件密码,Mac更安全
在Mac OS X中,管理员密码非常重要,在修改系统参数或安装软件时都要求输入密码,这避免了绝大部分的破坏性误操作和恶意程序(尽管Mac上恶意程序本来就少的可怜). 但是对Mac OS X比较熟悉的人 ...
- [React] React Router: Redirect
The Redirect component in react-router does exactly what it sounds like. It allows us to redirect fr ...
- [Angular 2] Refactoring mutations to enforce immutable data in Angular 2
When a Todo property updates, you still must create a new Array of Todos and assign a new reference. ...
- Java基础知识强化39:StringBuffer类之StringBuffer的删除功能
1. StringBuffer的删除功能: public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回字符串缓冲区本身. public Str ...
- 使用SQL语句创建和删除约束
原文:http://blog.csdn.net/hamber_bao/article/details/6504905 约束的目的就是确保表中的数据的完整性. 常用的约束类型如下: 主键约束:(Prim ...
- 解决手机浏览器和微信中select中border:none;无用和去掉小三角
设置select的边框为none:在PC端是没有问题的,但是放到手机浏览器和微信里上还是会出现边框 解决办法: appearance:none; -moz-appearance:none; /* Fi ...
- Quartz实现定时任务的配置方法
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...