首页 > 代码库 > UVA 12507 Kingdoms

UVA 12507 Kingdoms

D - Kingdoms

Time Limit:1000MS     Memory Limit:0KB     64bit 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 to
connect the cities, but unfortunately, the kingdom is running out of money. The total cost of rebuilding
roads should 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 by
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

解题:枚举点,注意题意,最大人口不是指完全直接与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

 

UVA 12507 Kingdoms