1034. Head of a Gang (30)
2015-01-30 22:20
190 查看
题目:
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:
Sample Output 1:
Sample Input 2:
Sample Output 2:
注意:
1、DFS求解连通区域。
2、由于每个人是以名称作为标识,在索引时会很麻烦,所以建立一个map将其离散化(我这里较为麻烦,是根据出现的次序来给其int标识的,也可以直接使用26进制转换成int类型)。
3、注意在判断时间是否符合时,总时间要先除以2.
4、啊,回过头来看看觉得确实写得好复杂T^T
代码:
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、DFS求解连通区域。
2、由于每个人是以名称作为标识,在索引时会很麻烦,所以建立一个map将其离散化(我这里较为麻烦,是根据出现的次序来给其int标识的,也可以直接使用26进制转换成int类型)。
3、注意在判断时间是否符合时,总时间要先除以2.
4、啊,回过头来看看觉得确实写得好复杂T^T
代码:
#include<iostream> #include<vector> #include<map> #include<string> #include<algorithm> using namespace std; struct person { int name;//name index vector< int>connected; int time;//phone call time bool operator < (const person &p) const { return time<p.time; } }; struct gang { string head; //head name int num;//number of persons bool operator < (const gang &g) const { return head<g.head; } }; vector<person>allperson; //list of all persons vector<gang>allgang; //list of all gangs vector<person>onegang; //list of a gang int gangtime;//total time of a gang map<string,int>name2num; //map of name and index number map<int,string>num2name; //map of index number and name //n:the number of phone calls //k:the weight threthold int n,k; int index=0;//the index of each person vector<int>visited; void dfs(int n) { visited =1; for(int i=0;i<allperson .connected.size();++i) if(!visited[allperson .connected[i]]) { gangtime += allperson[allperson .connected[i]].time; onegang.push_back(allperson[allperson .connected[i]]); dfs(allperson .connected[i]); } } int main() { cin>>n>>k; for(int i=0;i<n;++i) { string a,b; int tim; cin>>a>>b>>tim; if(!name2num.count(a)) { //if a is not exist in the name list person p; p.time=0; p.name=index; allperson.push_back(p); name2num.insert(pair<string, int>(a,index)); num2name.insert(pair< int,string>(index++,a)); } if(!name2num.count(b)) { //if b is not exist in the name list person p; p.time=0; p.name=index; allperson.push_back(p); name2num.insert(pair<string, int>(b,index)); num2name.insert(pair< int,string>(index++,b)); } allperson[name2num[a]].connected.push_back(name2num[b]); allperson[name2num[b]].connected.push_back(name2num[a]); allperson[name2num[a]].time += tim; allperson[name2num[b]].time += tim; } visited.resize(index,0); for(int i=0;i<index;++i) { if(!visited[i]) { onegang.clear(); gangtime=allperson[i].time; onegang.push_back(allperson[i]); dfs(i); if(onegang.size()>=3 && gangtime/2>k) { // gangtime/2 because gangtime has count twice time of each person sort(onegang.begin(),onegang.end()); gang g; g.num=onegang.size(); g.head=num2name[onegang[g.num-1].name]; allgang.push_back(g); } } } sort(allgang.begin(),allgang.end()); cout<<allgang.size()<<endl; for(int i=0;i<allgang.size();++i) cout<<allgang[i].head<< ' '<<allgang[i].num<<endl; return 0; }
相关文章推荐
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT_A 1034. Head of a Gang (30)
- PAT (Advanced Level)1034. Head of a Gang (30) map的灵活运用
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)【离散化+并查集+搜索】——PAT (Advanced Level) Practise
- 1034. Head of a Gang (30)
- PTA 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)
- 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30) DFS,删边
- 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)
- 1034. Head of a Gang (30)