Constructing Roads HDU - 1102

最小生成树算法实践
本文介绍了一个关于使用最小生成树算法解决村庄连接问题的具体案例。通过Kruskal算法实现所有村庄间的最短路径连接,确保任意两个村庄都能互相连通,并使总建造成本最低。文章详细展示了从输入数据解析到算法实现的全过程。
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. 

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. 

InputThe 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. 

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. 
OutputYou 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. 
Sample Input

3
0 990 692
990 0 179
692 179 0
1
1 2

Sample Output

179
题意:给你一个数字N,代表村庄数,下面是一个n*n的矩阵,对应的i,j坐标是i到j修建道路所用长度。再输入的Q的值是有Q条路已经修建,即i到j的路已经给你修好了.

解法:你只需要把已经修筑好的Q条路变为“0”长度就好了,但是我还是wa了一次,超时一次才解决,这道题和poj的POJ 2485很像哦,那道题他给你提示用scanf输入,
这道题却没给你提示(话说我这个人很懒得,我直接用取消同步做的,他没卡我数据我过了)。这道题我wa是因为我寻找对应的路线的权值方法不对导致的,
我最后选择直接继续往结构体里存(反正结构体开的够大,用kruskal算法有排序,我怕啥,emmm)。但是杭电他居然卡我数据时间,我取消同步都不让我过,
最后一气之下我用scanf做就好了(我认怂,zz),直接轻易过。

 1 #include<stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1e3;
 6 int p[maxn];
 7 int n,m;
 8 struct face//存储数据
 9 {
10     int u,v,w;
11 } edge[maxn*maxn];
12 bool cmp(face a,face b)//权值排序
13 {
14     return a.w<b.w;
15 }
16 void init()//并查集1
17 {
18     for(int i=1; i<=n; i++)
19         p[i]=i;
20 }
21 int find(int x)//并查集2
22 {
23     return x==p[x]?x:p[x] = find(p[x]);
24 }
25 long long kruskal()//核心算法,记得用longlong,他的数据挺大的。
26 {
27     long long ans=0;//防止ans存不下
28     init();
29     sort(edge,edge+m,cmp);//此处就是将给的权值先排序
30     for(int i=0; i<m; i++)
31     {
32         int x = find(edge[i].u);
33         int y = find(edge[i].v);
34         if(x!=y)
35         {
36             ans+=edge[i].w;
37             p[x] = y;
38         }
39     }
40     return ans;
41 }
42 int main()
43 {
44    // std::ios::sync_with_stdio(false);
45     while(scanf("%d",&n)!=EOF)//但是注意输入,HDU - 1233 和HDU - 1863给的n和m不一样(切记注意)
46     {
47         m=0;
48         for(int i=1; i<=n; i++)
49         {
50             for(int j=1; j<=n; j++)
51             {
52                 int a;
53                 scanf("%d",&a);
54                 edge[m].u=i;
55                 edge[m].v=j;
56                 edge[m].w=a;
57                 m++;
58             }
59         }
60         int t;
61         scanf("%d",&t);
62         for(int i=1;i<=t;i++)
63         {
64             int a,b;
65             scanf("%d%d",&a,&b);
66             edge[m].u=a;
67             edge[m].v=b;
68             edge[m].w=0;
69             m++;
70         }
71         printf("%lld\n",kruskal());//longlong输出。
72     }
73     return 0;
74 }
View Code
     

转载于:https://www.cnblogs.com/luhongkai/p/9573203.html

一、数据采集层:多源人脸数据获取 该层负责从不同设备 / 渠道采集人脸原始数据,为后续模型训练与识别提供基础样本,核心功能包括: 1. 多设备适配采集 实时摄像头采集: 调用计算机内置摄像头(或外接 USB 摄像头),通过OpenCV的VideoCapture接口实时捕获视频流,支持手动触发 “拍照”(按指定快捷键如Space)或自动定时采集(如每 2 秒采集 1 张),采集时自动框选人脸区域(通过Haar级联分类器初步定位),确保样本聚焦人脸。 支持采集参数配置:可设置采集分辨率(如 640×480、1280×720)、图像格式(JPG/PNG)、单用户采集数量(如默认采集 20 张,确保样本多样性),采集过程中实时显示 “已采集数量 / 目标数量”,避免样本不足。 本地图像 / 视频导入: 支持批量导入本地人脸图像文件(支持 JPG、PNG、BMP 格式),自动过滤非图像文件;导入视频文件(MP4、AVI 格式)时,可按 “固定帧间隔”(如每 10 帧提取 1 张图像)或 “手动选择帧” 提取人脸样本,适用于无实时摄像头场景。 数据集对接: 支持接入公开人脸数据集(如 LFW、ORL),通过预设脚本自动读取数据集目录结构(按 “用户 ID - 样本图像” 分类),快速构建训练样本库,无需手动采集,降低系统开发与测试成本。 2. 采集过程辅助功能 人脸有效性校验:采集时通过OpenCV的Haar级联分类器(或MTCNN轻量级模型)实时检测图像中是否包含人脸,若未检测到人脸(如遮挡、侧脸角度过大),则弹窗提示 “未识别到人脸,请调整姿态”,避免无效样本存入。 样本标签管理:采集时需为每个样本绑定 “用户标签”(如姓名、ID 号),支持手动输入标签或从 Excel 名单批量导入标签(按 “标签 - 采集数量” 对应),采集完成后自动按 “标签 - 序号” 命名文件(如 “张三
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值