PAT甲级1034
题目:
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
题目大意:给出n行电话记录,只要A打过电话给B或者B打过电话给A,这两个人就算一个圈子里,若一个圈子里的人电话记录里的总时间超过k并且人数不少于3个则属于一个犯罪团伙,找出总共几个团体,并且输出团队首领(通话总时长最多的那个)
这题属于图的遍历,查找连通图的个数,邻接表邻接图都可,柳神用的图,所以自己手写了个表的遍历。团队数组最高不会超过334,按字典序从小到大排,主要都在DFS里,设置visit数组,访问过则设为true,这里要注意DFS遍历时候无论有没有访问过都要置为true且不能提前跳出,因为要有个全局变量记录总电话时长以便比较是否符合犯罪团伙要求,如果提前跳出一定会有边少算。还有就是字符串转int用map即可,最大数组容量不能小于2000
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#define MAXN 2002
using namespace std;
struct gang{
string head;
int number=0;
}g[340];
bool cmp(gang a,gang b){
return a.head<b.head;
}
struct node{
int next;
int length;
node(int _next,int _length){
next=_next,length=_length;
}
};
vector<node> adjv[MAXN];
int length[MAXN];
bool visit[MAXN]={false};
map<string,int> stringtoint;
map<int,string> inttostring;
int totalmember,totaltime,maxtime;
string head;
void DFS(int k){
visit[k]=true; //无论有没有访问过都要设为true
if(0==adjv[k].size()) return ; //若k节点没有边了则返回
for(int i=0;i<adjv[k].size();i++){
if(!visit[adjv[k][i].next]){ //k的第i条边未访问
visit[adjv[k][i].next]=true;
totalmember++; //第一次碰到总人数加1
if(length[adjv[k][i].next]>maxtime){ //确定是否是头目
maxtime=length[adjv[k][i].next];
head=inttostring[adjv[k][i].next];
}
DFS(adjv[k][i].next); //继续往下查找
}
totaltime+=adjv[k][i].length; //这里总时间打电话和接电话都要算
}
return ;
}
int main(){
int n,k,tmp,cnt=0,total=1;
string name1,name2;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
cin>>name1>>name2;
scanf("%d",&tmp);
if(stringtoint[name1]==0){
stringtoint[name1]=total;
inttostring[total]=name1;
total++;
}
if(stringtoint[name2]==0){
stringtoint[name2]=total;
inttostring[total]=name2;
total++;
}
adjv[stringtoint[name1]].push_back(node(stringtoint[name2],tmp));
length[stringtoint[name1]]+=tmp;
length[stringtoint[name2]]+=tmp;
}
for(int i=0;i<MAXN;i++){
if(adjv[i].size()!=0){
if(!visit[i]){
totalmember=1,totaltime=0;
maxtime=length[i],head=inttostring[i];
DFS(i);
if(totalmember>2&&totaltime>k){
g[cnt].head=head;
g[cnt].number=totalmember;
cnt++;
}
}
}
}
sort(g,g+cnt,cmp);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++){
cout<<g[i].head<<" "<<g[i].number<<endl;
}
system("pause");
return 0;
}