PAT 甲级 A1034 Head of a Gang

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

题意(样例分析):

给出8个通话记录,给出一个阈值59,然后下面8个通话记录,第一第二个字符串是拨打电话的两个人的名字,后面跟着的10是指通话时间,求出有多少个犯罪团伙(即求连通块的数目,跟组内人员打了电话的,有关系的都是一个连通块内),每个犯罪团伙头目的名字和团伙内人数(头目是联通块通话时间最长的那个)。

思路:
用二维数组存储图,边权是通话时间,点权是这个人通话了多久,建好图后,遍历图内的连通块,深度优先搜索找该连通图内成员, 一个连通块就是一个犯罪团伙,从中找到点权最大的就是头目,连通块内顶点的个数就是该团伙内的人数,用一个Map存放团伙头目的名字和团伙内的人数,因为map可以自动排序,从而满足题目要求按名字的字典顺序从小 到大排列。

注意点:该题算点算边的都有,一定要区分开,算边和点没有关系,点被标记了,边还是要算的,算团内人数的时候,是属于点的计算,不能把算人数放到算边的位置。

代码:

#include<iostream>
#include<map>
using namespace std ;
const int maxn = 2010;//最多1000组通话记录,即最多有2000个人通话;
int G[maxn][maxn] = {0};//边权 
int weight[maxn] = {0};//点权;
int n, k, w, numPerson = 0;
bool vis[maxn] = {false};

map<string, int > stringToNum; //名字->编号; 
map<int, string > numToString;//编号->名字 
map<string ,int > Gang;//存放头目名称和团体人数; 


int create(string s) {//创建映射,给图内所有顶点编号; 
	if(stringToNum.find(s) != stringToNum.end( )) { 
		return stringToNum[s];
	} else {
		stringToNum[s] = numPerson;
		numToString[numPerson] = s;
		return numPerson++;//计算参与通话的人数,通话记录n != numPerson; 
	}
}

//挖出连通块;
void DFS(int nowVistor, int& head,int& numMember, int& totalValue) {//调用栈相互影响,从而算出最终的三个值; 
	vis[nowVistor] = true;
	numMember++;
	if(weight[nowVistor] > weight[head]) head = nowVistor;	
	for(int i = 0; i < numPerson; i++) {//遍历所有顶点; 
		if(G[nowVistor][i] > 0) {//这些顶点是直接连通的,有通话记录就要算,因为这个一个算边的过程,和顶点是否被访问无关;
		totalValue += G[nowVistor][i];//totalValue算出该连通块的边权; 
		G[nowVistor][i] = 0;//已经计算过该边就删除; 
		G[i][nowVistor] = 0; 
			if(vis[i] == false) {//保证遍历每条边,每个顶点,不重不漏; 
				DFS(i, head, numMember, totalValue); 
			} 
		}
	}
}

//找出连通块;
void DFSTravel( ) {
	for(int i = 0; i < numPerson; i++) {//遍历所有顶点; 
		if(vis[i] == false) {
			int nowVistor, head = i, numMember = 0, totalValue = 0;//连通块初始化;
			 
			DFS(i, head, numMember, totalValue);//遍历完以后判断人数和总边权; 
			if(numMember > 2 && totalValue > k) {
				Gang[numToString[head] ] = numMember;//map自动从小到大排列; 
			}
		}
	}
} 
int main() {
	scanf("%d %d", &n, &k);
	string str1, str2;
	for(int i = 0; i < n; i++) {
		cin >> str1 >> str2 >> w;//输入通话人的名字和,通话时间;
		int id1 = create(str1);
		int id2 = create(str2);
		G[id1][id2] += w;//边权是累加的,可能有相同的人的通话记录; 
		G[id2][id1] += w;
		weight[id1] += w;
		weight[id2] += w;
	}
	DFSTravel( );//遍历图;
	printf("%d\n",Gang.size( ));//团伙组数;
	for(map<string, int > :: iterator it = Gang.begin( ); it != Gang.end( ); it++) {
		cout<< it->first << " " << it->second << "\n";
	}
	return 0; 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值