zoukankan      html  css  js  c++  java
  • PAT Advanced Level 1034

    1034 Head of a Gang (30)(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 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
    /**********************
    author: yomi
    date: 18.8.22
    ps: 写完dfs() 恍然大悟 原来我方了的原因就是 我没把相同两点之间的多边合并 心里想着出环了
    这环还不是普通环 然后就没有然后了
    **********************/
    #include <iostream>
    #include <map>
    #include <set>
    using namespace std;
    const int maxn = 2010;
    int n, k, head, totalvalue, t[maxn], num;
    map<string, int>gang;
    map<string, int>stringToInt;
    map<int, string>intToString;
    int g[maxn][maxn], si;
    bool vis[maxn];
    int toInt(string n)
    {
        map<string, int>::iterator iter= stringToInt.find(n);
        int s = stringToInt.size();
        if(iter!=stringToInt.end()){
            return iter->second;
        }
        else{
            stringToInt[n] = s;
            return s;
        }
    }
    void dfs(int u)///整个dfs写懵了 我选择题解
    {
        num++;
        vis[u] = true;
        if(t[u] > t[head])
            head = u;
        for(int v=0; v<si; v++){
            if(g[u][v]>0){
                totalvalue += g[u][v];
                g[u][v] = g[v][u] = 0;
                if(!vis[v]){
                    dfs(v);
                }
            }
        }
    }
    int main()
    {
        cin >> n >> k;
        string n1, n2;
        int u, v, w, ans;
        for(int i=0; i<n; i++){
            cin >> n1 >> n2 >> w;
            u = toInt(n1);
            v = toInt(n2);
            intToString[u] = n1;
            intToString[v] = n2;
            t[u]+=w;
            t[v]+=w;
            ///g[v][u] = g[u][v] = w;错误一 因为两个人可以联系多次 我这样写 之前的权值就被覆盖了
            g[u][v]+=w;
            g[v][u]+=w;
    
        }
        si = stringToInt.size();
        for(int i=0; i<si; i++){
            head = i, totalvalue = 0, num = 0;
            dfs(i);
            if(num>2 && totalvalue>k){
                gang[intToString[head]] = num;
            }
    
        }
        cout << gang.size() << endl;
        for(map<string, int>::iterator iter=gang.begin(); iter!=gang.end(); iter++){
            cout << iter->first << ' ' << iter->second << endl;
        }
        return 0;
    }
    /**
    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
    **/
  • 相关阅读:
    [WinJS] Promise 用法
    Python 统计代码行
    mac下Apache + MySql + PHP网站开发
    android中,获取网速的方法实现
    如何屏蔽掉两个activity切换时的动画效果
    dp与px的相互转化
    毫秒的格式化
    关于android中事件传递和分发的一些小理解
    汉字转拼音
    关于实现无限循环的做法
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9518553.html
Copyright © 2011-2022 走看看