pat a1034 团伙头目【图的深搜】

 题目:

https://pintia.cn/problem-sets/994805342720868352/problems/994805456881434624

一、问题描述

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.

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

给出若干人之间的通话长度(视为无向边),这些通话将他们分为若干组。每个组的总边权设为该组内的所有通话的长度之和,而每个人的点权设为该人参与的通话长度之和。现在给定一个阈值K,且只要一个组的总边权超过K,并满足成员人数超过2,则将该组视为“犯罪团伙(Gang)”,而该组内点权最大的人视为头目。

要求输出“犯罪团伙”的个数,并按头目姓名字典序从小到大的顺序输出每个“犯罪团伙”的头目姓名和成员人数。

二、算法实现

#include<iostream> 
#include<string>
#include<map>
using namespace std;

const int maxn=2010;
const int inf=1000000000;

map<int,string> intToString;
map<string,int> stringToInt;
map<string,int> Gang; 

int G[maxn][maxn]={0};
int weight[maxn]={0};

int n;  //通话记录数量 
int k;  //阈值 
int numPerson=0;  //总人数 

bool vis[maxn]={false};

int change(string str)
{
	if(stringToInt.find(str)!=stringToInt.end())   //能找到 
	{
		return stringToInt[str];
	}
	else  //没有找到 
	{
		stringToInt[str]=numPerson;
		intToString[numPerson]=str;
		//numPerson++;
		return numPerson++;  //骚操作!! 
	}		
}

//nowVisit:当前访问的结点
//head:头目
//numMember: 成员人数
//totalValue:团伙内总的通话时长 
void DFS(int nowVisit,int &head,int &numMember,int &totalValue)
{
	numMember++;
	vis[nowVisit]=true;
	if(weight[nowVisit]>weight[head])
	{
		head=nowVisit;
	} 
	for(int i=0;i<numPerson;i++)
	{
		if(G[nowVisit][i]>0)
		{
			totalValue+=G[nowVisit][i];
			G[nowVisit][i]=G[i][nowVisit]=0;
			if(vis[i]==false)
			{
				DFS(i,head,numMember,totalValue);
			}			
		}
	}	
}

void DFSTrave()
{
	for(int i=0;i<numPerson;i++)
	{
		if(vis[i]==false)
		{
			int head=i;  //老大 
			int numMember=0;  //团伙成员人数 
			int totalValue=0;  //团伙总的通话时长 
			
			DFS(i,head,numMember,totalValue);
			if(numMember>2&&totalValue>k)
			{
				Gang[intToString[head]]=numMember;
			}			
		}		
	}	
}

int main() 
{
	int w;  //通话时长 
	string str1,str2; //通话双方 
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>str1>>str2>>w;
		int id1=change(str1);
		int id2=change(str2); 
		//边读入,边操作 
		weight[id1]+=w;
		weight[id2]+=w;
		G[id1][id2]+=w;
		G[id2][id1]+=w;		
	}
	DFSTrave();	
	cout<<Gang.size()<<endl;
	for(map<string,int>::iterator it=Gang.begin();it!=Gang.end();it++) 
	{
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

string转int函数

map<int,string> intToString;
map<string,int> stringToInt;
int numPerson=0;  //总人数 

int change(string str)
{
	if(stringToInt.find(str)!=stringToInt.end())   //能找到 
	{
		return stringToInt[str];
	}
	else  //没有找到 
	{
		stringToInt[str]=numPerson;
		intToString[numPerson]=str;
		//numPerson++;
		return numPerson++;  //骚操作!! 
	}		
}
发布了180 篇原创文章 · 获赞 64 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/OpenStack_/article/details/104072110
今日推荐