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

1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B...

PAT (Advanced Level) 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 pho...

PAT (Advanced Level) Practise 1034 Head of a Gang (30)

1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B...

PAT 1034. Head of a Gang (30)

//1034. Head of a Gang (30) //#include "stdafx.h" //case 3 and case 5一直无法通过:主要是没有对结果进行排序 //比较简单的df...

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

注: Gang的成员数要达到两人以上。#include #include #include #include #include #include #include using names...

PAT A1034. 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 pho...

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

1034. Head of a Gang (30)

1. 原题: https://www.patest.cn/contests/pat-a-practise/1034 2. 思路: 题意: 给你很多条边,求出符合条件的连通分量数,以及每个 连通...

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

1034. Head of a Gang (30)

首先并查集模版要先写上,hash肯定要用 设置times数组记录每个人的通话总时长,并且把所有人合并集合 设置一个长度为N结构体,集合父亲对应位置存储此集合的一些信息,比如集合中所有人的通话总时长...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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