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
题意分析:
犯罪集团:2人以上//通话时间超过K
集团:有通话记录 视为一伙人----->用图解决
罪犯头头:集团中通话时间最长的人
注意点:
1.1000条通话记录可能会有2000个犯罪分子//注意数组的大小
2.建立int和犯罪分子的name之间的映射,用到map stl//这里用到了一些map的基础用法
3.基础的图的遍历//算法
#include <stdio.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
struct node{
string name;
int timetotal;
}a[2010];
//方便计算还是用map
map<int, string> InttoString;
map<string, int> StringtoInt;
map<string, int > ans;
int countNumber = 1;
//如果没有出现过//增加一个//否则返回原有的id
int ChangetoInt(string s1){
if (StringtoInt[s1] == 0){
StringtoInt[s1]=countNumber;
InttoString[countNumber] = s1;
return countNumber++;
}
else return StringtoInt[s1];
}
int n, k;
int g[2010][2010] = { 0 };
int w[2010] = { 0 };//记录自己的权重
bool inq[2010] = { false };//未被访问
void dfs(int u, int &head, int &numMember, int &totalw){
//设置已访问,成员数+1//头子更换//增加权重//访问未访问的点
inq[u] = true;
numMember++;
if (w[u]>w[head]){
head = u;
}
for (int v = 1; v < countNumber; v++){
if (g[u][v]>0){
totalw += g[u][v];
g[u][v] = g[v][u] = 0;
if (inq[v] == false){
dfs(v, head, numMember, totalw);
}
}
}
}
void dfsTrave(){
for (int i = 0; i < countNumber; i++){
if (inq[i] == false){
int head = i, numMember = 0, totalw = 0;
dfs(i, head, numMember, totalw);
if (numMember>2 && totalw > k){
ans[InttoString[head]] = numMember;
}
}
}
}
//
int main(){
scanf("%d %d", &n, &k);
string s1, s2;
int t;
int f1, f2;
for (int i = 0; i < n; i++){
cin >> s1 >> s2 >> t;
f1 = ChangetoInt(s1);
f2 = ChangetoInt(s2);
g[f1][f2] += t;
g[f2][f1] += t;
w[f1] += t;
w[f2] += t;
}
dfsTrave();
cout << ans.size() << endl;
for (auto it = ans.begin(); it != ans.end(); it++){
cout << it->first << " " << it->second << endl;
}
return 0;
}