题目大意:

输入 N,M

接下来1-N行输入读该书的用时time[i]

接下来1-M行输入a,b  表示a和b是similar的

若a读过则读b用时为 time[b]/2 ,若b读过则读a用时为 time[a]/2

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

Hint

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.

 
思路:
把每本书当做图的一个点 similar则表示两点之间存在无向路径 
Prim找出最短用时的没读过的书 sum+读该书用时 标为已读
将更新距离dis[]的部分换为BFS 搜索与该书similar的其他没读过的书 直到所有书都读过
 
#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的更多相关文章

  1. GDCPC 2008:B Reading books

    Problem B Reading books (Input File: book.in / Standard Output) In the summer vacation, LRJ wants to ...

  2. POJ 3026 Borg Maze(Prim+BFS建邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  3. POJ 3026 Borg Maze(Prim+bfs求各点间距离)

    题目链接:http://poj.org/problem?id=3026 题目大意:在一个y行 x列的迷宫中,有可行走的通路空格’  ‘,不可行走的墙’#’,还有两种英文字母A和S,现在从S出发,要求用 ...

  4. POJ3026 Borg Maze(Prim)(BFS)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 Descri ...

  5. Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)

    题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...

  6. 无向图最小生成树(prim算法)

    普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点,且其所有边的权值之和亦为最小.该算法于1930年由捷 ...

  7. 每日英语:Does China Face a Reading Crisis?

    For much of the last year, intellectuals and officials in China -- land of world-beating students an ...

  8. List the Books

    描述 Jim is fond of reading books, and he has so many books that sometimes it's hard for him to manage ...

  9. zoj 2727 List the Books

    List the Books Time Limit: 2 Seconds      Memory Limit: 65536 KB Jim is fond of reading books, and h ...

随机推荐

  1. angularjs 中实现 load more 功能

    在UI 我们经常需要render 一些集合, 如果集合数据过多,那我们可能会采取分页的解决方案,本文是另外一种解决方法,就是当集合数量大于一定数量的时候显示一个 加载更多按钮,点击后,自动加载指定数量 ...

  2. windows server2012r2 安装NET Framework 3.5

    在Windows Server 2012上安装一些软件,比如Oracle 11g等,经常会出现下面这样的错误:“无法安装一下功能:.NET Framework 3.5(包括.NET 2.0和3.0)” ...

  3. vscode gp 安装第三方包

    由于code.google.com被墙,导致一些托管在code.google.com上面的包go get不下来,此功能就是用于解决这个问题. http://www.golangtc.com/downl ...

  4. npm install 超时 国内 切换源; npm ERR! code ELIFECYCLE;

    install 超时 查看npm源地址 npm config get registry #http://registry.npmjs.org 为国外镜像地址 设置阿里云镜像 npm config se ...

  5. ubuntu ceph集群安装以及简单使用

    ubuntu ceph安装以及使用 1.安装环境 本文主要根据官方文档使用ubuntu14.04安装ceph集群,并且简单熟悉其基本操作.整个集群包括一个admin节点(admin node,主机名为 ...

  6. Navicat创建事件,定时更新数据库

    一.新建事件 二.在定义里编写要更改的SQL语句 如果SQL语句有多条,需要将SQL语句放在begin...end中 begin update student set num = '0'; updat ...

  7. docker容器的常见操作

    进入容器 docker exec -it 12a022ee8127 /bin/bash 交互模式进入容器 docker exec -it 12a022ee8127 ip a 查看容器的ip等信息 批量 ...

  8. JDK8新特性之函数式接口

    什么是函数式接口 先来看看传统的创建线程是怎么写的 Thread t1 = new Thread(new Runnable() { @Override public void run() { Syst ...

  9. js中的数据类型隐式转换的三种情况

    js的数据类型隐式转换主要分为三种情况: 1. 转换为boolean类型 2. 转换为number类型 3. 转换为string类型 转换为boolean类型 数据在 逻辑判断 和 逻辑运算 之中会隐 ...

  10. 10_PAE_非PAE

    前置知识: 在 windows 中 保护模式 有两种模式: 段保护 和 页保护 段保护 主要体现在 段选择子上:但是数据段.代码段.栈段等采用的都是4GB平坦模式,段的特征并没有那样展现.所以具体的保 ...