PATA 1034 Head of a Gang(30 分)解题报告

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

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

题目大意:如果两个人之间有通话记录,那么我们就称这两个人是有联系的,他们两个人联系的权重就是通话时长,我们把一个帮派定义为,有超过两个人彼此直接有联系,且整个帮派联系的总权重超过一个给定的值K,帮派的首领定义为通话时间最久的那个人,要求你在第一行输出帮派数量,之后的每一行输出帮派首领的名称和人数

解题思路:这个题是典型的图遍历问题,求一个连通分量的结点数和总边权,然后根据K值来判断是否是一个帮派,同时我们需要记录每一个结点的点权,只要通话产生,通话的双方都应该加上通话时间作为点权。同时因为名字是string类,我们需要把他转换成int类的id来保存,同时又要根据id来输出名字,所以我们需要两个map变量,一个int to string,一个string to int ,用来保存姓名和对应数据,最后设一个gang,string  to int 变量,用于保存结果并输出。注意点是,当你遍历了一条边之后,要把这条边删除,免得出现循环遍历的情况。

 

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<cctype>
using namespace std;
const int maxn=2010;
const int INF=1000000000;
map<int,string> intToString;
map<string,int> stringToInt;
map<string,int> gang;
int G[maxn][maxn]={0},weight[maxn]={0};//图,点权 
int n,k,numperson=0;//通话记录数量,权值下限,人数 
bool vis[maxn]={false};//访问标记 
int change(string str);//string to int 函数 
void DFS(int nowvisit,int& head,int& numMember,int& totalvalue);
void DFSTrave();
int main()
{
	cin>>n>>k;
	string name1,name2;
	int time;
	for(int i=0;i<n;i++)
	{
		cin>>name1>>name2>>time;
		int id1=change(name1);//把name转为id 
		int id2=change(name2);
		//把相应的点权增加,我们视图为无向图,所以要加两次权值 
		weight[id1]+=time;
		weight[id2]+=time;
		G[id1][id2]+=time;
		G[id2][id1]+=time; 
	}
	DFSTrave();
	cout<<gang.size()<<endl;
	for(auto it=gang.begin();it!=gang.end();it++)
		cout<<it->first<<" "<<it->second<<endl; 
	return 0;
}

int change(string str)
{//恰好还能统计一整个图中恰好有多少人 
	if(stringToInt.find(str)!=stringToInt.end())//如果这个名字不是第一次出现 
		return stringToInt[str];
	else//这个名字第一次出现 
	{
		stringToInt[str]=numperson;//name to id
		intToString[numperson]=str;//id to name 
		return numperson++;	//总人数+1
	}
}

void DFS(int nowvisit,int& head,int& numMember,int& totalvalue)
{
	//head是头目,numMember是连通分量的成员数量,totalvalue是连通分量的总边权
	//注意这里使用的都是引用 
	numMember++;
	vis[nowvisit]=true;
	if(weight[nowvisit]>weight[head])
		head=nowvisit;
	for(int i=0;i<numperson;i++)//对每一个顶点 
	{
		if(G[nowvisit][i]>0)//如果能到达 
		{
			totalvalue+=G[nowvisit][i];
			G[nowvisit][i]=G[i][nowvisit]=0;//删除这条边,防止回头,无向图,所以删两个 
			if(vis[i]==false)
				DFS(i,head,numMember,totalvalue);
		}
	}
}

void DFSTrave()
{
	for(int i=0;i<numperson;i++)//对每一个顶点 
	{
		if(vis[i]==false)
		{
			int head=i,numMember=0,totalvalue=0;
			DFS(i,head,numMember,totalvalue);
			if(numMember>2&&totalvalue>k)
			{
				//intToString[head]是把对应的id转为name,放在gang这个变量中保存 
				gang[intToString[head]]=numMember;
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值