zoukankan      html  css  js  c++  java
  • PAT A1034 Head of a Gang (30 分)——图遍历DFS,字符串和数字的对应保存

    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
    
     
    #include <stdio.h>
    #include <string>
    #include <algorithm>
    #include <map>
    #include <iostream>
    using namespace std;
    const int maxn = 2010;
    map<string, int> s2i;
    map<int, string> i2s;
    map<string,int> gang;
    int n, k, num = 0;
    int g[maxn][maxn], w[maxn];
    bool vis[maxn];
    int change(string s) {
        if (s2i.find(s) == s2i.end()) {
            i2s[num] = s;
            s2i[s] = num;
            return num++;
        }
        else {
            return s2i[s];
        }
    }
    void dfs(int v,int &count,int &weight,int& head) {
        vis[v] = true;
        count++;
        if (w[v] > w[head]) {
            head = v;
        }
        for (int i = 0; i < num; i++) {
            if (g[v][i] != 0) {
                weight += g[v][i];
                g[v][i] = 0;
                g[i][v] = 0;
                if (vis[i] == false) {
                    dfs(i, count, weight, head);
                }
            }
        }
    }
    void dfsTrave() {
        fill(vis, vis + maxn, false);
        for (int i = 0; i < num; i++) {
            int count = 0, weight = 0, head = i;
            if (vis[i] == false) {
                dfs(i,count,weight,head);
            }
            if (count > 2 && weight > k) {
                gang[i2s[head]] = count;
            }
        }
    }
    int main() {
        fill(g[0], g[0] + maxn * maxn, 0);
        fill(w, w + maxn, 0);
        scanf("%d %d", &n, &k);
        for (int i = 0; i < n; i++) {
            string s1, s2;
            int t;
            cin >> s1 >> s2 >> t;
            getchar();
            int id1 = change(s1);
            int id2 = change(s2);
            w[id1] += t;
            w[id2] += t;
            g[id1][id2] += t;
            g[id2][id1] += t;
        }
        dfsTrave();
        printf("%d
    ", gang.size());
        for (auto it = gang.begin(); it != gang.end(); it++) {
            cout << it->first << " " << it->second << endl;
        }
        system("pause");
        return 0;
    }

    注意点:dfs遍历图,就是中间的计算比较复杂,算总weight时要注意环,算一条边就把那条边置为0,可以防止重复计算

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    module5-01-jQuery 基础
    module4-JavaScript 高级特性、ES6 新特性
    module4-05-ES6新特性
    module4-04-正则表达式
    module4-03-继承和函数进阶
    module4-02-面向对象编程案例 随机方块、贪吃蛇
    module4-01-面向对象编程、原型链、构造函数、原型对象
    module3-Web APIs 网页应用编程
    module3-05-定时器的应用-简单动画-无缝滚动-轮播图
    人生赢家从规划开始,先觉知、量己力、衡外情、重实践、善反省
  • 原文地址:https://www.cnblogs.com/tccbj/p/10402409.html
Copyright © 2011-2022 走看看