1736. Electric Power Grid
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Grid Computing is a hot topic these years. Many people have heard of it, but some are still confused about it. In fact, this concept came from the electric power grid. Within an electric power grid, power stations and power users are all connected to the grids. Power stations generate the electricity and input it into the grid. On the other hand, the users get electricity from the grid, unaware where it comes from. Grid Computing works in the similar way.
Electric power grids worked very well in the past one hundred years. Recently, a plan about building a new electric power grid in our province was being discussed by GDCPC (Guangdong Construction Power Committee). This grid will cover all the cities and supply the maximum electric power. The electric network has been constructed. The committee members are now discussing where the power stations should be located. This grid has the following features:
1.All cities are within the network.
2.There is at most one power station built within a city. Of course, it locates far away from the residential area.
3.There is at most one connection between two cities.
4.There may be some circles in the network, none of which contains more than three distinct cities. For example, the network in Picture 1 is allowed while the one in Picture 2 is not.
5.Taking safety into consideration, if two cities are directly connected, they cannot both have power stations.
6.Due to the environment condition of the cities, the output of each power station cannot exceed a limitation value. These values differ from city to city.
In order to provide the maximum electric power, the location of the power stations should be carefully selected. Now, it is your task to help the committee decide the cities in which power stations will be built.
Input
There are multiple cases.
The input format for each case is: The first line contains only one integer N (1 <= N <= 100) indicating the number of the city. There are N positive integers (not greater than 1000) in the second line. The ith integer is the limitation value of the ith city. And then there’s a number in a line indicating the number of edges, i. e. , the number of pairs of linked cities. The following lines will describe the structure of the network. Each line contains two integers k and m, indicating that the kth city and the mth city is directly connected. N = 0 indicates the end of input.
Output
For each case output one line containing one integer. It is the maximum electric power that the grid will provide.
Sample Input
2 1 2 1 1 2 5 1 2 3 4 5 6 1 2 2 3 1 3 3 4 3 5 4 5 0
Sample Output
27
// Problem#: 1736 // Submission#: 3584390 // 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 <stdio.h> #include <string.h> int n; int c[101]; int tag[101]; int father[101]; int g[101][101]; int tree[101][101]; int f0[101], f1[101]; int solve1(int); int solve0(int x) { if (f0[x] >= 0) return f0[x]; int i, res = 0, temp1, temp2; for (i = 1; i <= n; i++) if (tree[x][i] == 1) { temp1 = solve0(i); temp2 = solve1(i); if (temp1 > temp2) res += temp1; else res += temp2; } f0[x] = res; return f0[x]; } int solve1(int x) { if (f1[x] >= 0) return f1[x]; int i, j, mark[101], temp1, temp2, res = 0; for (i = 0; i <= n; i++) mark[i] = 1; for (i = 1; i <= n; i++) { if (tree[x][i] == 1) { mark[i] = 0; for (j = 1; j <= n; j++) if (tree[i][j] == 1 && g[x][j] == 1) { mark[j] = 0; } } } for (i = 1; i <= n; i++) if (mark[i] == 1 && father[i] > 0 && mark[father[i]] == 0) { temp1 = solve0(i); temp2 = solve1(i); if (temp1 > temp2) res += temp1; else res += temp2; } f1[x] = c[x] + res; return f1[x]; } void maketree(int x) { tag[x] = 1; for (int i = 1; i <= n; i++) if (tag[i] == 0 && g[x][i] == 1) { tree[x][i] = 1; father[i] = x; maketree(i); } } int main() { int i, j, k, m, ans; while (1) { scanf("%d", &n); if (!n) break; for (i = 1; i <= n; i++) scanf("%d", &c[i]); memset(g, 0, sizeof(g)); memset(tree, 0, sizeof(tree)); scanf("%d", &m); for (i = 1; i <= m; i++) { scanf("%d%d", &j, &k); g[j][k] = g[k][j] = 1; } memset(tag, 0, sizeof(tag)); ans = 0; for (i = 1; i <= n; i++) f0[i] = f1[i] =father[i] = -1; for (i = 1; i <= n; i++) if (tag[i] == 0) { maketree(i); father[i] = 0; j = solve0(i); k = solve1(i); if (j > k) ans += j; else ans += k; } printf("%d\n", ans); } return 0; }