1034 Head of a Gang (30分)(dfs 利用map)

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

题目分析:刚开始想怎么才能利用字符串来表示图 觉得得使用map或者类似哈希函数将字符转化为数字 但一直想不到什么好方法 看了柳神的博客后...哇 还可以这样
具体就是利用 两个map来存节点与对应的值 其实也就是将每一个值对应为一个数字 这种想法感觉是哈希函数的一种实现
最后利用dfs来遍历图 找到需要的解
注意的是对于每条存在的边都需要纳入计算 而每个节点只需要且只能遍历一次
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <climits>
 3 #include<iostream>
 4 #include<vector>
 5 #include<queue>
 6 #include<map>
 7 #include<stack>
 8 #include<algorithm>
 9 #include<string>
10 #include<cmath>
11 using namespace std;
12 map<string, int>stringtoint;
13 map<int, string>inttostring;
14 map<string, int>ans;
15 int N, K;
16 int number = 1;
17 int G[2020][2020];
18 int weight[2020];
19 int Collected[2020];
20 int STOI(string s)
21 {
22     if (stringtoint[s] == 0)
23     {
24         stringtoint[s] = number;
25         inttostring[number] = s;
26         return number++;
27     }
28     else
29         return stringtoint[s];
30 }
31 void dfs(int u, int& head, int& totalnumber, int& totalweight)
32 {
33     Collected[u] = 1;
34     totalnumber++;
35     if (weight[head] < weight[u])
36         head = u;
37     for (int v = 1; v < number; v++)
38     {
39         if (G[u][v])
40         {
41             totalweight += G[u][v];
42             G[u][v] = G[v][u] = 0;
43             if (!Collected[v])
44                 dfs(v, head, totalnumber, totalweight);
45         }
46     }
47 }
48 int main()
49 {
50     cin >> N >> K;
51     string s1, s2;
52     int w;
53     for (int i = 0; i < N; i++)
54     {
55         cin >> s1 >> s2 >> w;
56         G[STOI(s1)][STOI(s2)]+=w;
57         G[STOI(s2)][STOI(s1)]+=w;
58         weight[STOI(s1)] += w;
59         weight[STOI(s2)] += w;
60     }
61     for (int i = 1; i < number; i++)
62     {
63         int head = i;
64         int totalnumber = 0;
65         int totalweight = 0;
66         if (!Collected[i])
67             dfs(i, head, totalnumber, totalweight);
68         if (totalnumber >2 && totalweight > K)
69             ans[inttostring[head]] = totalnumber;
70     }
71     cout << ans.size() << endl;
72     for (auto it : ans)
73         cout << it.first << " " << it.second << endl;
74     return 0;
75 }
View Code
 

猜你喜欢

转载自www.cnblogs.com/57one/p/12013480.html