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

原创 2015年11月20日 11:16:43

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



















版权声明:本文为博主原创文章,未经博主允许不得转载。

PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)

官网1034. Head of a Gang (30)时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, ...
  • qq_16949707
  • qq_16949707
  • 2016年09月08日 13:45
  • 102

PAT1034有理数四则运算(20)

1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B ...
  • AliceGoToAnother
  • AliceGoToAnother
  • 2016年02月23日 22:08
  • 345

1034_有理数四则运算(20)

#include int gong(long long a,long long b) { long long r,min,max; min=(a>b)?b:a; max=(a>b)?a:b; ...
  • fang_abc
  • fang_abc
  • 2015年03月07日 09:43
  • 2319

1057. Stack (30)-PAT甲级真题(树状数组)

1057. Stack (30) Stack is one of the most fundamental data structures, which is based on the princi...
  • liuchuo
  • liuchuo
  • 2016年08月17日 15:30
  • 295

【九度】题目1446:Head of a Gang

题目链接:题目1446:Head of a Gang                 看到这个题目的时候,感觉应该用并查集去做。但是想了好多次,都觉得下不了手。         下午踏踏实实的写了一...
  • u013027996
  • u013027996
  • 2013年12月03日 21:17
  • 1679

PAT : 1034. 有理数四则运算(20)

本题要求编写程序,计算2个有理数的和、差、积、商。 输入格式: 输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中分子和分母全是整型范围内的整数,负号只可能出现...
  • hyt502
  • hyt502
  • 2015年03月25日 21:48
  • 881

【PAT】1034. 有理数四则运算(20)

1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHE...
  • artemisrj
  • artemisrj
  • 2015年07月13日 21:28
  • 751

PAT 乙级 1034.有理数四则运算

1034. 有理数四则运算 本题要求编写程序,计算2个有理数的和、差、积、商。 输入格式: 输入在一行中按照“a1/b1 a2/b2”的格式给出两个分数形式的有理数,其中...
  • Dodd9199
  • Dodd9199
  • 2016年03月27日 21:00
  • 230

1034. 有理数四则运算(20) PAT

1034. 有理数四则运算(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B ...
  • qq_34447388
  • qq_34447388
  • 2017年05月19日 21:43
  • 68

【PAT1018】 Public Bike Management 单源最短路径&路径记录回溯

1018. Public Bike Management (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 S...
  • tiantangrenjian
  • tiantangrenjian
  • 2014年02月27日 22:24
  • 3226
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【PAT】1034. Head of a Gang (30)
举报原因:
原因补充:

(最多只允许输入30个字)