题目大意:

输入 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. 用 Flask 来写个轻博客 (2) — Hello World!

    Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 实现最简单的 Flask 应用 创建 config.py 文 ...

  2. winform界面设计

    http://www.cnblogs.com/wuhuacong/  这位大师给了我指导方向 http://officeribbon.codeplex.com 提供了ribbon界面的控件 动态web ...

  3. adb devices unauthorized的解决办法

        Hi, trying to launch adb but get: daemon not running. starting it now on port * daemon started s ...

  4. Docker、Kubernetes(k8s)与OpenShift之间的关系

    openshift是基于容器计数搭建的一个云平台.这里的容器技术即包括Docker和Kunbernetes.如下图所示,OPenshift底层以Docker作为容器引擎驱动,以Kubernetes作为 ...

  5. jQuery 加载事件

    1. jquery加载事件实现 ① $(document).ready(function处理); ② $().ready(function处理); ③ $(function处理);  对第一种加载的封 ...

  6. PAT_A1016#Phone Bills

    Source: PAT A1016 Phone Bills (25 分) Description: A long-distance telephone company charges its cust ...

  7. Django框架(十二)—— 补充:inclusion_tag、defer、only、choice、事务、创建多对多的第三张表

    目录 补充:inclusion_tag.defer.only.choice.事务.创建多对多的第三张表 一.inclusion_tag 1.作用 2.使用 二.defer与only 1.定义 2.使用 ...

  8. Apriori-关联规则挖掘算法

    Apriori算法采用的是自底向上的方法,从1-频繁集开始,逐步找出高阶频繁集. 它的基本流程是:第一次扫描交易数据库D时,产生1- 频繁集.在此基础上经过连接.修剪产生2-频繁集.以此类推,直到无法 ...

  9. 使用Swagger2Markup归档swagger生成的API文档

    文章出处: http://blog.didispace.com/swagger2markup-asciidoc/ 说明 项目中使用Swagger之后,我们能够很轻松的管理API文档,并非常简单的模拟接 ...

  10. ForEach控制器

    ForEach一般是和用户定义的变量结合使用的 前面前缀一般是和用户定义的变量前缀一致 输出的变量是要使用的变量 这样能保证每次使用的变量不一样 add"_"before numb ...