Problem A

Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. <br><br>We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.<br>
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.<br><br>Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.<br>
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum. <br>
 

Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output

179

代码:

 #include<iostream>  #include<algorithm>   using namespace std;   struct node{       int from;       int to;       int w;       }; node  edge[102*100];   int parent[102];   bool cmp(node a,node b)  {      if(a.w<=b.w) return true;      return false;  } //查找已经建完道路的顶点 int find(int a) {     if(a!=parent[a])         return find(parent[a]);     else return a; }  int kruskal(int n,int m)  {      sort(edge,edge+m,cmp);//将边的权值从小到大排序      int i,x,y,ans=0;     for(i=0;i<m;i++)      {         x=edge[i].from;         y=edge[i].to;         x=find(x);         y=find(y);         if(x!=y)         {             ans+=edge[i].w;             parent[y]=x;         } }    return ans;  }  int main()  {      int n,q,k,i,j,m;      while(cin>>n)      {          m=0;         for(i=1;i<=n;i++)          {              for(j=1;j<=n;j++)              {                  cin>>k;                 if(i>=j) continue;//标记过的不用重复记录                  edge[m].from=i;                  edge[m].to=j;                  edge[m].w=k;             m++;            }         }         for(k=1;k<=n;k++)         parent[k]=k;          cin>>q;          //将建完的道路的起点和终点都置为相同的起点         for(k=1;k<=q;k++)         {             cin>>i>>j;            i=find(i);             j=find(j);              parent[j]=i;       }       //n个点,m条边         cout<<kruskal(n,m)<<endl;      }      return 0;  }

### Codeforces Contest 370 Problem A: "Ksenia and Pan Scales" 在Codeforces竞赛370中的问题A,题目名为“Ksenia and Pan Scales”,要求解决一个关于天平的问题。具体来说,问题是围绕天平的平衡条件展开的,需要判断给定的一组重量是否能够使天平保持平衡[^1]。 #### 问题描述 给定一个字符串 `s` 表示天平的状态,其中包含字符 `'.'` 和 `'*'`。字符 `'.'` 表示空位,而字符 `'*'` 表示放置了砝码的位置。任务是检查是否存在一种方法,通过将砝码分配到天平的两侧,使得天平达到平衡状态。 #### 解决方案 解决方案的核心思想是模拟天平的工作原理,并计算左右两边的力矩差值。以下是具体的实现步骤: 1. 首先,遍历字符串 `s`,统计左侧和右侧的力矩。 2. 如果左右力矩相等,则输出 `"YES"`,否则输出 `"NO"`。 以下是实现该算法的代码示例: ```python def solve_pan_scales(s): left_torque = 0 right_torque = 0 n = len(s) for i in range(n): if s[i] == '*': if i < n // 2: left_torque += (n // 2 - i) elif i > n // 2: right_torque += (i - n // 2) if left_torque == right_torque: return "YES" else: return "NO" # 示例输入 s = input().strip() print(solve_pan_scales(s)) ``` 此代码通过计算天平两侧的力矩来判断是否可以达到平衡状态[^2]。 #### 注意事项 - 输入字符串的长度为奇数,确保天平中心点明确。 - 确保正确处理边界情况,例如所有砝码都在一侧的情况。 ### 结论 通过上述方法,可以有效地解决 Codeforces Contest 370 Problem A 的问题。该方法的时间复杂度为 O(n),满足题目对时间限制的要求[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值