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用时为 time[a]/2
2 1
6
10
0 1
3 2
1
2
3
0 1
1 2
3 1
2
4
6
0 1
0 0
11
3
10
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.
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int a[],first[],to[];
int u[],v[],vis[];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&(n||m))
{
for(int i=;i<n;i++)
scanf("%d",&a[i]); //读每本书的时间 memset(first,-,sizeof(first));
for(int i=;i<m;i++)
{
scanf("%d%d",&u[i],&v[i]);
to[i]=first[u[i]];
first[u[i]]=i;
}
for(int i=;i<m;i++)
{
u[i+m]=v[i], v[i+m]=u[i];
to[i+m]=first[v[i]];
first[v[i]]=i+m;
} /// 建立邻接表 正反向 int sum=;
memset(vis,,sizeof(vis));
while() /// Prim部分
{
int mini=INF,index=;
for(int i=;i<n;i++)
if(a[i]<mini&&!vis[i])
mini=a[i], index=i; /// 找到用时最短且没读过的书
if(mini==INF) break; /// 当mini没有被交换 说明没有书没读过 sum+=mini; vis[index]=; queue <int> q; q.push(index);
while(!q.empty()) // BFS
{
int k=first[q.front()]; q.pop();
while(k!=-) /// 邻接表遍历
{
if(!vis[v[k]])
{ /// 将与该书similar且未没读过的书也读了 并存入队列
sum+=a[v[k]]/;
vis[v[k]]=; // 标记为读过
q.push(v[k]);
}
k=to[k];
}
} /// 直到队列中这一连串的similar都被读过 则结束
} printf("%d\n",sum);
}
return ;
}
Reading books /// Prim+BFS oj21633的更多相关文章
- GDCPC 2008:B Reading books
Problem B Reading books (Input File: book.in / Standard Output) In the summer vacation, LRJ wants to ...
- POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- POJ 3026 Borg Maze(Prim+bfs求各点间距离)
题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’ ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
- Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...
- 无向图最小生成树(prim算法)
普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小.该算法于1930年由捷 ...
- 每日英语: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 ...
随机推荐
- CSS:CSS 字体
ylbtech-CSS:CSS 字体 1.返回顶部 1. CSS 字体 CSS字体属性定义字体,加粗,大小,文字样式. serif和sans-serif字体之间的区别 在计算机屏幕上,sans-se ...
- 【Java多线程系列二】Thread类的方法
Thread实现Runnable接口并实现了大量实用的方法. /* * 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机会,它自己也有可能再次得到执行机会 */ public s ...
- jQuery 对象与 Dom 对象转化
首先,我们需要知道,为什么我们需要转化两者,原因在于,两者提供的方法不能共用. 比如: $("#id").innerHTML; document.getElementById(id ...
- Netty 相关目录
Netty 相关目录 Netty 源码学习--客户端流程分析 Netty 源码学习--服务端流程分析 Netty 源码分析--ChannelPipeline Netty 源码学习--EventLoop ...
- 【AI图像识别一】人脸识别测试探索
****************************************************************************** 本文主要介绍AI能力平台的人脸识别技术的测 ...
- 20140604 word表格中打钩 循环右移
1.如在在word表格中打钩 符号->其他符号->字体(wingdings2) 2.循环右移 方法1: #include<stdio.h> void move(char *s) ...
- 43-Ubuntu-用户管理-08-chown-chgrp
1.修改文件|目录的拥有者 sudo chown 用户名 文件名|目录名 2.递归修改文件|目录的主组 sudo chgrp -R 组名 文件名|目录名 例1: 桌面目录下有test目录,拥有者为su ...
- ArrayList底层代码解析笔记
通过底层代码可以学习到很多东西: public class ArrayList<E> extends AbstractList<E> implements List<E& ...
- layui 封装自定义模块
转自:https://lianghongbo.cn/blog/430585105a35948c layui是国人开发的一款非常简洁的UI框架,使用了模块化加载方式,因此在使用过程中我们难免需要添加自己 ...
- tcmalloc jemalloc 和ptmalloc 对比
ptmalloc 是glibc的内存分配管理 tcmalloc 是google的内存分配管理模块 jemalloc 是BSD的提供的内存分配管理 三者的性能对比参考从网上的一个图如下: 自己测试了一下 ...