CSU 1116: Kingdoms(状态压缩+DP)

Kingdoms

Time Limit: 3 Sec  Memory Limit: 64 MB
Submit: 410  Solved: 127
[Submit][Status][Web Board]

Description

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

Sample Output

1100
1000

HINT

Source



题意:有m条可修复的路,每个城市有相应的人口数,修复每条路需要指定的话费,问你在话费不超过K,最多能与多少人相通(可以当作能获取多少资源)


解题思路:比赛时,由于到最后一个小时才接到这题,思路很混乱就开始上去写了,比赛时将代码写戳了。言归正传:

    我们可以用二进制状态表示访问的每种情况,比如1010,表示城市二和四被访问了,然后我们只要建立相应的状态转移方程即可。


<span style="font-size:18px;">#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
const int N=17;
int map[N][N];
int value[N];
int dp[1<<N];
int main()
{
    int n,m,l;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&l);
        int x,y,dis;
        memset(map,0x3f,sizeof(map));
        for(int i=1;i<=n;i++) scanf("%d",&value[i]);
        for(int i=0;i<m;i++){
                scanf("%d %d %d",&x,&y,&dis);
                map[y][x]=map[x][y]=min(map[x][y],dis);
        }
        memset(dp,0x3f,sizeof(dp));
        dp[1]=0;
        for(int i=2;i<(1<<n);i++) ///状态
        {
            if(i&1){
                for(int j=1;j<=n;j++){
                    if(i &(1 <<(j-1))){
                        for(int k=1;k<=n;k++){
                            if(i &(1 <<(k-1))){
                                dp[i]=min(dp[i],dp[i &(~(1 <<(k-1)))]+map[k][j]);
                            }
                        }
                    }
                }
            }
        }
        int ans=0;
        for(int i=1;i<(1<<n);i++)
        {
            int sum=0;
            if(dp[i]<=l){
                for(int j=1;j<=n;j++){
                    if(i &(1 <<(j-1)))
                        sum+=value[j];
                }
            }
            ans=max(ans,sum);
        }
        printf("%d\n",ans);
    }
    return 0;
}

</span>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值