GDCPC 2008:B Reading books
Problem B
Reading books
(Input File: book.in / Standard Output)
In the summer vacation, LRJ wants to improve himself in computer science. So he finds out N books of computer science in the school library. The books are numbered from 0 to N-1.
To finish reading the i-th book, it takes LRJ time[i] minutes. But some books are similar in the content. If the i-th book and the j-th book are similar, then if LRJ has finished reading the i-th book, it will take him only minutes to finish reading the j-th book. Of course if LRJ has finished reading the j-th book, it will take him only minutes to finish reading the i-th book. Now you are asked to tell LRJ the minimal total time to finish reading all the N books.
Input:
The first line contains two integers N (0<=N<=100) and M (0<=M<=N*(N-1)/2). N is the total number of books. M is the number of pairs which are similar.
Then the following N lines describe time[0], time[1], … , time[N-1] (1<=time[i]<=105).
Next comes M lines, each contains two integer (i, j), indicating that the i-th book and the j-th book are similar.
Input is ended by N=0 and M=0.
Output:
For each test case, just output the minimal total time on a single line.
Sample input:
2 1
6
10
0 1
3 2
1
2
3
0 1
1 2
3 1
2
4
6
0 1
0 0
Sample output:
11
3
10
Hints:
For the first test case, if LRJ read the books in the order (0, 1), then the total time = 6+10/2=11; If in the order (1, 0), then the total time =10+ 6/2=13.
用矩阵将全部相似的书都标记起来,然后通过dfs去算和就可以
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int map[105][105],val[105],n,m,sum,minn;
int vis[105],ans; void dfs(int x)
{
vis[x] = 1;
sum+=val[x]/2;
if(minn == -1 || val[x]<minn)
minn = val[x];
for(int i = 0; i<n; i++)
{
if(!vis[i] && map[x][i])
dfs(i);
}
} int main()
{
int i,x,y;
while(~scanf("%d%d",&n,&m),n+m)
{
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
ans = 0;
for(i = 0; i<n; i++)
scanf("%d",&val[i]);
for(i = 0; i<m; i++)
{
scanf("%d%d",&x,&y);
map[x][y] = map[y][x] = 1;
}
for(i = 0; i<n; i++)
{
minn = -1;
if(vis[i])
continue;
sum = 0;
dfs(i);
ans = ans+sum-minn/2+minn;
}
printf("%d\n",ans);
} return 0;
}
GDCPC 2008:B Reading books的更多相关文章
- Reading books /// Prim+BFS oj21633
题目大意: 输入 N,M 接下来1-N行输入读该书的用时time[i] 接下来1-M行输入a,b 表示a和b是similar的 若a读过则读b用时为 time[b]/2 ,若b读过则读a用时为 ti ...
- Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...
- 每日英语:Does China Face a Reading Crisis?
For much of the last year, intellectuals and officials in China -- land of world-beating students an ...
- List the Books
描述 Jim is fond of reading books, and he has so many books that sometimes it's hard for him to manage ...
- zoj 2727 List the Books
List the Books Time Limit: 2 Seconds Memory Limit: 65536 KB Jim is fond of reading books, and h ...
- From 《Soft Skill》——Chapter 69. My personal success book list
There have been many excellent books that have greatly influenced what I believe and how I behave. I ...
- Tips for newbie to read source code
This post is first posted on my WeChat public account: GeekArtT Reading source code is always one bi ...
- Codeforces Round #327 (Div. 2) B. Rebranding C. Median Smoothing
B. Rebranding The name of one small but proud corporation consists of n lowercase English letters. T ...
- Linux SSH登录慢案例分析
手头有台Linux服务器ssh登录时超级慢,需要几十秒.其它服务器均没有这个问题.平时登录操作都默默忍了.今天终于忍不住想搞清楚到底什么原因.搜索了一下发现了很多关于ssh登录慢的资料,于是自己也学着 ...
随机推荐
- **如何让CI框架支持service层
http://www.bitscn.com/pdb/php/201411/404708.html 大家知道CodeIgniter框架式MVC分层的,通常大家把业务逻辑写到Controller中,而Mo ...
- django中的view测试和models测试样例
感觉用model_mommy比factory_boy要好些. 如果Models.py如下: from django.db import models from django.contrib.auth. ...
- [转] map/reduce
如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Processing on Large Clusters”,你就能大概明白map/reduce的概念. ...
- elasticsearch query dsl
1.match / match_phrase / match_phrase_prefix / multi_match[查询] 1.1 match 它会根据所给的字符串,进行分词,然后去找出,包含这些分 ...
- 001.Rsync简介及使用
一 基础知识 1.1 简介 Rsync是Linux系统中的数据镜像备份工具,通过rsync可以将本地系统数据通过网络备份到任何远程主机上.rysnc不仅仅能对不同位置的文件和目录进行同步,还可以差异计 ...
- WIN10下 VS2017+OpenCv 3.4.1 配置
写篇博客来记录一下opencv在VS中的配置. 一.下载OpenCv安装包 下载的途径有三种: 1.官网下载 但是官网下载真的是贼头大,首先下载好好的突然说下载中断,而且无法恢复,此外,还慢,毕竟外网 ...
- BZOJ.4032.[HEOI2015]最短不公共子串(DP 后缀自动机)
题目链接 1.求A的最短子串,它不是B的子串. 子串是连续的,对B建SAM,枚举起点,在SAM上找到第一个无法匹配点即可.O(n)用SAM能做吗..开始想错了. 2.求A的最短子串,它不是B的子序列. ...
- BZOJ.4816.[SDOI2017]数字表格(莫比乌斯反演)
题目链接 总感觉博客园的\(Markdown\)很..\(gouzhi\),可以看这的. 这个好像简单些啊,只要不犯sb错误 [Update] 真的算反演中比较裸的题了... \(Descriptio ...
- 【拓扑排序】BZOJ4010-[HNOI2015]菜肴制作
[题目大意] 是要求N个点的一个拓扑序,且满足以下条件:编号1的位置尽可能靠前,在满足所有限制,编号2的位置尽可能靠前,以此类推. [思路] 一开始觉得优先队列维护一下拓扑就好了.然而样例告诉我们是不 ...
- BZOJ 4610: [Wf2016]Ceiling Functi 水题
4610: [Wf2016]Ceiling Functi 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4610 Description ...