1034. Head of a Gang (30)

原文链接: http://www.cnblogs.com/zzandliz/p/5023126.html
注意这里n是电话数,电话数不超过1000代表人数不超过两千,。。。好坑。。。。


时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 

 
    
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #include<map>
  5. #include<queue>
  6. #include<string>
  7. using namespace std;
  8. struct Gang {
  9. int num;
  10. int head;
  11. };
  12. int n, k;
  13. int net[2010][2010] = { 0 };
  14. int mark[2010] = { 0 }, markOnce[2010] = { 0 };
  15. int phoneTime[2010] = { 0 };
  16. map<string, int> mp;
  17. vector<Gang> gang;
  18. int nameIndex = 0;
  19. queue<int> dfsQ;
  20. int Name2Int(string s) {
  21. if (mp[s] == 0) {
  22. nameIndex++;
  23. mp[s] = nameIndex;
  24. return nameIndex;
  25. }
  26. else {
  27. return mp[s];
  28. }
  29. }
  30. string Int2Name(int i) {
  31. for (auto &k : mp) {
  32. if (k.second == i) {
  33. return k.first;
  34. }
  35. }
  36. return "";
  37. }
  38. bool cmp(Gang a,Gang b) {
  39. string a1 = Int2Name(a.head);
  40. string b1 = Int2Name(b.head);
  41. return a1 < b1;
  42. }
  43. void dfs(int i) {
  44. dfsQ.pop();
  45. mark[i] = 1;
  46. markOnce[i] = 1;
  47. for (int j = 1; j <= nameIndex; j++) {
  48. if (net[i][j] > 0 && mark[j] == 0) {
  49. //dfs(j);
  50. dfsQ.push(j);
  51. }
  52. }
  53. if (dfsQ.empty() == false)
  54. dfs(dfsQ.front());
  55. }
  56. int main(void) {
  57. cin >> n >> k;
  58. for (int i = 0; i < n; i++) {
  59. string s1, s2;
  60. int time;
  61. cin >> s1 >> s2 >> time;
  62. phoneTime[Name2Int(s1)] += time;
  63. phoneTime[Name2Int(s2)] += time;
  64. net[Name2Int(s1)][Name2Int(s2)] = 1;
  65. net[Name2Int(s2)][Name2Int(s1)] = 1;
  66. }
  67. for (int i = 1; i <= nameIndex; i++) {
  68. if (mark[i] == 0) {
  69. for (int j = 1; j <= nameIndex; j++)
  70. markOnce[j] = 0;
  71. dfsQ.push(i);
  72. dfs(i);
  73. }
  74. else {
  75. continue;
  76. }
  77. int cnt = 0;
  78. int max = 0, maxIndex = 0;
  79. int clusterPhone = 0;
  80. for (int j = 1; j <= nameIndex; j++) {
  81. if (markOnce[j] > 0) {
  82. cnt++;
  83. clusterPhone += phoneTime[j];
  84. if (phoneTime[j] > max) {
  85. maxIndex = j;
  86. max = phoneTime[j];
  87. }
  88. }
  89. }
  90. clusterPhone /= 2;
  91. if (cnt > 2 && clusterPhone > k) {
  92. Gang temp;
  93. temp.head = maxIndex;
  94. temp.num = cnt;
  95. gang.push_back(temp);
  96. }
  97. }
  98. sort(gang.begin(), gang.end(), cmp);
  99. cout << gang.size() << endl;
  100. for (int i = 0; i < gang.size(); i++) {
  101. string name = Int2Name(gang[i].head);
  102. cout << name << " " << gang[i].num << endl;
  103. }
  104. return 0;
  105. }





转载于:https://www.cnblogs.com/zzandliz/p/5023126.html

猜你喜欢

转载自blog.csdn.net/weixin_30399155/article/details/94785173