1034 Head of a Gang (30分) C++ BFS() STL(map,set)

该博客介绍了一种解决警察查找犯罪团伙头目的方法。通过分析人们的电话记录,定义了两个人之间的关系权重为他们之间所有通话的总时长。当存在一个由超过两人组成的关系群且总权重超过给定阈值时,这个群被视为一个帮派。博客提供了C++代码实现,通过邻接矩阵存储关系,并使用广度优先搜索寻找帮派及其头目。输出包括帮派总数及每个帮派的头目和成员数。示例输入和输出展示了如何处理特定的电话记录数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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

输入说明

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.

输出说明

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.

输入样例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
输出样例1
2
AAA 3
GGG 3
输入样例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
输出样例2
0

分析

本题首先要把名字赋予一个特定的数字id进行存储然后利用邻接矩阵对边之间的关系进行存储,可以用map<>对姓名和id还有id和姓名进行相互映射。
然后进行bfs()返回在同一集合中的总人数temp,计算其通话总时间sum和通话时间最大的人,如果temp>2同时sum>2*ts(总门槛),则属于一个帮派。

C++ 代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4;
vector<string> ansn;
set<string> sname;
map<string,int> mp;     //姓名,id映射
map<int,string> ying;   //id,姓名映射
int n,ts,idx,Time,g[N][N],dist[N];
int ans,psize[N];
bool st[N],bfst[N]; //st用来存储此人是否被遍历过,bfst则是判断此人是否在一轮bfs中被遍历过
int bfs(int u)
{
	memset(bfst,0,sizeof bfst);
	memset(dist,0,sizeof dist);
	int capa=1;
	st[u]=true;
	bfst[u]=true;
	queue<int> q;
	q.push(u);
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		for(int i=0;i<idx;i++)
		{
			dist[t]+=g[t][i];
			if(!bfst[i] && g[t][i])
			{
				q.push(i);
				st[i]=true;
				bfst[i]=true;
				capa++;
			}
		}
	}
	if(capa<3) capa=0;  //帮派人数小于3,直接返回0
	return capa;
}
int main()
{
	cin>>n>>ts;
	string name1,name2;
	for(int i=0;i<n;i++)
	{
		cin>>name1>>name2>>Time;
		if(!sname.count(name1))
		{
			mp[name1]=idx;
			ying[idx]=name1;
			idx++;
			sname.insert(name1);
		}
		if(!sname.count(name2))
		{
			mp[name2]=idx;
			ying[idx]=name2;
			idx++;
			sname.insert(name2);
		}
		g[mp[name1]][mp[name2]]+=Time;
		g[mp[name2]][mp[name1]]+=Time;
	}
	for(int i=0;i<idx;i++)
	{
		if(!st[i])
		{
			int temp=bfs(i);    
			if(temp)    //temp非0则进行下一步运算,反之则跳过节省时间
			{
				double sum=0;
				int maxp=-1,idxp=-1;
				for(int i=0;i<idx;i++)
				{
					sum+=dist[i];
					if(maxp<dist[i])
					{
						idxp=i;
						maxp=dist[i];
					}
				}
				if(sum>ts*2)
				{
					ansn.push_back(ying[idxp]);
					ans++;
					psize[idxp]=temp;
				}
			} 
		}
	}
	sort(ansn.begin(),ansn.end());
	cout<<ans<<endl;
	if(ans>0)
	{
		for(int i=0;i<ans;i++)
		{
			cout<<ansn[i]<<" "<<psize[mp[ansn[i]]]<<endl;
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值