您的位置:首页 > 其它

1801. Reading books

2013-01-08 23:26 615 查看
//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;
}
                           
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Sicily 集合划分