1801. Reading books
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
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 ith book, it takes LRJ time[i] minutes. But some books are similar in the content. If the ith book and the jth book are similar, then if LRJ has finished reading the ith book, it will take him only minutes to finish reading thejth book. Of course if LRJ has finished reading the jth book, it will take him only
minutes to finish reading the ith 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 N (0 ≤ N ≤ 100) and M (0 ≤ M ≤ N (N - 1) / 2).N is the total number of books. M is the number of pairs which are similar.
Then the following N lines describe time[0], time[1], … , time[N-1] (1 ≤ time[i] ≤ 105).
Next comes M lines, each contains two integer (i, j), indicating that the ith book and the jth book are similar.
N=0 and M=0.
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 0 0
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.
// Problem#: 1801
// Submission#: 3327739
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;
const int MAX_N = 105;
int N, M;
int cost[MAX_N];
bool vis[MAX_N];
vector<int> similar[MAX_N];
vector<int> connect;
int ans;
void dfs(int now) {
vis[now] = true;
connect.push_back(now);
int s = similar[now].size();
for (int i = 0; i < s; i++) {
if (!vis[similar[now][i]]) {
dfs(similar[now][i]);
}
}
}
void cal() {
int sum = 0, s = connect.size(), Min = 99999999, MinP = -1;
for (int i = 0; i < s; i++) {
if (Min > cost[connect[i]]) {
Min = cost[connect[i]];
MinP = i;
}
}
for (int i = 0; i < s; i++) if (i != MinP) sum += cost[connect[i]] / 2;
ans += Min + sum;
}
int main() {
std::ios::sync_with_stdio(false);
while (1) {
cin >> N >> M;
if (N == 0 && M == 0) break;
ans = 0;
for (int i = 0; i < N; i++) cin >> cost[i];
memset(vis, false, sizeof(vis));
for (int i = 0; i < N; i++) similar[i].clear();
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
similar[a].push_back(b);
similar[b].push_back(a);
}
for (int i = 0; i < N; i++) {
if (!vis[i]) {
connect.clear();
dfs(i);
cal();
}
}
cout << ans << endl;
}
//getchar();
//getchar();
return 0;
}