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

 题目大意:

警察查找犯罪头目的一个方法是查看通话记录。如果A和B之间通过话,就说明A和B有联系,联系之间的权重取决于两个人打电话的时间长短。一个犯罪团体就是一个超过两人并且总通话时间超过K的团体,在每一个犯罪团体中,权重最大的那个就是头目。现给出一个通话列表,要求你求出每个犯罪团体的头目。

解题思路:

DFS遍历,既可以求得连通分量,也可以求得每个连通分量的总权值和最大权值。

注意点:

  • visited标记,已经标记为true的点就无法访问了,但是该题存在环,因此仅仅是标记为true就不访问了的话将会导致边权计算有遗漏。这里我同时使用边权和visited来进行标记,只有当visited为true且两点间边权为0时才说明两点之间已经没有权值可以计算。
  • 会有两点之间存在多条边的情况,这里采用的方法是,如果两点间的边权之前已经有值了,就将新的边权值直接叠加上去。
  • 当成无向图处理。

代码如下:

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;

int N,K;
map<string,int> heads;//自动按照第一个非递减排序 
int arcs[3000][3000];//边权
int vexs[3000] = {0};//点权 
bool visited[3000] = {false};
vector<string> vec;
int Max = 0,maxNumber = 0,sum = 0,cnt = 0;

int change(string s){
	for(int i = 0 ; i < vec.size() ; i ++)
		if(s == vec[i])
			return i;
	vec.push_back(s);
	return vec.size() - 1;
}

void dfs(int u){
	visited[u] = true;
	cnt ++;
	if(vexs[u] > Max){
		Max = vexs[u];
		maxNumber = u;
	}
	for(int i = 0 ; i < vec.size() ; i ++){
		if(!visited[i] && arcs[u][i] != 0)
		{
			sum += arcs[u][i];
			arcs[u][i] = arcs[i][u] = 0;
			dfs(i);
		}else if(visited[i] && arcs[u][i] != 0){
			sum += arcs[u][i];
			arcs[u][i] = arcs[i][u] = 0;
		}
	}
}

int main(){
	scanf("%d%d",&N,&K);
	fill(arcs[0],arcs[0]+3000*3000,0);
	for(int i = 1 ; i <= N ; i ++){
		string a,b;
		int c;
		cin>>a>>b>>c;
		int aa = change(a);
		int bb = change(b);
		if(arcs[aa][bb] == 0 || arcs[bb][aa] == 0)/*会有环,消除环*/ 
			arcs[aa][bb] = arcs[bb][aa] = c;
		else{
			arcs[aa][bb] += c;
			arcs[bb][aa] = arcs[aa][bb];
		}
		vexs[aa] += c;
		vexs[bb] += c;
	} 
	for(int i = 0 ; i < vec.size() ; i ++){
		if(!visited[i])
		{
			Max = 0;
            maxNumber = 0;
            sum = 0;
            cnt = 0;
			dfs(i);
			if(cnt > 2 && sum > K)
			{
				heads[vec[maxNumber]] = cnt;
			}
		}
			
	} 
	printf("%d\n",heads.size());
	for(map<string,int>::iterator it = heads.begin();it != heads.end(); it ++)
		cout<<it->first<<" "<<it->second<<endl;
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值