1034 Head of a Gang
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.
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.
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.
- 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
- 2
- AAA 3
- GGG 3
- 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
0
总结:还是不会写T_T!不过也学到了东西,有时候可以想问题可以将字符串转化成数字(需要使用两个数组,记录字符串和数字之间的对应关系)
大佬代码:
图 dfs 遍历满足条件:①:两点之间有通路 ②目的点并未被访问
在这题的遍历中,代码写了满足这两个条件,至于为什么要分开写,和题目的问题有关,先判断当前两个点是否有通路的话,在判断该点是否被访问,这样可以计算出所有路径之和
- #include
- #include
- using namespace std;
-
- map
int> stringToint; - map<int,string> intTostring;
- map
int> ans;//人物姓名+团队人数 - int numIndex=1,n,k;
- int weight[2010],g[2010][2010];
- bool st[2010];
-
- int stringToInt(string s){
- if(stringToint[s]==0){
- stringToint[s]=numIndex;
- intTostring[numIndex]=s;
- return numIndex++;
- }
- else return stringToint[s];
- }
-
- void dfs(int u,int &head,int &numMember,int &totalweight){
- //参数的含义:u可以表示的编号为u的点,head表示的是电话时间最长的人的编号
- //numMember表示当前集合中的总人数,totalweight表示的是当前集合中的总权重
- st[u]=true;
- numMember++;
- if(weight[u]>weight[head]){
- head=u;
- }
- for(int v=1;v
- if(g[u][v]){
- totalweight+=g[u][v];
- g[u][v]=g[v][u]=0;
- if(!st[v]){
- dfs(v,head,numMember,totalweight);
- }
- }
- }
- }
-
- void dfstrave(){
- for(int i=1;i
- if(!st[i]){
- int head=i,totalweight=0,numMember=0;
- dfs(i,head,numMember,totalweight);//这里假设第一个人就是head,在后面递归的过程中不断找出head
- if(numMember>2 && totalweight>k)
- ans[intTostring[head]]=numMember;
- }
- }
- }
-
- int main(){
- cin >> n >> k;
-
- for(int i=0;i
- string a,b;
- int t;
- cin >> a >> b >> t;
- int id1=stringToInt(a);
- int id2=stringToInt(b);
- weight[id1]+=t;//算出每个人的通话时间,后面可以在dfs的时候比较出最长通话时间的人
- weight[id2]+=t;
- g[id1][id2]+=t;//在遍历的时候表示两个人之间有通路,这里必须是+=,因为可能会存在两个人之间重复通话
- g[id2][id1]+=t;
- }
-
- dfstrave();
- cout << ans.size() << endl;
- for(auto it=ans.begin();it!=ans.end();it++){
- cout << it->first << ' ' << it->second << endl;
- }
-
- return 0;
- }
好好学习,天天向上!
我要考研!
-
相关阅读:
gradle使用教程,小白一篇就够
OpenGL LUT滤镜算法解析
eKuiper Newsletter 2022-07|v1.6.0:Flow 编排 + 更好用的 SQL,轻松表达业务逻辑
SpringCloud优化记录
华为数通方向HCIP-DataCom H12-831题库(多选题:101-120)
信号类型(雷达)——连续波雷达(二)
一款跳转警告HTML单页模板源码
Springboot集成activiti,低代码整合平台,智慧审批,前端vue
VirtualLab专题实验教程-1.超表面纳米柱及其相位分析
Maven 知识点总结
-
原文地址:https://blog.csdn.net/weixin_50679551/article/details/126923749