PAT甲级刷题记录——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 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 Name1and Name2are the names of people at the two ends of the call, and Timeis 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

思路

这题的题目有点绕,我参考的也是晴神写的代码,用DFS对图进行遍历来做。

题目的大致意思是这样的:给一些两个人之间的通话记录,如果某个团伙人数大于2,且通话记录大于给出的阈值K,那么就认定这个团伙是犯罪团伙,于是就要输出它的头目名字(头目是参与通话时间最长的那个),和那个团伙的人员数。

这题的话建议还是用邻接矩阵来存储,因为双向删除一条边的操作,邻接表的话只能一个一个访问过去,邻接矩阵可以直接置0。

然后就是几个比较重要的点:
①姓名编号之间的转换:用两个map,写一个change()函数,这个函数建议记下来,因为之前做过的那题【1087 All Roads Lead to Rome (30分)】也需要这么做,否则不好操作;
②在进行dfs()时,应该先将团伙人员数++,并且如果当前点权更大的话,就要更新头目,然后再寻找可达的那些点,进行累加边权和删除边的操作,最后再根据是否已访问来确定要不要对可达点进行dfs;
③在dfstrave()(对图的遍历)时,每次应当在dfs前重置头目、团伙人员数和团伙总边权量,因为在这里选择进行dfs的话,必然是进入一个新的连通块进行搜索,所以要重置这些量。然后搜索完了之后,要进行判断,如果团伙人员数>2并且团伙总边权量>K,那么这是一个犯罪团伙,于是,犯罪团伙数量numGang++,然后把相应的头目放到map里作为key,把相应的团伙人员数也放到map里作为value。

代码

#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
const int maxn = 2010;
const int INF = 123123123;
bool vis[maxn] = {false};
int weight[maxn] = {0};
int G[maxn][maxn] = {0};
map<int, string> IntToString;
map<string, int> StringToInt;
map<string, int> GangNumber;
int nameperson = 0, numGang = 0;
int change(string str){
    if(StringToInt.find(str)!=StringToInt.end()) return StringToInt[str];
    else{
        StringToInt[str] = nameperson;
        IntToString[nameperson] = str;
        return nameperson++;
    }
}
void dfs(int u, int &head, int &numMember, int &totalValue){
    vis[u] = true;
    numMember++;
    if(weight[u]>weight[head]) head = u;
    for(int i=0;i<nameperson;i++){
        if(G[u][i]>0){
            totalValue += G[u][i];
            G[u][i] = G[i][u] = 0;
            if(vis[i]==false) dfs(i, head, numMember, totalValue);
        }
    }
}
void dfstrave(int k){
    for(int i=0;i<nameperson;i++){
        if(vis[i]==false){
            int head = i, numMember = 0, totalValue = 0;
            //每次对一个新的犯罪集团(连通块)搜的时候要重置
            dfs(i, head, numMember, totalValue);
            if(numMember>2&&totalValue>k){//搜完之后就得到了该犯罪集团的头目
                numGang++;//犯罪集团的数量++
                GangNumber[IntToString[head]] = numMember;
            }
        }
    }
}
int main(){
    int N, K;
    cin>>N>>K;
    for(int i=0;i<N;i++){
        string tmpa, tmpb;
        int tmp;
        cin>>tmpa>>tmpb>>tmp;
        int idA = change(tmpa);
        int idB = change(tmpb);
        G[idA][idB] += tmp;//加边权(边权是idA和idB两个人的通话时长)
        G[idB][idA] += tmp;
        weight[idA] += tmp;//加点权(点权是每个人参与的通话时长)
        weight[idB] += tmp;
    }
    dfstrave(K);
    cout<<numGang<<'\n';
    for(map<string, int>::iterator it = GangNumber.begin();it!=GangNumber.end();it++){
        cout<<it->first<<" "<<it->second<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值