湖南省第八届程序设计竞赛F

Problem F. Kingdoms

 

Akingdom has n cities numbered 1 to n, and some bidirectional roads connectingcities. The capital is always city 1.

 

After awar, all the roads of the kingdom are destroyed. The king wants to rebuild someof the roads to connect the cities, but unfortunately, the kingdom is runningout of money. The total cost of rebuilding roads should not exceed K.

 

Giventhe list of m roads that can be rebuilt (other roads are severelydamaged and cannot be rebuilt), the king decided to maximize the total populationin the capital and all other cities that are connected (directly or indirectly)with the capital (we call it "accessible population"), can you helphim?

 

Input

The first line of input contains a single integer T (T<=20), the numberof 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 npositive 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 roadconnecting city u and v, whose rebuildingcost is c. Note that two cities canbe directly connected by more than one road, but a road cannot directlyconnect a city and itself.

 

Output

For each test case, print the maximalaccessible population.

 

Sample Input                           Output for 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

1100

1000

 

代码:

// Rujia Liu
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 16 + 5;
const int maxm = 100 + 5;
const int INF = 1000000000;
int n, m;

int pa[maxn];
int findset(int x) { return pa[x] != x ? pa[x] = findset(pa[x]) : x; } 

struct Edge {
  int u, v, c;
  bool operator < (const Edge& rhs) const {
    return c < rhs.c;
  }
};

Edge e[maxm];

int MST() {
  sort(e, e+m);
  for(int i = 0; i < n; i++) pa[i] = i;
  int cnt = 0;
  int ans = 0;
  for(int i = 0; i < m; i++) {
    int u = e[i].u, v = e[i].v;
    u = findset(u);
    v = findset(v);
    int c = e[i].c;
    if(u != v) {
      pa[u] = v;
      ans += c;
      if(++cnt == n-1) return ans;
    }
  }
  return INF;
}

int in, im, K;
Edge ie[maxm];
int p[maxn];
int id[maxn];

int main() {
  int T;
  scanf("%d", &T);
  while(T--) {
    scanf("%d%d%d", &in, &im, &K);
    int p[maxn];
    Edge ie[maxn*maxn];
    for(int i = 0; i < in; i++) scanf("%d", &p[i]);
    for(int i = 0; i < im; i++) {
      int u, v, c;
      scanf("%d%d%d", &u, &v, &c);
      ie[i] = (Edge){ u-1, v-1, c };
    }

    int ans = p[0];
    int bestn = 1;
    for(int S = 0; S < (1<<(in-1)); S++) {
      n = 1;
      m = 0;
      memset(id, -1, sizeof(id));
      id[0] = 0; // capital
      for(int i = 0; i < in-1; i++)
        if(S & (1<<i)) id[i+1] = n++;
      for(int i = 0; i < im; i++) {
        int u = id[ie[i].u];
        int v = id[ie[i].v];
        if(u >= 0 && v >= 0) e[m++] = (Edge) { u, v, ie[i].c };
      }
      if(MST() <= K) {
        int tot = p[0];
        for(int i = 0; i < in-1; i++)
          if(S & (1<<i)) tot += p[i+1];
        if(tot > ans) { bestn = n; }
        ans = max(ans, tot);        
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值