1034 Head of a Gang(未解决)——PAT A

25 篇文章 0 订阅

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 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

【分析】无向带权图,求满足要求的连通分量数量

总结:

这个问题要整整思路再来写,不然写出来就是下面这个乱七八糟的东西。

目前的结果是这样,暂时不知道问题在哪。。

下次再试试,花了好长时间了。

0答案正确84 ms16256 KB
1答案正确83 ms16256 KB
2答案正确65 ms16256 KB
3运行时错误90 ms16384 KB
4运行时错误76 ms16288 KB
5答案正确87 ms16288 KB
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <string.h>
#include <set>
using namespace std;
const int maxn = 2e3 + 5;
int n, k, num = 0, ans = 0;
int G[maxn][maxn];
bool used[maxn];
string person[maxn];
set<int> gang[maxn];
typedef pair<string, int> P;
vector<P> head;
int weight[maxn], total[maxn];
int locate(string s) {
    for (int i = 0; i < num; i++) {
        if (s == person[i]) return i;
    }
    return -1;
}
void dfs(int x) {
    gang[ans].insert(x);
    used[x] = true;
    for (int i = 0; i < num; i++) {
        if (G[x][i] != 0 && !used[i]) {
            dfs(i);
        }
    }
}
void calc() {
    for (int i = 0; i < ans; i++) {
        int index, maxi = 0;
        for (set<int>::iterator it = gang[i].begin(); it != gang[i].end(); it++) {
            total[i] += weight[*it];
            if (weight[*it] > maxi) {
                index = *it;
                maxi = weight[*it];
            }
        }
        total[i] /= 2;
        if (gang[i].size() > 2 && total[i] > k) head.push_back(make_pair(person[index], gang[i].size()));
    }
}
void solve() {
    fill(used, used + num, false);
    for (int i = 0; i < num; i++) {
        if (!used[i]) {
            dfs(i);
            ans++;
        }
    }
    calc();
    if (head.size() == 0) {
        printf("0");
        return;
    }
    printf("%d\n", head.size());
    sort(head.begin(), head.end());
    for (int i = 0; i < head.size(); i++) {
        cout << head[i].first << " " << head[i].second;
        printf("%s", i != head.size() - 1 ? "\n" : "");
    }

}
int main() {
    cin >> n >> k;
    memset(G, 0, sizeof(G));
    memset(weight, 0, sizeof(weight));
    memset(total, 0, sizeof(total));
    for (int i = 0; i < n; i++) {
        char a[10], g1[5], g2[5];
        int w, id1, id2;
        getchar();
        scanf("%[^\n]s", a);
        sscanf(a, "%s %s %d", &g1, &g2, &w);
        string s1 = g1, s2 = g2;
        id1 = locate(s1);
        id2 = locate(s2);
        if (id1 == -1) {
            id1 = num;
            person[num++] = s1;
        }
        if (id2 == -1) {
            id2 = num;
            person[num++] = s2;
        }
        G[id1][id2] += w;
        G[id2][id1] += w;
        weight[id1] += w;
        weight[id2] += w;
    }
    solve();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值