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 = ;
- }
- }ans[];///记录答案
- int n,k,c,d,pos[],mp[][],vis[],val[],ant,temp;///pos记录hashing值对应1~c的第几个值
- char s1[],s2[],s[][];
- int getval(char *t) {///hashing值
- return (t[] - 'A') * * + (t[] - 'A') * + t[] - 'A';
- }
- void dfs(int p) {///搜索某个连通分支
- vis[p] = ;///标记
- ans[ant].num ++;///人数加一
- if(val[p] > val[ans[ant].id])ans[ant].id = p;///更新head
- for(int i = ;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]) < ;
- }
- int main() {
- scanf("%d%d",&n,&k);
- for(int i = ;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 = ;i <= c;i ++) {
- if(!vis[i]) {
- temp = ;
- dfs(i);
- if(ans[ant].num > && temp > k)ant ++;///满足条件才算
- else ans[ant].num = ,ans[ant].id = ;
- }
- }
- printf("%d\n",ant);
- sort(ans,ans + ant,cmp);
- for(int i = ;i < ant;i ++) {
- printf("%s %d\n",s[ans[i].id],ans[i].num);
- }
- }
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 pho ...
- 1034 Head of a Gang (30 分)(图的遍历or并查集)
dfs #include<bits/stdc++.h> using namespace std; ; int mp[N][N]; int weight[N]; int vis[N]; ma ...
- pat 甲级 1034 ( Head of a Gang )
1034 Head of a Gang (30 分) One way that the police finds the head of a gang is to check people's pho ...
- 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 p ...
- pat 甲级 1034. Head of a Gang (30)
1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...
- PAT 1034 Head of a Gang[难][dfs]
1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...
- PAT 1034. Head of a Gang
1034. Head of a Gang (30) One way that the police finds the head of a gang is to check people's phon ...
- etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))
# 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...
- PAT甲级1034. Head of a Gang
PAT甲级1034. Head of a Gang 题意: 警方找到一个帮派的头的一种方式是检查人民的电话.如果A和B之间有电话,我们说A和B是相关的.关系的权重被定义为两人之间所有电话的总时间长度. ...
- c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00
C# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...
随机推荐
- Realm多线程中的那些坑...
个人在开发中遇到的一些小坑... 可能会持续更新... 1.RealmObject自带线程保护功能.仅仅能在创建它的线程中訪问.在子线程中不能訪问. 也就是说.假设你在主线程中new了一个RealmO ...
- Android Handler 异步消息处理机制的妙用 创建强大的图片载入类
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 近期创建了一个群.方便大家交流,群号: ...
- 常见的CPU訪问引起的内存保护问题为什么仅仅用event_122上报 - 举例2
还有一个样例.通过以下的log看,CPU在訪问reserved的地址0x53611EFD.非法訪问时该地址会在L1D内存控制器的L1DMPFSR寄存器中记录. ** FATAL EXCEPTION N ...
- linux uart驱动——相关数据结构以及API(二)
一.核心数据结构 串口驱动有3个核心数据结构,它们都定义在<#include linux/serial_core.h>1.uart_driver uart_driver包 ...
- mongo 介绍
[介绍]:MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能.MongoDB 旨在为WEB应用提供可扩展的高性能数据存 ...
- PHP购物车模块的实现(php/ajax/session)
购物车网页代码 1.登录界面login.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 九度OJ 1079:手机键盘 (翻译)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2279 解决:1233 题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在"1"键上,输入a只 ...
- Spring自定义配置--ConfigurationProperties
自定义配置的变量名: 在 *.properties 里面定义特定的变量 server.port=9000 amazon.associateId=habuma-20 建立Properties文件制定特定 ...
- 《程序员代码面试指南》第八章 数组和矩阵问题 打印N 个数组整体最大的Top K
题目 打印N 个数组整体最大的Top K java代码 package com.lizhouwei.chapter8; /** * @Description: 打印N 个数组整体最大的Top K * ...
- GDB打印内存命令
用gdb查看内存 格式 x /nfu 参数说明 x是 examine 的缩写 n表示要显示的内存单元的个数 f表示显示方式, 可取如下值 x 按十六进制格式显示变量 d 按十进制格式显示变量 u 按十 ...