您的位置:首页 > 其它

SZU:B36 Reading books

2014-02-15 15:33 120 查看

Description

In the summer vacation, LRJ wants to improve himself in computer science. So he finds out N books of computer science in the school library. The books are numbered from 0 to N-1.

To finish reading the i-th book, it takes LRJ time[i] minutes. But some books are similar in the content. If the i-th book and the j-th book are similar, then if LRJ has finished reading the i-th book, it will take him only 

 minutes to finish reading the j-th book. Of course if LRJ has finished reading the j-th book, it will take him only 

 minutes to finish reading the i-th book. Now you are asked to tell LRJ the minimal total time to finish reading all the N books.

Input

The first line contains two integers 

 and 

. N is the total number of books. M is the number of pairs which are similar.

Then the following N lines describe 

.

Next comes M lines, each contains two integer (i,j), indicating that the i-th book and the j-th book are similar.

Input is ended with EOF.

Output

For each test case, just output the minimal total time on a single line.

Sample Input

2 1
6
10
0 1
3 2
1
2
3
0 1
1 2
3 1
2
4
6
0 1


Sample Output

11
3
10

 Hint: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.

 Ps  :这道题我看错题意了,导致一直无法AC,而且被无法言喻的提示误导了。原来这个(0,1)顺序是自动找出i,j 书最少时间的,思想错了,导致做了一下午无法解决
     其实少了dd这个老师,挺难过的说。

1 #include<stdio.h>
2 int num[120];
3 int root(int n)
4 {
5     if(n!=num
)
6         n=root(num
);
7     return n;
8 }
9 int main()
10 {
11        int m,n,i,sum,a,b,time[120];
12     while(scanf("%d%d",&m,&n)!=EOF){
13         for(i=0;i<m;i++){
14             scanf("%d",&time[i]);
15             num[i]=i;
16         }
17         for(i=0;i<n;i++){
18             scanf("%d%d",&a,&b);
19             a=root(a);
20             b=root(b);
21             if(time[a]<time[b])
22                 num[b]=a;
23             else num[a]=b;
24         }
25         sum=0;
26         for(i=0;i<m;i++){
27             if(num[i]==i)
28                 sum+=time[i];
29             else sum+=time[i]/2;
30         }
31         printf("%d\n",sum);
32     }
33     return 0;
34 }


 

 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: