未AC题 PAT A 1034

#include <cstdio>
#include <algorithm>
#include <set>
#include <cstring>
#include <vector>
using namespace std;
struct Gang {
    int headid, nMem, weight;
};
const int maxn = 26 * 26 * 26 + 10;
char idToName[maxn][4];
int nameToId(char name[]);
set<int> vertex; // 存储姓名的hashid,因为是set 所以访问时按字典序访问
int graph[maxn][maxn] = {0};
int weightOfVertex[maxn] = {0}; // weightOfVertex[x] 表示:x 的所有边的权值的总和
bool visited[maxn] = {false};
void DFSTravel();
void DFS(set<int>::iterator vit, Gang &gang);
bool cmp(const Gang &a, const Gang &b);
int n, threthold;
vector<Gang> ans;
int main() {
    scanf("%d %d\n", &n, &threthold);
    char name1[4], name2[4];
    int weight, id1, id2;
    for (int i = 0; i < n; ++i) {
        scanf("%s %s %d", name1, name2, &weight);
        id1 = nameToId(name1);
        id2 = nameToId(name2);
        if (id2 < id1) {
            swap(id1, id2);
        }
        // 因为是无向图,所以只存储上三角矩阵,同时只存储上三角矩阵有一个好处就是可以避免环的情况
        graph[id1][id2] += weight;   // 有 AAA BBB 10; BBB AAA 20的情况,所以用 +=
        weightOfVertex[id1] += weight; // id1 的所有边的权值再增加
        weightOfVertex[id2] += weight;
    }
    DFSTravel();
    sort(ans.begin(), ans.end(), cmp); // 按姓名字典序排序,原先没加,现在加上了,还是错
    printf("%d\n", ans.size());
    for (int i = 0; i < ans.size(); ++i) {
        printf("%s %d\n", idToName[ans[i].headid], ans[i].nMem);
    }
    return 0;
}

void DFSTravel() {
    Gang gang;
    for (set<int>::iterator it = vertex.begin(); it != vertex.end(); it++) {
        if (!visited[*it]) {
            // 一个连通分量一个 gang
            gang.headid = *it;
            gang.nMem = 0;
            gang.weight = 0;
            DFS(it, gang);
            if (gang.weight > threthold && gang.nMem > 2) {
                ans.push_back(gang);
            }
        }
    }
}
void DFS(set<int>::iterator vit, Gang &gang) {
    visited[*vit] = true;
    gang.nMem++;
    if (weightOfVertex[gang.headid] < weightOfVertex[*vit]) {
        gang.headid = *vit;
    }
    set<int>::iterator uit = vit; // 保存当前顶点为uit
    vit++;
    for (vit; vit != vertex.end(); vit++) {
        // 从当前顶点的下一个顶点出发,一直遍历到最后一个姓名字典序最大的顶点
        if (graph[*uit][*vit] > 0) {
        // uit 为当前顶点,vit 为当前顶点后面的顶点,即*uit < *vit ,所以不会重复遍历
            gang.weight += graph[*uit][*vit];  // 连通分量中所有的边都要统计上
            if (!visited[*vit]) {
                DFS(vit, gang);
            }
        }
    }
}
int nameToId(char name[]) {
    int hashvalue = 0;
    for (int i = 0; i < 3; ++i) {
        hashvalue = (hashvalue * 26 + name[i] - 'A');
    }
    if (!idToName[hashvalue][0]) {
        strcpy(idToName[hashvalue], name);
        vertex.insert(hashvalue);
    }
    return hashvalue;
}
bool cmp(const Gang &a, const Gang &b) {
    return a.headid < b.headid;
}
/*
  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:
  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 threshold, respectively.
  Then N lines follow, each in the following format:
    Name1 Name2 Time
  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.

 */
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值