PAT A 1034. Head of a Gang (30)
2014-05-15 12:50
260 查看
题目
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
即求图中,点的数量(成员)和权重(通话时间)都大于限定的所有连通域中权值(通话时间)最大的点。
借助map对成员编号、获得图,然后bfs\dfs探测连通域,求这个连通域的相关信息,暂存符合条件的最大的点,排序输出
代码:
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
即求图中,点的数量(成员)和权重(通话时间)都大于限定的所有连通域中权值(通话时间)最大的点。
借助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为其独立的id int 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; }
相关文章推荐
- PAT A1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT甲级刷题
- 1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- PTA 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- pat 甲级 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT甲题题解-1034. Head of a Gang (30)-并查集
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT (Advanced Level) 1034. Head of a Gang (30)
- 1034. Head of a Gang (30) (并查集 & map)
- PAT_A 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT甲级 1034. Head of a Gang (30)