1116: Kingdoms+csuoj+暴力枚举+最小生成树

1116: Kingdoms

Time Limit: 3 Sec   Memory Limit: 64 MB
Submit: 125   Solved: 33
[ 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


解决方案:此题可以先确定1是在点集里,然后暴力枚举其它城市是否要连,对每个枚举的结果求最小生成树,选出符合条件的最优解。
code:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=0x3f3f3f3f;
int n,m,k;
int population[20];
int Map[20][20];
bool exits[20];
bool vis[20];
int d[20];
int p;
void init(int n)
{
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            Map[i][j]=(i!=j?maxn:0);
        }
    }
}
int prim()
{
    int sum=0;
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)
    {
        d[i]=Map[1][i];
    }
    vis[1]=true;
    for(int i=2; i<=n; i++)
    {

        int min=maxn,mini;
        for(int j=1; j<=n; j++)
        {
            if(!vis[j]&&exits[j]&&d[j]<min)
            {
                min=d[j];
                mini=j;
            }
        }
        if(min==maxn) break;
        sum+=min;
        vis[mini]=true;
        for(int k=1; k<=n; k++)
        {
            if(exits[k]&&!vis[k]&&Map[mini][k]<d[k])
            {
                d[k]=Map[mini][k];
            }
        }


    }
    int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++){
        cnt1+=exits[i];
    }
    for(int i=1;i<=n;i++){
        cnt2+=vis[i];
    }
    if(cnt1==cnt2)
    return sum;
    else return maxn;

}

void dfs(int cur)
{
    if(cur>n)
    {
        int pr=prim();
        int temp=0;
        if(pr<=k)
        {
            for(int i=1; i<=n; i++)
            {
                if(exits[i])
                {
                    temp+=population[i];
                }
            }
            if(temp>p) p=temp;
        }
        return ;
    }
    for(int i=0; i<2; i++)
    {
        exits[cur]=i==1?true:false;
        dfs(cur+1);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&population[i]);
        }
        init(n);
        for(int i=0; i<m; i++)
        {
            int from,to,c;
            scanf("%d%d%d",&from,&to,&c);
            if(Map[from][to]>c)
            {
                Map[from][to]=Map[to][from]=c;
            }
        }
        p=population[1];
        exits[1]=true;
        dfs(2);
        printf("%d\n",p);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个任务可以分为以下几部分: 1. 读取文件并提取人名 2. 对人名进行计数 3. 生成词频最高的10-20个词 4. 生成词云 我们可以使用Python中的一些第三方库来实现这些功能。具体实现如下: ```python # 导入所需的库 import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplot as plt # 读取文件 with open('Romance of the Three Kingdoms.txt', 'r', encoding='utf8') as f: text = f.read() # 提取人名 words = jieba.cut(text) names = [] for word in words: if len(word) > 1 and word[0] == '第': # 排除章节名 continue names.append(word) # 计数 name_cnt = Counter(names) name_cnt = dict(name_cnt.most_common()) # 生成词频最高的10-20个词 top_names = list(name_cnt.keys())[:20] # 生成词云 wc = WordCloud(font_path='msyh.ttc', background_color='white', max_words=2000, width=800, height=600) wc.generate_from_frequencies(name_cnt) plt.imshow(wc, interpolation='bilinear') plt.axis('off') plt.show() ``` 解释一下代码: 1. 使用jieba库进行中文分词,提取人名。 2. 对人名进行计数,并使用字典的方式保存结果。 3. 取出前20个最高频的人名,作为词云的输入。 4. 使用WordCloud库生成词云,并使用matplotlib库显示结果。 需要注意的是,这个版本的代码没有去重,所以结果中可能会出现同一个人名出现多次的情况。如果需要更准确的处理方式,可以使用同义词词典进行替换。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值