码迷,mamicode.com
首页 > 其他好文 > 详细

1034 Head of a Gang (30 分)

时间:2019-02-21 23:18:35      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:time   travel   wing   hit   put   map   view   als   lines   

1034 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 Aand 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 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 /**
 2 * Copyright(c)
 3 * All rights reserved.
 4 * Author : Mered1th
 5 * Date : 2019-02-21-21.05.03
 6 * Description : A1034
 7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=2010;
20 int G[maxn][maxn]={0},weight[maxn]={0};
21 bool vis[maxn]={false};
22 map<string,int> stringToInt;
23 map<int,string> intToString;
24 map<string,int> Gang; //Gang的人数
25 int numGang=0,numPerson=0;
26 int n,th;
27 int change(string str){
28     if(stringToInt.find(str)!=stringToInt.end()){
29         return stringToInt[str];
30     }
31     else{
32         stringToInt[str]=numPerson;
33         intToString[numPerson]=str;
34         return numPerson++;
35     }
36 }
37 
38 void DFS(int index,int& head,int& numMember,int& totalValue){
39     numMember++;
40     vis[index]=true;
41     if(weight[index]>weight[head]){
42         head=index;
43     }
44     for(int i=0;i<numPerson;i++){
45         if(G[index][i]>0){
46             totalValue+=G[index][i];
47             G[index][i]=G[i][index]=0;  //遍历过后把该边删除
48             if(vis[i]==false) DFS(i,head,numMember,totalValue);
49         }
50     }
51 }
52 
53 void DFSTravel(){
54     for(int i=0;i<numPerson;i++){
55         if(vis[i]==false){
56             int head=i,numMember=0,totalValue=0;
57             DFS(i, head, numMember, totalValue);
58             if(numMember > 2&& totalValue > th){
59                 Gang[intToString[head]]=numMember;
60             }
61         }
62     }
63 }
64 
65 int main(){
66 #ifdef ONLINE_JUDGE
67 #else
68     freopen("1.txt", "r", stdin);
69 #endif
70     int w;
71     string str1,str2;
72     scanf("%d%d",&n,&th);
73     for(int i=0;i<n;i++){
74         cin>>str1>>str2>>w;
75         int id1=change(str1);
76         int id2=change(str2);
77         weight[id1]+=w;
78         weight[id2]+=w;
79         G[id1][id2]+=w;
80         G[id2][id1]+=w;
81     }
82     DFSTravel();
83     cout<<Gang.size()<<endl;
84     for(auto it =Gang.begin();it!=Gang.end();it++){
85         cout<<it->first<<" "<<it->second<<endl;
86     }
87     return 0;
88 }

 

 
 

1034 Head of a Gang (30 分)

标签:time   travel   wing   hit   put   map   view   als   lines   

原文地址:https://www.cnblogs.com/Mered1th/p/10415767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!