专题四 Problem E

本文介绍了一种使用并查集算法解决最小生成树问题的方法。具体地,通过将道路成本作为边权,实现了计算连接所有村庄所需最低成本的目标。文章提供了完整的AC代码示例。
一、题目编号:
          1004
二、简单题意:
      省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。现得到城镇道路统计表,表中列出了任意两城镇间修建道路的费用,以及该道路是否已经修通的状态。现请你编写程序,计算出全省畅通需要的最低成本。
三、解题思路形成过程
        依旧用并查集来做,按照乡村之间的距离升序排列,如果两个村庄没有联系,成本就加上修路费用,并且利用并查集将其放在一个集合,设置父节点。最后输出即可。
四、感想
       仍然是利用并查集,跟前面题相比,只不过是把距离换成费用,但是换汤不换药,思路不变。
五、AC代码
#include <cstdio>
#include <cstdlib>
#include <cstring>

const int maxx = 101;

typedef struct E{
 int x,y,cost;
}E;

int pre[maxx];
E edg[5200];

void init(int n){
 int i;
 for(i=1;i<=n;++i){
  pre[i] = i;
 }
}

int root(int x){
 if(x!=pre[x]){
  pre[x] = root(pre[x]);
 }
 return pre[x];
}

int merge(int x,int y){
 int ret = 0;
 int fa = root(x);
 int fb = root(y);
 if(fa!=fb){
  ret = 1;
  pre[fa] = fb;
 }
 return ret;
}

int cmp(const void *a,const void *b){
 E *pa = (E *)a;
 E *pb = (E *)b;
 return pa->cost-pb->cost;
}

int main(){
 int n,x,y,cst,stat,i,pos;

 while(scanf("%d",&n) && n){
  init(n);
  pos = 0;
  int limit = n*(n-1)/2;
  for(i=0;i<limit;++i){
   scanf("%d %d %d %d",&x,&y,&cst,&stat);
   if(stat==1){
    merge(x,y);
   }else{
    edg[pos].x = x;
    edg[pos].y = y;
    edg[pos].cost = cst;
    ++pos;
   }
  }
  qsort(edg,pos,sizeof(E),cmp);
  int minx = 0;
  for(i=0;i<pos;++i){
   if(merge(edg[i].x,edg[i].y)==1){
    minx += edg[i].cost;
   }
  }

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







Dynamic programming is an algorithm suitable for solving the Knapsack problem. It avoids repeated calculations by decomposing complex problems into overlapping sub - problems and storing the solutions to the sub - problems. The core of dynamic programming is state definition and state transition equations [^1]. The general steps of using dynamic programming to solve the Knapsack problem are as follows: 1. **Define the state**: Usually, two - dimensional states are defined. For example, let `dp[i][j]` represent the maximum value that can be obtained when considering the first `i` items and the capacity of the knapsack is `j`. 2. **State transition equation**: For the 0 - 1 Knapsack problem, if the weight of the `i` - th item is `w[i]` and the value is `v[i]`, then the state transition equation is: - When `j < w[i]`, `dp[i][j]=dp[i - 1][j]` (the current item cannot be put into the knapsack). - When `j >= w[i]`, `dp[i][j]=max(dp[i - 1][j], dp[i - 1][j - w[i]]+v[i])` (choose whether to put the current item into the knapsack). Here is a simple Python code example for the 0 - 1 Knapsack problem: ```python def knapsack(weights, values, capacity): n = len(weights) dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)] for i in range(1, n + 1): for j in range(1, capacity + 1): if j < weights[i - 1]: dp[i][j] = dp[i - 1][j] else: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - weights[i - 1]] + values[i - 1]) return dp[n][capacity] weights = [2, 3, 4, 5] values = [3, 4, 5, 6] capacity = 8 print(knapsack(weights, values, capacity)) ``` ### Application scenarios - **Resource allocation**: In project management, given a limited amount of resources (such as time, budget, manpower), and different tasks with different resource requirements and benefits, the Knapsack problem can be used to select the most profitable combination of tasks. - **Stock selection**: When an investor has a certain amount of funds and there are multiple stocks with different prices and expected returns, the Knapsack problem can be used to select the most profitable stock portfolio.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值