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

有几点注意:
1.题目中给的n(通话记录数)是小于等于1000的,我按惯性思维直接将maxn定义为1010,结果测试点3出现段错误,应该要注意一通电话是由两个人打的,因此maxn应定义为2010
2.注意将字符串转换为整型的方法和map的使用
3.这道题目涉及到了边权值,我个人认为边权值使用邻接矩阵更好做题——g[u][0…n]即表示u所有的边权值。而且由于无向图临界矩阵是对称的,所以可以当v>u时遍历g[u][u+1…n]相加就可以得到一个连通块的所有权之和——这样可以避免回路时候DFS加不到构成闭环边的权。

代码如下:

#include<iostream>
#include<map>
#include<vector>
#include<string>
#define maxn 1010
using namespace std;
map<string,int> StringToNum;
map<int,string> NumToString;
map<string,int> ans;
int g[maxn][maxn]={0};      //邻接矩阵存储边权值便于直接读取两边的长度 
int n,k,stringnum;   //stringnum顶点的个数 
bool visit[maxn]={false};
int length,sum,maxno;      //length每个组内的总权值  sum每个组的成员数   maxno最大边权和的顶点  
int change(string str){     //string转成int 
	if(StringToNum.find(str)==StringToNum.end()){     //第一次出现 
		StringToNum[str]=stringnum;
		NumToString[stringnum]=str;
		stringnum++;
	}
	return StringToNum[str];
}
int maxlength; 
void DFS(int u){    //a总数  b总权值 
	visit[u]=true;
//	cout<<"当前访问的顶点是: "<<NumToString[u]<<endl;
	sum++;
	int nowlength=0;      
	for(int v=0;v<n;v++){    //对这个顶点的操作 
		if(g[u][v]!=0){         //计算这个顶点的所有边的权值和并记录下最大值的顶点
		    nowlength=nowlength+g[u][v]; 
		 //   cout<<NumToString[u]<<"所有边的权值为:"<<nowlength<<endl;
		    if(nowlength>maxlength){
			    maxlength=nowlength;
		//	    cout<<"最大的边权值为: "<<maxlength<<endl;
			    maxno=u;
		    }
	    }
	    if(v>u&&g[u][v]!=0){     //计算总长度 
			length=length+g[u][v];
		}    
	}
	for(int v=0;v<n;v++){
		if(visit[v]==false&&g[u][v]!=0){    //如果v未被访问,且u可到达v 		
			DFS(v);
		}
	}
}
void DFSTravel(){
	for(int u=0;u<=n;u++){
	    if(visit[u]==false){
	    //	cout<<"当前的总权值为: "<<length<<endl;
	    	if(sum>2&&length>k){    //符合要求 
	    		ans[NumToString[maxno]]=sum;   //存入 首领名-成员数 的映射 
	    //		cout<<"首领是: "<<NumToString[maxno]<<endl;
			}
			sum=0;
			length=0;     //重新初始化
			maxno=0; 
			maxlength=0;
			DFS(u); 
		}
	}
}
int main(){
	cin>>n>>k;
	string tempa,tempb;
	int temp;
	for(int i=0;i<n;i++){
		cin>>tempa>>tempb>>temp;
		int a=change(tempa);
		int b=change(tempb);
		g[a][b]=temp+g[a][b]; 
		g[b][a]=temp+g[b][a];
    }
    DFSTravel(); 
    cout<<ans.size()<<endl;
    for(map<string,int>::iterator it=ans.begin();it!=ans.end();it++){
    	cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值