• 1034 Head of a Gang


    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:

    1. 8 59
    2. AAA BBB 10
    3. BBB AAA 20
    4. AAA CCC 40
    5. DDD EEE 5
    6. EEE DDD 70
    7. FFF GGG 30
    8. GGG HHH 20
    9. HHH FFF 10

    Sample Output 1:

    1. 2
    2. AAA 3
    3. GGG 3

    Sample Input 2:

    1. 8 70
    2. AAA BBB 10
    3. BBB AAA 20
    4. AAA CCC 40
    5. DDD EEE 5
    6. EEE DDD 70
    7. FFF GGG 30
    8. GGG HHH 20
    9. HHH FFF 10

    Sample Output 2:

    0

    题目大意

    给你一个无向图,假设可以互相抵达的点为一个集群;每个点的权重为其所连边长总和,求权重总和超过limit且点数大于二的集群数量,以及每个集群中,权重最高的那个点


    思路

    BFS搜索每一个未被搜索过的点


    C/C++ 

    1. #include
    2. using namespace std;
    3. map> nums; // 互相有关系的人
    4. mapint> keys; // 该人通话的总权值
    5. mapbool> apr; // 判断该人是否查询过
    6. set all;
    7. int N,limit,key,number;
    8. string a,b,idKey;
    9. int findUnion(const string& now);
    10. int main()
    11. {
    12. cin >> N >> limit;
    13. while (N--){
    14. cin >> a >> b >> key;
    15. keys[a]+=key;
    16. keys[b]+=key;
    17. nums[a].push_back(b);
    18. nums[b].push_back(a);
    19. all.insert(a);
    20. all.insert(b);
    21. }
    22. limit*=2;
    23. setint>> result;
    24. for(const string& x:all){
    25. if(apr[x]) continue;
    26. idKey = x;
    27. number = 0;
    28. int flag = findUnion(x);
    29. // cout << flag << endl;
    30. if(flag>limit && number>2) result.insert({idKey,number});
    31. }
    32. cout << result.size() << endl;
    33. for(const auto& x:result) cout << x.first << " " << x.second << endl;
    34. return 0;
    35. }
    36. int findUnion(const string& now){
    37. if(apr[now]) return 0;
    38. apr[now] = true;
    39. number++;
    40. if(keys[now]>keys[idKey]) idKey = now;
    41. int sum = keys[now];
    42. for(const string& x:nums[now]) sum += findUnion(x);
    43. return sum;
    44. }


  • 相关阅读:
    软件测试面试题 —— 整理与解析(5)
    iOS越狱检测总结
    交换瓶子问题(暴力求解 + 图论解法)
    各类经典VRP,车间调度问题,组合优化问题基准测试集Benchmark
    如何使用Flask request对象处理请求
    AIGC , 超级热点 or 程序员创富新起点?
    Vulnhub: Ragnar Lothbrok: 1靶机
    11、电路综合-集总参数电路结构的S参数模型计算与Matlab
    python 基于 Web3.py 和 Infura 网关采集链上数据
    c# wpf template ItemsPanel 简单试验
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127732177