题目
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.
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:
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.
1
2
3
4
5
6
7
8
9
|
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:
1
2
3
4
5
6
7
8
9
|
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:
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<int,string> intToStr;
map<string,int> strToInt;
map<string,int> gang;
int person=0;
int G[2005][2005]={0};
int head[2005]={0}; //点权和
bool vis[2005]={false};
void dfs(int nowPerson,int &headId,int &pSum,int &totalTime)
{
pSum++;
vis[nowPerson]=true;
if(head[nowPerson]>head[headId])
headId=nowPerson;
for(int i=0;i<person;i++)
{
if(G[nowPerson][i]>0)
{
totalTime+=G[nowPerson][i];
G[nowPerson][i]=G[i][nowPerson]=0;
if(vis[i]==false)
dfs(i,headId,pSum,totalTime);
}
}
}
int getId(string str)
{
if(strToInt.find(str)!=strToInt.end())
return strToInt[str];
else
{
strToInt[str]=person;
intToStr[person]=str;
person++;
return strToInt[str];
}
}
int main()
{
int n,t,weight,inta,intb;
string a,b;
cin >> n >>t;
for(int i=0;i<n;i++)
{
cin >> a>>b>>weight;
inta=getId(a);
intb=getId(b);
// cout << "ida:" << inta << "\tidb:" <<intb<<endl;
G[inta][intb]+=weight;
G[intb][inta]+=weight;
// cout << G[inta][intb] << G[intb][inta] <<endl;
head[inta]+=weight;
head[intb]+=weight;
// cout << head[inta] << head[intb] <<endl;
}
for(int i=0;i<person;i++)
{
if(vis[i]==false)
{
int nowPerson=i,headId=i,pSum=0,totalTime=0;
dfs(nowPerson,headId,pSum,totalTime);
if(pSum>2&&totalTime>t)
gang[intToStr[headId]]=pSum;
}
}
cout << gang.size() << endl;
map<string,int>::iterator it;
for(it=gang.begin();it!=gang.end();it++)
cout << it->first << " "<< it->second<<endl;
return 0;
}
|
思路
将有向图转为无向图,进行深度优先搜索。
- map中元素自动按键从小到大排序
- 图中可能有环,如第一个例子中的FGH,因此在累加完这条边权后需要将此边删除