HDU - 2682 

Tree

description:

There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and B whose value of happiness are VA and VB,if VA is a prime number,or VB is a prime number or (VA+VB) is a prime number,then they can be connected.What’s more,the cost to connecte two cities is Min(Min(VA , VB),|VA-VB|).
Now we want to connecte all the cities together,and make the cost minimal.

Input

The first will contain a integer t,followed by t cases.
Each case begin with a integer N,then N integer Vi(0<=Vi<=1000000).

Output

If the all cities can be connected together,output the minimal cost,otherwise output “-1”;

Sample Input

2
5
1
2
3
4
5

4
4
4
4
4

Sample Output

4
-1

题目大意:
所有的城市有一个快乐值Vi,城市A和B认为是连接的,如果Va或者Vb或者Va+Vb为素数。同时这个权值为,Min(Min(VA , VB),|VA-VB|)。现在求这些城市连接最小代价。(求这个图的最小生成树)。

解题思路:
1.最小生成树问题,这里由于要先判断城市间有没有连接以及连接的权值,所以后面采用Kruskal算法更合适一些。
2.在求边的权值的时候,由于要多次判断素数。因此可以采用素数打表的方式,预先求出0~2000001范围内的全部素数。这样会大大加快程序整体速度!


源代码:

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string.h>

using namespace std;

int n,e;
int fa[605];
int hp[605];
bool prime[2000010];
struct edg{
    int start,end;
    int cost;
    bool operator < (const edg& a) const{
        return cost<a.cost;
    }
}; 
edg roads[360000];

int find(int x){
    int r=x;
    while(r!=fa[r])
        r=fa[r];
    int i=x,j;
    while(fa[i]!=r){
        j=fa[i];
        fa[i]=r;
        i=j;
    }
    return r;
}  

int kruskal(){
    if(e<n-1){
        return -1;
    }
    for(int i=0;i<n;i++)
      fa[i] = i;
    sort(roads,roads+e);
    int now_choose=0,total_cost = 0;
    for(int i=0;i<e;i++){
        int fs = find(roads[i].start);
        int fe = find(roads[i].end);        
        if(fs!=fe){
            fa[fs] = fe;
            total_cost+=roads[i].cost;
            now_choose++;
        }       
        if(now_choose==n-1)
          break;
    }
    if(now_choose<n-1)
      return -1;
    return total_cost;  
}

void Get_prime(){  
    int i,j;  
    memset(prime,true,sizeof(prime));  
    prime[0]=prime[1]=false;  
    for(i=2;i<2000010;i++)  
      if(prime[i])  
        for(j=i<<1;j<2000010;j+=i)  
          prime[j]=false;  
}  

void init(){
    e = 0;
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(prime[hp[i]] || prime[hp[j]] || prime[hp[i]+hp[j]] ){
                roads[e].start = i;
                roads[e].end = j;
                roads[e].cost = min( min(hp[i],hp[j]),  abs(hp[i]-hp[j])  );
                e++;
                //cout<<i<<"   "<<j<<"   "<<roads[e].cost<<endl;
            }
        }
    }
}

int main(){
    int times;
    Get_prime();
    scanf("%d",&times);
    while(times--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&hp[i]);
        }
        init();
        printf("%d\n",kruskal());
    }
    return 0;
} 

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值