关闭

【PAT】1034. Head of a Gang (30)

标签: 并查集BFS
411人阅读 评论(0) 收藏 举报
分类:

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

分析:题目写的不够清楚。

什么是gang? 

 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.

(1)成员数据大于2  (2)相互间的通话时间总和大于K,例如:

AAA BBB 10
BBB AAA 20
AAA CCC 40
则她们相互间的通话时间为10 + 20 + 40 。


以下代码有两组数据错误,得分为22,待改进:

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define INF 9999
int N,K,index;
vector<vector<int> > cost;
vector<bool> used;
vector<int> weight; 
map<string,int> name2id;
map<int, string> id2name;

pair<int,int> bfs(int s){
	queue<int> que;
	que.push(s);
	int total=0, max=weight[s], maxIndex=s, cnt=0;
	while(!que.empty()){
		int t = que.front();
		que.pop();
		cnt++;	
		total += weight[t];	 
		if(weight[t]>max){
			max = weight[t];
			maxIndex = t;
		}
		used[t] = true;
		for(int i=0; i<index; i++){
		    if( (!used[i])&&(cost[t][i]!=INF) ){
				used[i] = true;
		    	que.push(i);
			}
		}
	}
	if(total>K*2 && cnt>2)
		return make_pair(maxIndex,cnt);
	else
		return make_pair(-1,-1);	
}

int main(){
	scanf("%d%d",&N,&K);
	int i,time;
	string stra, strb;
	int a, b;
	cost.resize(2*N,vector<int>(2*N,INF));
	weight.resize(2*N,0);
	index=0;
	for(i=0; i<N; i++){
		cin>>stra>>strb>>time;
		if(name2id.find(stra)!=name2id.end()){
			a = name2id[stra];		 
		}else{
			a = index;
			name2id.insert(make_pair(stra,index));
			id2name.insert(make_pair(index,stra));
			index++;		
		}		
		if(name2id.find(strb)!=name2id.end()){
			b = name2id[strb];		 
		}else{
			b = index;
			name2id.insert(make_pair(strb,index));
			id2name.insert(make_pair(index,strb));
			index++;
		}
		if(cost[a][b]==INF){
			cost[a][b] = 0;
			cost[b][a] = 0;
		} 
		cost[a][b] += time;
		cost[b][a] += time;
		weight[a] += time;
		weight[b] += time;
	}	
	
	used.resize(index-1,false);
	vector<pair<int,int> > result;
	int max = 0;
	for(i=0; i<index; i++){
		if(!used[i]){
			pair<int, int> p;
			p = bfs(i);
			if(p.first==-1){
				continue;
			}
			if(p.second==max){
				result.push_back(p);
			}else if(p.second>max){
				result.clear();
				result.push_back(p);
				max = p.second;
			}			
		}
	}
	printf("%d\n",result.size());
	for(i=0; i<result.size(); i++){
		cout<<id2name[result[i].first]<<" "<<result[i].second<<endl;
	}
	return 0;
}



















0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:104559次
    • 积分:2485
    • 等级:
    • 排名:第14831名
    • 原创:142篇
    • 转载:6篇
    • 译文:0篇
    • 评论:21条
    About Me
    211小硕在读,程序员,阅读爱好者,马拉松练习者。 微博搜索: cs_xuejin http://weibo.com/realxuejin
    文章分类
    最新评论