D-kingdoms
Time limit:1000 msMemory limit:0 KB64bit Io format:% LLD & % LlU
A kingdom has n cities numbered 1 to n, and some bidirectional roads connecting cities. The capital is
Always City 1.
After a war, all the roads of the Kingdom are destroyed. The King wants to rebuild some of the roads
Connect the cities, but unfortunately, the Kingdom is running out of money. The total cost of rebuilding
Roads shoshould not exceed K.
Given the list of M roads that can be rebuilt (Other roads are severely damaged and cannot be rebuilt ),
The king decided to maximize the total population in the capital and all other cities that are connected
(Directly or indirectly) with the capital (we call it "accessible population"), can you help him?
Input
The first line of input contains a single integer T (t <= 20), the number of test cases. Each test case
Begins with three integers n (4 <= n <= 16), m (1 <= m <= 100) and K (1 <= k <= 100,000). The second line
Contains n positive integers Pi (1 <= pI <= 10,000), the population of each city. Each of the following m
Lines contains three positive integers U, V, C (1 <= u, v <= N, 1 <= C <= 1000), representing a destroyed Road
Connecting City U and V, whose rebuilding cost is C. Note that two cities can be directly connected
More than one road, but a road cannot directly connect a city and itself.
Output
For each test case, print the maximal accessible population.
Sample Input
2
4 6 6
500 400 300 200
1 2 4
1 3 3
1 4 2
4 3 5
2 4 6
3 2 7
4 6 5
500 400 300 200
1 2 4
1 3 3
1 4 2
4 3 5
2 4 6
3 2 7
Output for sample input
1100
1000
Problem solving: enumeration points. Pay attention to the question. The largest population is not the population of a point directly connected to 1.
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 20;18 int g[maxn][maxn],d[maxn],p[maxn];19 int n,m,k,cost,pu;20 void prim(int b) {21 int i,vis[maxn] = {0};22 for(i = 0; i <= n; ++i) d[i] = INF;23 cost = pu = i = 0;24 while(b) {25 vis[i+1] = b&1;26 b >>= 1;27 ++i;28 }29 vis[1] = 1;30 d[1] = 0;31 while(true) {32 int minv = INF,index = -1;33 for(int i = 1; i <= n; ++i)34 if(vis[i] == 1 && d[i] < minv) minv = d[index = i];35 if(index == -1 || minv == INF) break;36 cost += minv;37 vis[index] = 2;38 for(int i = 1; i <= n; ++i){39 if(vis[i] == 1 && d[i] > g[index][i]){40 d[i] = g[index][i];41 }42 }43 }44 for(int i = 1; i <= n; ++i) if(vis[i] == 2) pu += p[i];45 }46 int main() {47 int t,u,v,w;48 scanf("%d",&t);49 while(t--){50 scanf("%d %d %d",&n,&m,&k);51 for(int i = 1; i <= n; ++i)52 scanf("%d",p+i);53 for(int i = 0; i <= n; ++i)54 for(int j = 0; j <= n; ++j)55 g[i][j] = INF;56 for(int i = 0; i < m; ++i){57 scanf("%d %d %d",&u,&v,&w);58 if(w < g[u][v]) g[u][v] = g[v][u] = w;59 }60 int maxp = 0;61 for(int i = 1; i < (1<<n); ++i){62 prim(i);63 if(cost <= k) maxp = max(maxp,pu);64 }65 printf("%d\n",maxp);66 }67 return 0;68 }
View code
Sha 12507 kingdoms