PAT 1034 Head 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 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 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

分析:
1、考察图的连通分量,难点在不是用数字表示各节点,因此需要用map来映射;计算各团队成员人数时,访问过后,就不再访问,所以用flag来标记;最后计算总通话时间是按访问边来计算的,为了避免重复加通话时间,再加完一次后需要把双方通话时间归零

注意:
1、测试点3错误,是因为最大通话条数为1000,则最多人数为2000人;测试点1错误可能是计算边的值时(总通话时间),重复加了通话时间

2、非常好的题,值得反复做

3、邻接链表版用了太多头文件,所以用了万能头文件,属实方便

#include<bits/stdc++.h>

包含:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

我的代码:

邻接矩阵版:

#include<cstdio>
#include<map>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 2010;
int rel[maxn][maxn] = {0},t[maxn] = {0},totaltime = 0,gangid = 0,gangcnt = 0;
map<string,int> mp,gang;
map<int,string> mp1;
bool flag[maxn] = {false};
void DFS(int n)
{           
    flag[n] = true;
    if (t[n] > t[gangid])
        gangid = n;
    gangcnt++;
    for(int i = 0 ;i < mp.size() ;i++)
    {
        if (rel[n][i] > 0)
        {
            totaltime += rel[n][i];
            rel[n][i] = rel[i][n] = 0;
            if (flag[i] == false)
                DFS(i);
        }            
    }
}
int main()
{
    int n,k,cnt = 0;
    scanf("%d%d",&n,&k);
    for (int i = 0 ;i < n ;i++)
    {
        string name[2];
        int time;
        cin >> name[0] >> name[1] >> time;
        for (int j = 0 ;j < 2 ;j++)
        {
            if (mp.find(name[j]) == mp.end())
            {
                mp1[cnt] = name[j];
                mp[name[j]] = cnt++;
            }             
        }
        for (int j = 0 ;j < 2 ;j++)
            t[mp[name[j]]] += time;
        rel[mp[name[0]]][mp[name[1]]] += time; 
        rel[mp[name[1]]][mp[name[0]]] += time;
    }
    for (int i = 0 ;i < cnt ;i++)
    {
        totaltime = gangcnt = 0;
        if (flag[i] == false)
        {
            gangid = i;
            DFS(i);
            if (gangcnt > 2 && totaltime > k)
                gang[mp1[gangid]] = gangcnt;
        }
    }
    if (gang.size() == 0)
        printf("0");
    else
    {
        printf("%d\n",gang.size());
        for (auto it = gang.begin() ;it != gang.end() ;it++)
            printf("%s %d\n",it->first.c_str(),it->second);
    }
    return 0;
}

邻接链表版:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2010;
map<string,int> mp,gang;
map<int,string> rmp;
struct Node
{
    int v,time;
}node;
int n,k,cnt = 0,t[maxn] = {0},totaltime = 0,gangcnt = 0,maxtime = 0,gangid;
vector<Node> v[maxn];
bool flag[maxn] = {false};
void DFS(int r)
{
    flag[r] = true;
    gangcnt++;
    if (maxtime < t[r])
    {
        gangid = r;
        maxtime = t[r];
    }       
    for (int i = 0 ;i < v[r].size() ;i++)
    {
        if (v[r][i].time > 0)
        {
            totaltime += v[r][i].time;
            if(flag[v[r][i].v] == false)
                DFS(v[r][i].v);
        }
    }
}
int main()
{
    scanf("%d%d",&n,&k);
    for (int i = 0 ;i < n ;i++)
    {
        string name[2];
        int time;
        cin >> name[0] >> name[1] >> time;
        for (int j = 0 ;j < 2 ;j++)
        {
            if (mp.find(name[j]) == mp.end())
            {
                rmp[cnt] = name[j];
                mp[name[j]] = cnt++;
            }               
        }
        t[mp[name[0]]] += time;
        t[mp[name[1]]] += time;
        node.v = mp[name[1]];
        node.time = time;
        v[mp[name[0]]].push_back(node);
        node.v = mp[name[0]];
        v[mp[name[1]]].push_back(node);
    }
    for (int i = 0 ;i < cnt ;i++)
    {
        gangcnt = totaltime = maxtime = 0;
        if (flag[i] == false)
        {
            gangid = i;
            DFS(i);
            if (gangcnt > 2 && totaltime > k)
                gang[rmp[gangid]] = gangcnt;
        }
    }
    if (gang.size() == 0)
        printf("0");
    else
    {
        printf("%d\n",gang.size());
        for (auto it = gang.begin() ;it != gang.end() ;it++)
            printf("%s %d\n",it->first.c_str(),it->second);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情书、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值