程序设计基础81 图之无向图防止回头和访问通向已访问结点的路径

1034 Head of a Gang (30 分)

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threthold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:

0

一,关注点

1,本题是无向图,注意在遍历完路径时要删除回头的结点。

2,注意本题要在判断完如今结点通向任意结点加完距离后再判断这个任意结点是否该访问。而不是判断这个任意节点是否已访问且是否可通向再累加。不要和最短路径混了,否则会落下路径。

二,细节

1,本题数组和邻接矩阵都是以2000开头的。

2,使用memset必须添加string.h

三,我的代码

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
const int max_n = 2020;
//const int co_max_n = 2020;
const int INF = 10000;
int N = 0, K = 0;
int co_index = 0;
int flag[max_n];
int G[max_n][max_n];
int PersonTimes[max_n];
struct Node {
	int id, num;
}node;
vector<Node> ans;
map<int, string> num_to_str;
map<string, int> str_to_num;
void convertion(string str) {
	if (str_to_num.count(str) == 0) {
		str_to_num[str] = co_index;
		num_to_str[co_index] = str;
		co_index++;
	}
}
bool cmp(Node a, Node b) {
	return num_to_str[a.id] < num_to_str[b.id];
}
void DFS(int nowPerson, int &nowTotalTime,int &nowMaxPerson,int &nowPersonNums) {
	flag[nowPerson] = 1;
	nowPersonNums++;
	if (PersonTimes[nowPerson] > PersonTimes[nowMaxPerson]) nowMaxPerson = nowPerson;
	for (int i = 0; i < N * 2; i++) {
		if (G[nowPerson][i] != 0) {
			nowTotalTime += G[nowPerson][i];
			G[i][nowPerson] = 0;
			if (flag[i] == false) {
				DFS(i, nowTotalTime, nowMaxPerson, nowPersonNums);
			}
		}
	}
}
int main() {
	string str_1, str_2;
	int times = 0;
	int TotalTime = 0, MaxPerson = 0, PersonNums = 0;
	memset(PersonTimes, 0, sizeof(PersonTimes));
	memset(flag, 0, sizeof(flag));
	memset(G, 0, sizeof(G));
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; i++) {
		cin >> str_1 >> str_2 >> times;
		convertion(str_1);
		convertion(str_2);
		G[str_to_num[str_1]][str_to_num[str_2]] += times;
		G[str_to_num[str_2]][str_to_num[str_1]] += times;
		PersonTimes[str_to_num[str_1]] += times;
		PersonTimes[str_to_num[str_2]] += times;
	}
	for (int i = 0; i < N * 2; i++) {
		TotalTime = 0, MaxPerson = i, PersonNums = 0;
		if (flag[i] == 0) {
			DFS(i, TotalTime, MaxPerson, PersonNums);
			if (TotalTime > K&&PersonNums > 2) {
				node.id = MaxPerson;
				node.num = PersonNums;
				ans.push_back(node);
			}
		}
	}
	sort(ans.begin(), ans.end(), cmp);
	printf("%d\n", ans.size());
	string name;
	for (int i = 0; i < ans.size(); i++) {
		name = num_to_str[ans[i].id];
		cout << name << " " << ans[i].num << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于YOLOv9实现工业布匹缺陷(破洞、污渍)检测系统python源码+详细运行教程+训练好的模型+评估 【使用教程】 一、环境配置 1、建议下载anaconda和pycharm 在anaconda中配置好环境,然后直接导入到pycharm中,在pycharm中运行项目 anaconda和pycharm安装及环境配置参考网上博客,有很多博主介绍 2、在anacodna中安装requirements.txt中的软件包 命令为:pip install -r requirements.txt 或者改成清华源后再执行以上命令,这样安装要快一些 软件包都安装成功后才算成功 3、安装好软件包后,把anaconda中对应的python导入到pycharm中即可(不难,参考网上博客) 二、环境配置好后,开始训练(也可以训练自己数据集) 1、数据集准备 需要准备yolo格式的目标检测数据集,如果不清楚yolo数据集格式,或者有其他数据训练需求,请看博主yolo格式各种数据集集合链接:https://blog.csdn.net/DeepLearning_/article/details/127276492 里面涵盖了上百种yolo数据集,且在不断更新,基本都是实际项目使用。来自于网上收集、实际场景采集制作等,自己使用labelimg标注工具标注的。数据集质量绝对有保证! 本项目所使用的数据集,见csdn该资源下载页面中的介绍栏,里面有对应的下载链接,下载后可直接使用。 2、数据准备好,开始修改配置文件 参考代码中data文件夹下的banana_ripe.yaml,可以自己新建一个不同名称的yaml文件 train:训练集的路径 val:验证集的路径 names: 0: very-ripe 类别1 1: immature 类别2 2: mid-ripe 类别3 格式按照banana_ripe.yaml照葫芦画瓢就行,不需要过多参考网上的 3、修改train_dual.py中的配置参数,开始训练模型 方式一: 修改点: a.--weights参数,填入'yolov9-s.pt',博主训练的是yolov9-s,根据自己需求可自定义 b.--cfg参数,填入 models/detect/yolov9-c.yaml c.--data参数,填入data/banana_ripe.yaml,可自定义自己的yaml路径 d.--hyp参数,填入hyp.scratch-high.yaml e.--epochs参数,填入100或者200都行,根据自己的数据集可改 f.--batch-size参数,根据自己的电脑性能(显存大小)自定义修改 g.--device参数,一张显卡的话,就填0。没显卡,使用cpu训练,就填cpu h.--close-mosaic参数,填入15 以上修改好,直接pycharm中运行train_dual.py开始训练 方式二: 命令行方式,在pycharm中的终端窗口输入如下命令,可根据自己情况修改参数 官方示例:python train_dual.py --workers 8 --device 0 --batch 16 --data data/coco.yaml --img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 500 --close-mosaic 15 训练完会在runs/train文件下生成对应的训练文件及模型,后续测试可以拿来用。 三、测试 1、训练完,测试 修改detect_dual.py中的参数 --weights,改成上面训练得到的best.pt对应的路径 --source,需要测试的数据片存放的位置,代码中的test_imgs --conf-thres,置信度阈值,自定义修改 --iou-thres,iou阈值,自定义修改 其他默认即可 pycharm中运行detect_dual.py 在runs/detect文件夹下存放检测结果片或者视频 【特别说明】 *项目内容完全原创,请勿对项目进行外传,或者进行违法等商业行为! 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值