• pat 甲级 1034. Head of a Gang (30)


    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 threshold, 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

    题意:gang是一个帮派群体,现在给出一张图,找到这张图上的所有帮派以及每个帮派的头目和帮派人数。其中要成为一个帮派,要满足以下要求:
    1:帮派人数大于等于3 2:帮派中人与人的通话总时长要高于一个界限threshhold.
    思路:dfs
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<set>
    #include<queue>
    #include<map>
    using namespace std;
    #define INF 0x3f3f3f
    #define N_MAX 10000+5
    typedef long long ll;
    struct gang{
        int num;
        string head;
        gang() {}
        gang(int num,string head):num(num),head(head) {}
        bool operator < (const gang&b) {
            return head < b.head;
        }
    };
    vector<gang>res;
    int n,m, threshold;
    map<string, int>Map;
    bool is_connect[N_MAX][N_MAX];
    string name[N_MAX];
    int weight[N_MAX],vis[N_MAX];
    
    int max_time = 0, id,num=0,sum=0; bool flag = 0;
    void dfs(int x) {
        sum += weight[x];
        num++;
        if (sum/2 > threshold)flag = true;
        vis[x] = true;
        if (max_time < weight[x]) {
            max_time = weight[x];
            id = x;
        }
        for (int i = 1; i < n;i++) {
            if (!is_connect[x][i]||vis[i])continue;
            dfs(i);
        }
    }
    
    int main() {
        while (cin>>m>>threshold) {
            n = 1;//n-1为人数
            for (int i = 0; i < m;i++) {
                string from, to; int cost;
                cin >> from >> to >> cost;
                if (Map[from] == 0) { Map[from] = n; name[n++] = from; }
                if (Map[to] == 0) { Map[to] = n; name[n++] = to; }
                is_connect[Map[from]][Map[to]] = 1;
                is_connect[Map[to]][Map[from]] = 1;
                weight[Map[from]] += cost; weight[Map[to]] += cost;
            }
            for (int i = 1; i < n;i++) {
                max_time = 0,flag = 0,num=0,sum=0;
                if (!vis[i]){
                    dfs(i);
                    if (flag&&num>2) {
                        res.push_back(gang(num, name[id]));
                    }
                }
            }
            sort(res.begin(),res.end());
            printf("%d
    ",res.size());
            for (int i = 0; i < res.size();i++) {
                printf("%s %d
    ",res[i].head.c_str(),res[i].num);
            }
    
        }
        return 0;
    }
  • 相关阅读:
    LeetCode题解(2)-- Add Two Numbers
    LeetCode题解(1)--Two Sum
    STL review:vector & string & map & struct
    机器学习导论(一) 基本概念
    Single Number
    Single Number II
    Copy List with Random Pointer
    Word Break
    Populating Next Right Pointers in Each Node II
    Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8570696.html
Copyright © 2020-2023  润新知