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 A A and B B B, we say that A A A and B B 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 K 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 N N and K K K (both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. Then N N 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已自动排序,可直接输出。

技巧

  1. 节点是字符串,可以用两个map<string,int>map<int,string>进行转换;
  2. 统计点权可以在数如的时候进行统计,即将所有点的点权都统计出来;
  3. 统计边权有一个地方要注意,由于图中有环,按照一般的深度优遍历可能会漏掉一条边,处理的办法在代码中解释;

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

const int maxn = 2001;
const int INF = 0x7fffffff;

int n, k, cnt=0;  // cnt用于给每个点编号
bool visited[maxn];
int graph[maxn][maxn];
int w[maxn];      // 记录点权
map<string,int> mp1;
map<int,string> mp2;
map<string,int> ans;
int wpl, nodes, maxw, head;

int transf(string p){       // 给每个点编号
    if(mp1.find(p) != mp1.end()){
        return mp1[p];
    }
    else{
        mp1[p] = cnt;
        mp2[cnt] = p;
        return cnt++;
    }
}

void dfs(int root){
    nodes++;
    visited[root] = true;
    if(w[root] > maxw){     // 统计当前连通域中点权最大的点
        head = root;
        maxw = w[root];
    }
    for(int i=0; i<cnt; i++){
        if(graph[root][i] != 0){       // 即使这个边对应的点被访问,这个边也不会漏掉,避免出现环漏掉边的情况
            wpl += graph[root][i];
            graph[root][i] = graph[i][root] = 0;
            if(!visited[i])
                dfs(i);
        }
    }
}

void dfsTravel(){
    for(int i=0; i<cnt; i++){
        wpl = nodes = maxw = head = 0;
        if(!visited[i]){
            dfs(i);
            if(nodes>2 && wpl>k){
                ans[mp2[head]] = nodes;
            }
        }
    }
}

int main(){
    fill(graph[0], graph[0]+maxn*maxn, 0);
    fill(w, w+maxn, 0);
    cin>>n>>k;
    for(int i=0; i<n; i++){
        string p1, p2;
        int time;
        cin>>p1>>p2>>time;
        int a = transf(p1), b = transf(p2);
        w[a] += time;
        w[b] += time;
        graph[a][b] += time;
        graph[b][a] += time;
    }
    fill(visited, visited+maxn, false);
    dfsTravel();
    cout<<ans.size()<<endl;
    for(map<string,int>::iterator it=ans.begin(); it!=ans.end(); it++)
        cout<<it->first<<" "<<it->second<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值