1034 Head of a Gang (30)(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 threshold, 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


读懂题之后就想用图的知识做,每个连通分支如果结点个数不超过2直接不考虑,所有边权值和不超过k也不考虑,搜索的过程中找到当前连通分支权值最大的点,给出的是字符串,可以hashing一个值,这个值不是连续的,再对应到连续的值1~c,题目中最多不超过2000 = 2 * 1000,然后就可以用mp记录边的值了,累加的,val记录点的值,也是累加的,这样一来,邻接矩阵就有了。。。然后进行搜多,vis标记访问,最后把答案排序。。。代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
struct info {///记录答案 写成结构体方便排序 头 和 数量
    int id,num;///id就是 head of a gang , num是人数
    info() {
        id = num = 0;
    }
}ans[2001];///记录答案
int n,k,c,d,pos[20000],mp[2001][2001],vis[2001],val[2001],ant,temp;///pos记录hashing值对应1~c的第几个值
char s1[4],s2[4],s[2001][4];
int getval(char *t) {///hashing值
    return (t[0] - ‘A‘) * 26 * 26 + (t[1] - ‘A‘) * 26 + t[2] - ‘A‘;
}
void dfs(int p) {///搜索某个连通分支
    vis[p] = 1;///标记
    ans[ant].num ++;///人数加一
    if(val[p] > val[ans[ant].id])ans[ant].id = p;///更新head
    for(int i = 1;i <= c;i ++) {
        if(!mp[p][i])continue;///无权边  即为 两人无关
        if(p < i)temp += mp[p][i];///仅当p < i 就是按同一个方向 累加关系值  防止重复和漏加
        if(vis[i] || !mp[p][i])continue;
        dfs(i);
    }
}
bool cmp(info a,info b) {
    return strcmp(s[a.id],s[b.id]) < 0;
}
int main() {
    scanf("%d%d",&n,&k);
    for(int i = 0;i < n;i ++) {
        scanf("%s%s%d",s1,s2,&d);
        int x,y;
        if(!pos[getval(s1)]) {
            x = pos[getval(s1)] = ++ c;
            strcpy(s[x],s1);///反过来要知道 pos对应的字符串
        }
        else x = pos[getval(s1)];
        if(!pos[getval(s2)]) {
            y = pos[getval(s2)] = ++ c;
            strcpy(s[y],s2);
        }
        else y = pos[getval(s2)];
        mp[x][y] += d;
        mp[y][x] += d;
        val[x] += d;
        val[y] += d;
    }
    for(int i = 1;i <= c;i ++) {
        if(!vis[i]) {
            temp = 0;
            dfs(i);
            if(ans[ant].num > 2 && temp > k)ant ++;///满足条件才算
            else ans[ant].num = 0,ans[ant].id = 0;
        }
    }
    printf("%d\n",ant);
    sort(ans,ans + ant,cmp);
    for(int i = 0;i < ant;i ++) {
        printf("%s %d\n",s[ans[i].id],ans[i].num);
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/9194195.html

时间: 2024-10-27 13:20:41

1034 Head of a Gang (30)(30 分)的相关文章

1034 Head of a Gang (30 分)

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 Aand B, we say that A and B is related. The weight of a relation is defined to be the total time length of

PAT 甲级 1034 Head of a Gang (30 分)(bfs,map,强连通)

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

PAT 1034 Head of a Gang[难]

1034 Head of a Gang (30)(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 lengt

【2014年6月份日常记录表(2014.6.1—6.30,30天)】

2014年5月份日常记录表(2014.5.1-5.31,31天) 日期 周次 天数 编程 日记 阅读 英语 早睡 备注 2014.6.1 周日 第1天           蓝桥杯比赛 大二下学期 第十五周 2014.6.2 周一 第2天           蓝桥之旅 2014.6.3 周二 第3天           蓝桥之旅 2014.6.4 周三 第4天             2014.6.5 周四 第5天           2014.6.6 周五 第6天             201

有五个学生,每个学生有3门课(语文、数学、英语)的成绩, * 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩)

/* * 3. 有五个学生,每个学生有3门课(语文.数学.英语)的成绩, * 写一个程序接收从键盘输入学生的信息,输入格式为:name,30,30,30(姓名,三门课成绩), * 然后把输入的学生信息按总分从高到低的顺序写入到一个名称"stu.txt"文件中. * 要求:stu.txt文件的格式要比较直观,打开这个文件,就可以很清楚的看到学生的信息 * */ import java.io.*; import java.util.*; public class Test3 { publi

1034 Head of a Gang (30分)

1. 题目 2. 思路 使用并查集归属集合 3. 注意点 可以使用map<string, string> 模拟int类型的并查集,减少了string和int类型转换的问题 因为c++的map,值如果不存在会自动初始化 map<string, string> father; //定义 // 查找root string findfather(string x){ if(father[x] == ""){ father[x] = x; }else{ while(fat

1034. Head of a Gang (30)

注意这里n是电话数,电话数不超过1000代表人数不超过两千,...好坑.... 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

题目例如以下: 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 call

PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,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 mad