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,可以防止重复计算

    ---------------- 坚持每天学习一点点
  • 相关阅读:
    【C#图解教程学习笔记】第13章 委托
    TreeView树,全选,反选,平级选操作
    C# 字符串和字节数组转换
    WebServices 使用Session
    Win8.1下Flash Builder 提示: 找不到所需的Adobe Flash Player调试器版本,解决办法
    C# winform DataGridView 绑定数据的的几种方法
    .NET Core GB2312
    [WPF 学习] 7.2 模板打印
    [WPF 学习] 7.1 多页打印
    [WPF 学习] 7 打印相关的东东
  • 原文地址:https://www.cnblogs.com/tccbj/p/10402409.html
Copyright © 2011-2022 走看看