1801. Reading books

来源:互联网 发布:南充广电网络收费标准 编辑:程序博客网 时间:2024/06/10 19:37

//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;}                            
原创粉丝点击