PAT 1034. Head of a Gang (30)(孤岛以及一个奇怪的cost的计算问题)

官网

1034. Head of a Gang (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.给定一组通话记录,找出“相互”通过电话的人,如果这个孤岛成员大于三并且他们总的通话时间大于阈值k,则保存然后按照字母顺序输出。

解题思路

  • 1.先找出每个孤岛的成员,然后统计每个成员播出去的电话的时间的和,如果成员个数和总时间都满足条件,就保存并按字母顺序输出即可。
  • 2.总时间计算有点坑,所以我只保存每个成员播出去的电话的额时间,避免重复计算,之前重复计算害死我了。

AC代码

#include<map>
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#define MAXN 2001
using namespace std;
int n,k,cnt=0,max_id = -1,max_call = 0,members =0,tem_sum = 0;
vector<int> member;
int call_total_from[MAXN],cost[MAXN];
bool road[MAXN][MAXN],visit[MAXN];
map<string,int> str2int;
map<int,string> int2str;

struct keep{
    string name;
    int numOfMembers;
    keep(string _n,int _m):name(_n),numOfMembers(_m){}
};
bool cmp(const keep & a,const keep & b){
    return a.name < b.name;
}
vector<keep> ke;
void dfs(int l){
    if (!visit[l]) {
        if (cost[l] > max_call) {
            max_call = cost[l];
            max_id = l;
        }
        members++;
        member.push_back(l);
    }
    visit[l] = true;
    for (int i = 1; i <= cnt; ++i) {
        if (!visit[i]&&road[l][i]) {
            dfs(i);
        }
    }

}
int main(int argc, char *argv[])
{
    cin >> n >> k;
    for (int i = 1; i <= 2*n; ++i) {
        cost[i] = 0;visit[i] = false;call_total_from[i] = 0;
        for (int j = 1; j <= 2*n; ++j) {
            road[i][j] = false;
        }
    }
    string w,t;int v;
    for (int i = 0; i < n; ++i) {
        cin >> w >> t >> v;
        if (!str2int[w]) {
            str2int[w] = ++cnt;
            int2str[cnt] = w;
        }
        if (!str2int[t]) {
            str2int[t] = ++cnt;
            int2str[cnt] = t;
        }
        //他们俩是否大过电话
        road[str2int[w]][str2int[t]] = road[str2int[t]][str2int[w]] = true;
        //w播出去的电话的时间
        call_total_from[str2int[w]] += v;
        //w通话的时间
        cost[str2int[w]] += v;
        //t通话的时间
        cost[str2int[t]] += v;
    }

    //dfs
    for (int i = 1; i <= cnt; ++i) {
        if (!visit[i]) {
            max_id = -1,max_call = 0,members =0;member.clear(),tem_sum = 0;
            dfs(i);
            //如果成员大于2了,再计算这些成员的总的通话的时间
            if (members > 2) {
                for (int i = 0; i < member.size(); ++i) {
                    //注意避免重复计算
                    tem_sum += call_total_from[member[i]];
                }
                //满足条件则保存,等待后面按照字母顺序输出
                if (tem_sum > k) {
                    ke.push_back(keep(int2str[max_id],members));
                }
            }
        }
    }
  //输出
    if (!ke.empty()) {
        sort(ke.begin(),ke.end(),cmp);
        cout << ke.size() << endl;
        for (int i = 0; i < ke.size(); ++i) {
            cout << ke[i].name << " " << ke[i].numOfMembers << endl;
        }
    }else {
        cout << 0 << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值