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 ...
随机推荐
- 使用redis实现客户端和服务端token验证
实在是思维江化啊,没有想到可以给redis设置不同的key值来实现不同key值存储不同的value值,而一直想着给一个名为token的key值新增不同的数据,并设置过期时间,然而这样却不能新增只能做到 ...
- idea无法引用jar包中的class
最近由eclipse换idea的过程中,出现了一个很奇妙的问题! 项目是maven+git+idea管理的,idea某次在使用的过程中,电脑死机重启后,发现无法引用jar包中的class.包括jdk中 ...
- AndroidFine Error:Annotation processors must be explicitly declared now.
环境 Android Studio 3.0 Gradle 3.0.0 gradle 4.1 Error Error:Execution failed for task ':app:javaPreCom ...
- 2019 ACM-ICPC 上海网络赛 B. Light bulbs (差分)
题目链接:Light bulbs 比赛链接:The Preliminary Contest for ICPC Asia Shanghai 2019 题意 给定 \(N\) 个灯泡 (编号从 \(0\) ...
- Java目录事件
当文件系统中的对象被修改时,我们可以监听watch服务以获取警报.java.nio.file包中的以下类和接口提供watch服务. Watchable接口 WatchService接口 WatchKe ...
- redis 部署方式及常见特性
单机部署 redis的单机部署 如何保证redis的高并发和高可用? redis的主从复制原理?redis的哨兵原理? redis单机能承载多高并发?如果单机扛不住如何扩容扛更多的并发? redis会 ...
- Struts2入门示例(Myeclipse)
1.新建Web项目在lib导入struts-2.3.37核心基础jar包 2.在WebRoot新建2个JSP demo1.jsp <%@ page language="java&quo ...
- C# WinForm 访问webService天气预报
1.直接添加服务引用 右键引用 -->添加服务引用 这样的好处是可以直接当成类用 简单 麻烦的地方就是地址是死的 代码如下: private void button1_Click(object ...
- 小程序onclick的写法?
bindtap="bindAction" 调用bindAction函数即可
- 代码控制PrivateBinPath和ConfigurationFile的位置
原文:代码控制PrivateBinPath和ConfigurationFile的位置 .Net的WinForm程序有的时候让人很烦的是,在执行目录下总是一大堆的DLL,配置文件,最少则是个以下,多的时 ...