1034 Head of a Gang (30 分)--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 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

题目大意:给定一些人之间的通话记录,要求找出黑帮老大及人数。条件是所有有联系的人的总通话时间大于门限K,总人数大于2,老大是其中通话时间最长的。
可以看成一个图,就是求各个连通域,只要该连通域中点数超过2,总边权大于K即可。结果可以用map<string,int>来表示头目名字到人数的映射,map已自动排序,可直接输出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<unordered_map>
using namespace std;
const int inf = 0x3fffffff;
const int maxn = 2010;
int cnt=0;
int n,k;
bool visit[maxn];
int call[maxn][maxn];//通话记录图,边权
int w[maxn];//每个人总通话时间,点权
unordered_map<int,string>mp2;//编号->名字
unordered_map<string,int>mp1;//名字->编号
map<string,int>res;//老大名字->人数
int maxw=0;//最大点权
int head=0;//老大编号
int num=0;//总人数
int sum=0;//总通话时间
void dfs(int root){
  //  cout<<mp2[root]<<" ";
    num++;
    visit[root]=true;
    if(w[root] > maxw){
        head=root;
        maxw=w[root];
    }
    for(int i=1;i<=cnt;i++){
        if(call[root][i]!=0){
            sum+=call[root][i];
            call[root][i]=call[i][root]=0;
            if(visit[i]==false) dfs(i);
        }
    }
}

int main()
{
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++){
        char a[4],b[4];
        int t;
        scanf("%s %s %d",a,b,&t);
        int x,y;
        if(mp1.find(a)==mp1.end()){
            cnt++;
            mp1[a]=cnt;
            mp2[cnt]=a;
            x=cnt;
        }else x=mp1[a];
        if(mp1.find(b)==mp1.end()){
            cnt++;
            mp1[b]=cnt;
            mp2[cnt]=b;
            y=cnt;
        }else y=mp1[b];
        w[x]+=t;
        w[y]+=t;
        call[x][y]+=t;
        call[y][x]+=t;
    }
    for(int i=1;i<=cnt;i++){
        maxw=0;
        head=0;
        num=0;
        sum=0;
        if(visit[i]==false) dfs(i);
    //    printf("%d %d %d %d\n",head,num,maxw,sum);
        if(num>2&&sum>k){
            res[mp2[head]]=num;
        }
    }
    printf("%d\n",res.size());
    for(map<string,int>::iterator it=res.begin();it!=res.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值