博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1801. Reading books
阅读量:7186 次
发布时间:2019-06-29

本文共 1717 字,大约阅读时间需要 5 分钟。

  1 
/*
学习了并查集
  2 
 *先找出所有的不关联集合,找出其中最小的一个,然后先读最小的那本书
  3 
 *图片不能显示,具体的计算方法是读的第一本书用全时,跟他关联的书用时间的一半
  4 
 *
  5 
 *
  6 
 
*/
  7 
#include 
<iostream>
  8 
#include 
<memory.h>
  9 
#include 
<algorithm>
 10 
using 
namespace std;
 11 
int B[
110];
//父节点
 12 
int V[
110];
//时间
 13 
int rank[
110];
//帙
 14 
int rd[
110][
110];
//关联的书
 15 
int n;
//书的种类
 16 
int set[
110][
110];
 17 
int len[
110];
//关联集合的长度,保存在祖先里
 18 
void make()
 19 {
 20     
for(
int i=
0;i<n;i++)
 21     {
 22         B[i]=i;
 23     }
 24 }
 25 
int find(
int x)
 26 {
 27     
if(B[x]!=x)
 28         B[x]=find(B[x]);
 29     
return B[x];
 30 }
 31 
void un(
int x,
int y)
 32 {
 33     x=find(x);
 34     y=find(y);
 35     
if(x==y)
 36         
return;
 37     
if(rank[x]>rank[y])
 38     {
 39         B[y]=x;
 40     }
 41     
else
 42     {
 43         
if(rank[x]==rank[y])
 44             rank[y]++;
 45         B[x]=y;
 46     }
 47 }
 48 
int cmp(
const 
void* a,
const 
void* b)
 49 {
 50     
return *(
int*)a>*(
int*)b;
 51 }
 52 
int main()
 53 {
 54     
int e;
 55     cin>>n>>e;
 56     
while(n!=
0&&e!=
0)
 57     {
 58         memset(rd,
0,
sizeof(rd));
 59         memset(V,
0,
sizeof(V));
 60         memset(rank,
0,
sizeof(rank));
 61         memset(len,
0,
sizeof(len));
 62         memset(set,
0,
sizeof(set));
 63         memset(B,
0,
sizeof(B));
 64         
for(
int i=
0;i<n;i++)
 65             cin>>V[i];
 66         
while(e--)
 67         {
 68             
int a,b;
 69             cin>>a>>b;
 70             rd[a][b]=
1;
 71         }
 72         make();
 73         
for(
int i=
0;i<n;i++)
 74             
for(
int j=
0;j<n;j++)
 75             {
 76                 
if(rd[i][j]==
1)
 77                     un(i,j);
 78             }
 79         
int l=
0;
 80         
int ans=
0;
 81         
for(
int i=
0;i<n;i++)
 82         {
 83             
int tem=find(i);
 84             set[tem][len[tem]++]=V[i];
 85         }
 86         
for(
int i=
0;i<n;i++)
 87         {
 88             
if(len[i]!=
0)
 89             {
 90                 qsort(set[i],len[i],
sizeof(set[i][
0]),cmp);
 91 
 92                 
for(
int j=
0;j<len[i];j++)
 93                 {
 94                     
if(j==
0)
 95                         ans+=set[i][j];
 96                     
else
 97                         ans+=set[i][j]/
2;
 98                 }
 99             }
100         }
101         cout<<ans<<endl;
102         cin>>n>>e;
103     }
104 }
105 

转载于:https://www.cnblogs.com/congzc/archive/2011/05/16/2329964.html

你可能感兴趣的文章
OCP 11g认证052考试最新题库(带答案)-带38题
查看>>
模拟误删除InnoDB ibdata数据文件恢复
查看>>
9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路(转)...
查看>>
asp.net core 2.2 根据PC端和移动端自动显示不同视图而不改变url地址
查看>>
LeetCode 341: Flatten Nested List Iterator
查看>>
easyui tabs页签显示在底部属性
查看>>
IIS7设置IP地址和域名限制
查看>>
Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1问题总结及解决方案...
查看>>
iOS,贝塞尔曲线(UIBezierPath)
查看>>
二维码生成类
查看>>
css 兼容性写法,CSS hack写法
查看>>
javascript新闻向上停顿1秒后继续滚动
查看>>
关于 IntelliJ IDEA 的Maven 版本修改
查看>>
1.OpenGLES——FBO方式的离屏渲染
查看>>
FIFO基础知识(转)
查看>>
js中页面加载完成后执行的几种方式及执行顺序
查看>>
H5 以及data-*应用和each用法
查看>>
web学习-XML基础
查看>>
Oracle SQL Lesson (5) - 使用组函数输出聚合数据
查看>>
【机器学习篇】--SVD从初始到应用
查看>>