PAT A 1034. Head of a Gang (30)

来源:互联网 发布:ddg1000数据 编辑:程序博客网 时间:2024/05/01 00: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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10

Sample Output 1:

2AAA 3GGG 3

Sample Input 2:

8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10

Sample Output 2:

0

 

即求图中,点的数量(成员)和权重(通话时间)都大于限定的所有连通域中权值(通话时间)最大的点。

借助map对成员编号、获得图,然后bfs\dfs探测连通域,求这个连通域的相关信息,暂存符合条件的最大的点,排序输出

 

代码:

#include <iostream>#include <string>#include <queue>#include <map>#include <algorithm>using namespace std;struct Member//用于存储成员信息{string name;//名字int time;//总时长,(统计head时是成员数)};bool cm(const Member& g1,const Member& g2);//根据名字排序int main(){int n,k;Member *member;//记录所有的成员信息int **time;//记录各个成员间的总通话时间,图map<string,int> name;//!!!用于保证成员间名字的唯一性,并保证快速按名字查找,int为其独立的idint i,j;cin>>n>>k;//输入基本信息member=new Member[2*n+2];//初始化time=new int* [2*n+2];for(i=0;i<2*n+2;i++){member[i].time=0;time[i]=new int [2*n+2];for(j=0;j<2*n+2;j++)time[i][j]=0;}int id=0;//指示成员数量,及标号int temptime;string name1,name2;for(i=0;i<n;i++)//输入信息{cin>>name1>>name2>>temptime;if(!name[name1])name[name1]=++id;if(!name[name2])name[name2]=++id;time[name[name1]][name[name2]]+=temptime;//刷新图time[name[name2]][name[name1]]+=temptime;member[name[name1]].name=name1;//刷新成员信息member[name[name1]].time+=temptime;member[name[name2]].name=name2;member[name[name2]].time+=temptime;}int gang_num=0;//帮派数量int head,membs,times;//head,成员数,总通话时间Member *gang=new Member [id+1];//帮派信息int *flag=new int [id+1];//标志向量,标志成员是否被探测过for(i=1;i<=id;i++)flag[i]=0;queue<int> gang_test;//bfs用结构for(i=1;i<=id;i++)//循环探测{if(flag[i]==0)//有没有探测到的,开新树{head=i;membs=0;times=0;gang_test.push(i);flag[i]=1;while(!gang_test.empty())//bfs{membs++;times+=member[gang_test.front()].time;if(member[gang_test.front()].time>member[head].time)head=gang_test.front();for(j=1;j<=id;j++){if(time[gang_test.front()][j]!=0&&flag[j]==0){gang_test.push(j);flag[j]=1;}}gang_test.pop();}if(membs>2&×/2>k)//保存符合条件的帮派信息{gang[gang_num].name=member[head].name;gang[gang_num++].time=membs;}}}if(gang_num==0)//输出cout<<0;else{cout<<gang_num<<endl;sort(gang,gang+gang_num,cm);for(i=0;i<gang_num;i++)cout<<gang[i].name<<" "<<gang[i].time<<endl;}delete [] gang;delete [] flag;delete [] member;for(i=0;i<2*n+2;i++)delete [] time[i];delete [] time;return 0;}bool cm(const Member& g1,const Member& g2){return g1.name<g2.name;}


 

 

 

 

 

0 0
原创粉丝点击