A1034. Head of a Gang (30)

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 <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <string>
 8 #include <stack> 
 9 #include <queue>
10 #include <vector>
11 #include <map>
12 using namespace std;
13 const int maxn=2010;
14 const int INF=1000000000;
15 
16 map<string,int> sToi;
17 map<int,string> iTos;
18 map<string,int> gang;//按字母顺序排序 
19 
20 int G[maxn][maxn]={0},weight[maxn]={0};
21 int n,k,numPerson;
22 bool vis[maxn]={false};
23 
24 int change(string s)
25 {
26     if(sToi.find(s)!=sToi.end())
27     {
28         return sToi[s];
29     }else
30     {
31         sToi[s]=numPerson;
32         iTos[numPerson]=s;
33         return numPerson++;
34     }
35 }
36 void DFS(int now,int &head,int &numM,int &totalv)
37 {
38     numM++;
39     vis[now]=true;
40     if(weight[now]>weight[head])
41     {
42         head=now;
43     }
44     for(int i=0;i<numPerson;i++)
45     {
46         if(G[now][i]>0)
47         {
48             totalv+=G[now][i];
49             G[now][i]=G[i][now]=0;
50             if(vis[i]==false)
51             {
52                 DFS(i,head,numM,totalv);
53             }
54         }
55     }
56 }
57 void DFSTra()
58 {
59   for(int i=0;i<numPerson;i++)
60   {
61    if(vis[i]==false)
62    {
63     int head=i,numMember=0,totalvalue=0;
64     DFS(i,head,numMember,totalvalue);
65     if(totalvalue>k&&numMember>2)
66     {
67         gang[iTos[head]]=numMember;
68     }    
69         
70    }     
71   }    
72 }
73 
74 int main(){
75   string s1,s2;
76   cin>>n>>k;
77   for(int i=0;i<n;i++)
78   {
79       int w;
80       cin>>s1>>s2>>w;
81       //将s1 s2转换成数字
82     int id1=change(s1);
83     int id2=change(s2);
84      G[id1][id2]+=w;//這裡必須是+=,否则不能输入重复边,导致样                      //例不过
85      G[id2][id1]+=w;
86      weight[id1]+=w;
87      weight[id2]+=w;  
88   }
89   DFSTra();
90   printf("%d\n",gang.size());
91   map<string,int>::iterator it;
92   for(it=gang.begin();it!=gang.end();it++)
93   {
94       cout<<it->first<<" "<<it->second<<endl;
95   }
96   return 0;
97 }

 

posted @ 2015-03-11 23:49  Joilee  阅读(155)  评论(0编辑  收藏  举报