博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT_A1034#Head of a Gang
阅读量:7153 次
发布时间:2019-06-29

本文共 3568 字,大约阅读时间需要 11 分钟。

Source:

Description:

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 Nand 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

Keys:

  • 深度优先搜索(Depth First Search)
  • map(C++ STL)

Attention:

  • 给出N条边,最多可能有2*N个结点;
  • 边权累加的操作如果放在 if 里面的话,图中如果回路,会少数一条边;
  • 边权累加的操作如果放在 if 外面的话,相当于各个边数了两次,结果除以2就可以了;
  • 边权累加的操作正常应该放在 if(grap[u][v]!=INF)的里面,不过这题边权初始化为0,加上无效边不影响结果;
  • 输入的时候预处理各个结点的边权之和,是个小的tips,可以注意一下;

Code:

1 /*  2 Data: 2019-04-14 19:46:23  3 Problem: PAT_A1034#Head of a Gang  4 AC: 61:22  5   6 题目大意:  7 A和B有通话总时长表示他们的联系程度,  8 如果一撮人通话总时长超过某一阈值,则认定为“犯罪团伙”,其中电话打的最多的就是头目;  9 输入: 10 第一行给出通话数目N和阈值K(<=1e3) 11 接下来N行给出 v1 v2 w,其中V用三个大写字母表示,w<=1e3 12 输出: 13 第一行输出团伙数目 14 接下来按照字典序依次输出各个团伙的头目及其人数 15  16 基本思路: 17 预处理各顶点边权之和, 18 遍历各个连通分量,统计结点数目,边权之和,犯罪头目 19 map存储犯罪头目及其人数,达到字典序排序的目的 20 */ 21 #include
22 #include
23 #include
24 #include
25 #include
26 using namespace std; 27 const int M=2e3+10; 28 int grap[M][M],vis[M],cost[M],pt=1; 29 int K,k,sum,Max; 30 map
mp,gang; 31 string toS[M],head; 32 33 int ToN(string s) 34 { 35 if(mp[s]==0) 36 { 37 mp[s]=pt; 38 toS[pt]=s; 39 return pt++; 40 } 41 else 42 return mp[s]; 43 } 44 45 void DFS(int u) 46 { 47 vis[u]=1; 48 sum++; 49 if(cost[u] > Max) 50 { 51 Max = cost[u]; 52 head = toS[u]; 53 } 54 for(int v=1; v
K*2 && sum>2) 81 { 82 cnt++; 83 gang[head]=sum; 84 } 85 } 86 } 87 return cnt; 88 } 89 90 int main() 91 { 92 #ifdef ONLINE_JUDGE 93 #else 94 freopen("Test.txt", "r", stdin); 95 #endif // ONLINE_JUDGE 96 97 int n; 98 scanf("%d%d", &n,&K); 99 fill(grap[0],grap[0]+M*M,0);100 fill(cost,cost+M,0);101 for(int i=0; i
> s1 >> s2 >> w;106 v1 = ToN(s1);107 v2 = ToN(s2);108 grap[v1][v2]+=w;109 grap[v2][v1]+=w;110 cost[v1] += w;111 cost[v2] += w;112 }113 int cnt=Travel();114 printf("%d\n", cnt);115 for(auto it=gang.begin(); it!=gang.end(); it++)116 cout << it->first << " " << it->second << endl;117 118 return 0;119 }

 

转载于:https://www.cnblogs.com/blue-lin/p/10887558.html

你可能感兴趣的文章
android View 绘制完成监听
查看>>
igbinary vs serialize vs json_encode
查看>>
Snipaste强大离线/在线截屏软件的下载、安装和使用
查看>>
禅与摩托车维修的艺术
查看>>
elasticsearch term match multi_match区别
查看>>
前端学PHP之PDO预处理语句
查看>>
webservice理解
查看>>
TestNG的简单使用
查看>>
数组可以容纳多少水----------给你出道题
查看>>
Linux之sar命令介绍
查看>>
飘逸的python - 增强的格式化字符串format函数
查看>>
纯html页面之间传参
查看>>
linux 如何查找io的进程
查看>>
AE控制图层中要素可见状态的几种方法
查看>>
Nginx之http_image_filter_module模块使用
查看>>
重温设计模式
查看>>
js dorado
查看>>
C#枚举
查看>>
ORACLE11g中毒恢复
查看>>
Maven核心概念之仓库,生命周期与插件
查看>>