Contestants Division - POJ 3140 树形dp

本文探讨了一个在新ACM-ICPC区域竞赛中,如何利用单向信息传输系统,将大学分为两个连接区域,使得这两个区域的学生数量之差最小化的策略。通过深度优先搜索(DFS)算法实现解决方案。

Contestants Division
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7987 Accepted: 2280

Description

In the new ACM-ICPC Regional Contest, a special monitoring and submitting system will be set up, and students will be able to compete at their own universities. However there’s one problem. Due to the high cost of the new judging system, the organizing committee can only afford to set the system up such that there will be only one way to transfer information from one university to another without passing the same university twice. The contestants will be divided into two connected regions, and the difference between the total numbers of students from two regions should be minimized. Can you help the juries to find the minimum difference?

Input

There are multiple test cases in the input file. Each test case starts with two integers N and M, (1 ≤ N ≤ 100000, 1 ≤ M ≤ 1000000), the number of universities and the number of direct communication line set up by the committee, respectively. Universities are numbered from 1 to N. The next line has N integers, the Kth integer is equal to the number of students in university numbered K. The number of students in any university does not exceed 100000000. Each of the following M lines has two integers st, and describes a communication line connecting university s and university t. All communication lines of this new system are bidirectional.

N = 0, M = 0 indicates the end of input and should not be processed by your program.

Output

For every test case, output one integer, the minimum absolute difference of students between two regions in the format as indicated in the sample output.

Sample Input

7 6
1 1 1 1 1 1 1
1 2
2 7
3 7
4 6
6 2
5 7
0 0

Sample Output

Case 1: 1

题意:给你一棵树,让你将这棵树分成两组,问权值的差最小是多少。

思路:dfs,比较简单就不解释了。另外我有一点困惑的是边的数量比较多,应该存在形成环的情况,可是我去搜题解的时候发现都没有说这一点,好像都是按照一棵树来做的,然后我就试了下,过了……欢迎大家批评指教。

莫名其妙的AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
vector<int> vc[100010];
long long value[100010],sum,ans;
int n,m,vis[100010];
void dfs(int u)
{ int i,j,k,v;
  vis[u]=1;
  for(i=0;i<vc[u].size();i++)
  { v=vc[u][i];
    if(vis[v]==0)
    { dfs(v);
      value[u]+=value[v];
      if(value[v]*2-sum<0)
       ans=min(ans,sum-value[v]*2);
      else
       ans=min(ans,value[v]*2-sum);
    }
  }
}
int main()
{ int i,j,k,u,v,t=0;
  while(~scanf("%d%d",&n,&m) && n+m)
  { sum=0;
    ans=1e18;
    for(i=1;i<=n;i++)
    { vc[i].clear();
      vis[i]=0;
    }
    for(i=1;i<=n;i++)
    { scanf("%I64d",&value[i]);
      sum+=value[i];
    }
    for(i=1;i<=m;i++)
    { scanf("%d%d",&u,&v);
      vc[u].push_back(v);
      vc[v].push_back(u);
    }
    dfs(1);
    printf("Case %d: %I64d\n",++t,ans);
  }
}


内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
虽然给定的参考引用中未涉及使用C语言实现7 - 5竞赛排名的内容,但可以提供一种通用的竞赛排名实现思路和示例代码。 ### 实现思路 1. 定义结构体来存储每个参赛选手的信息,包括编号、得分等。 2. 输入每个选手的得分。 3. 对选手按得分进行排序。 4. 输出排名结果。 ### 示例代码 ```c #include <stdio.h> #include <stdlib.h> // 定义选手结构体 typedef struct { int id; // 选手编号 int score; // 选手得分 } Contestant; // 比较函数,用于qsort排序 int compare(const void *a, const void *b) { Contestant *contestantA = (Contestant *)a; Contestant *contestantB = (Contestant *)b; return contestantB->score - contestantA->score; // 降序排序 } int main() { const int numContestants = 7; // 7名参赛选手 Contestant contestants[numContestants]; // 输入每个选手的得分 for (int i = 0; i < numContestants; i++) { contestants[i].id = i + 1; printf("请输入选手 %d 的得分: ", i + 1); scanf("%d", &contestants[i].score); } // 对选手按得分进行排序 qsort(contestants, numContestants, sizeof(Contestant), compare); // 输出排名结果 printf("竞赛排名如下:\n"); for (int i = 0; i < 5; i++) { // 取前5名 printf("第 %d 名: 选手 %d,得分: %d\n", i + 1, contestants[i].id, contestants[i].score); } return 0; } ``` ### 代码解释 1. **结构体定义**:`Contestant` 结构体用于存储每个选手的编号和得分。 2. **输入得分**:通过循环输入每个选手的得分。 3. **排序**:使用 `qsort` 函数对选手按得分进行降序排序。 4. **输出排名**:输出前5名选手的排名、编号和得分。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值