PAT 1034Head of a Gang(30分)

PAT 1034Head of a Gang(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 betweenAandB, we say thatAandBis 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 thresholdK. 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 numbersNandK(both less than or equal to 1000), the number of phone calls and the weight threthold, respectively. ThenNlines follow, each in the following format:

Name1 Name2 Time

whereName1andName2are the names of people at the two ends of the call, andTimeis the length of the call. A name is a string of three capital letters chosen fromA-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

思路

  • 每次输入两个人,最多有 2 * n个结点。
  • 使用map建立字符串名称与编号之间的关系。
  • 进行图的遍历,使用dfs便利每个连通块,并记录下权值最大的结点和结点的个数。
  • dfs中的参数使用引用形式。

代码

#pragma warning (disable: 4996)
#include 
#include 
#include 

using namespace std;

const int maxn = 2010;



map intToString;
map stringToInt;
map gang;

int G[maxn][maxn], weight[maxn];
bool vis[maxn];
int n, k, numPerson;

void dfs(int u, int& head, int& num, int& totalWeight)
{
    num++;
    vis[u] = true;
    if (weight[u] > weight[head])
    {
        head = u;
    }
    for (int v = 0; v < numPerson; v++)
    {
        if (G[u][v] > 0)
        {
            totalWeight += G[u][v];
            G[u][v] = G[v][u] = 0;
            if (vis[v] == false)
            {
                dfs(v, head, num, totalWeight);
            }
        }
    }
}


void  dfsTrave()
{
    for (int u = 0; u < numPerson; u++)
    {
        if (vis[u] == false)
        {
            int head = u, num = 0, totalWeight = 0;
            dfs(u, head, num, totalWeight);
            if (num > 2 && totalWeight > k)
            {
                gang[intToString[head]] = num;
            }
        }
    }
}

int change(string str)
{
    if (stringToInt.find(str) != stringToInt.end())
    {
        return stringToInt[str];
    }
    else
    {
        stringToInt[str] = numPerson;
        intToString[numPerson] = str;
        numPerson++;
        return stringToInt[str];
    }

}

int main()
{
    //freopen("test.txt", "r", stdin);
    string str1, str2;
    int w;
    cin >> n >> k;
    for (int i = 0; i < n; i++)
    {
        cin >> str1 >> str2 >> w;
        int id1 = change(str1);
        int id2 = change(str2);
        weight[id1] += w;
        weight[id2] += w;
        G[id1][id2] += w;
        G[id2][id1] += w;
    }

    dfsTrave();
    cout << gang.size() << endl;
    map ::iterator it;
    for (it = gang.begin(); it != gang.end(); it++)
    {
        cout << it -> first << " " << it -> second << endl;

    }

    return 0;
}
参考资料:《算法笔记》.

你可能感兴趣的:(图,算法,c++)