1801. Reading books

//use the method of dividing the books into several subsets
//color the similar books with the same color unless it has been colored

//then read the book with the least time in each subset first         

#include <iostream>
#include <cstring>
#include <set>
using namespace std;

struct Node{
public:
       int rank;
       int t;
       Node(int i,int j):rank(i),t(j){}
       bool operator <(const Node &node)const{
            t < node.t;
       }
};
set<int> s[100];
int color[100];
void color_set(int k,int c){
     if (color[k]!=-1)
        return;
     color[k] = c;
     for(set<int>::iterator it=s[k].begin(); it!=s[k].end(); it++)
                            color_set(*it,c);
}
int main(){
    int n,m;
    while( cin>>n>>m && n ){
           int t[100];
           memset(color,-1,sizeof(color));
           for(int i=0; i<n; i++){
                   cin>>t[i];
                   s[i].clear();
                   s[i].insert(i);
           }
           for(int i=0; i<m; i++){
                   int a,b;
                   cin>>a>>b;
                   s[a].insert(b);
                   s[b].insert(a);
           }
           for(int i=0; i<n; i++)         
                   color_set(i,i);
                   
           int sum = 0;
           for(int i=0; i<n; i++){
                   set<int> tmp;
                   for(int j=0; j<n; j++)
                           if (color[j]==i)
                              tmp.insert(t[j]);
                   if (tmp.empty())
                      continue;
                   sum += *tmp.begin();
                   tmp.erase(tmp.begin());
                   for(set<int>::iterator it=tmp.begin(); it!=tmp.end(); it++)
                                          sum += (*it)/2;
           }
           cout<<sum<<endl;
                   
    }
    return 0;
}
                            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值