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

想法

九月就快要结束了。 整个九月,最重要的事情, 就是pta了。
这一题, 在算法笔记里面, 考察的是, 图的深度优先遍历。
对c++的map有了更深的理解。 难点在于, 有了很多变量, 数组, 从string -》 int , 再从 int -》 string, 很是纷杂。 还是第一次, 跟着算法笔记的思路, 敲了一遍。 新知识, 还要多加练习,理解、记忆。

认识

0. 此题, 主要考查的是, 图的深度优先遍历。 我们知道, 对于 有多个连通子图的图来说, DFS要调用多次, 所以图不同于树, 见代码, 图有DFSTravel, 来保证包括孤立点的所有点都经过一次DFS函数。

1.涉及到, 图的边权 时候, 最好用 邻接矩阵表示, 如果同时还有, 点权, 建立一个数组, 存放点权。 如何建立, 边、点之间的关联呢? 首先我们一般刷题的时候, 都会先给一个N,表示此图, 抽象为图后, 有的节点数,我们用1~N - 1, 表示下标,表示这些点。 如果点, 有特定的表达方式, 比如, “AA”, “BB”,“CC”,这类用字符串表示的点, 我们可以使用map,构建, string to int 的映射, 在构建, 点 到 点权 边权 的联系。

2. 此题目就是使用邻接矩阵。 图的另外一种表示是, 邻接表。 通常用, 链表 或者 vector表示。 这种方式,表示起来, 要很简单, 但是不好表示图的边权。

**3.**不同于树, 树是从上到下的, 有明确的方向, 所以, DFS只要一次, 开始一定是root,就能完成整个树的遍历。 但是图, 首先, 图所定义的起点, 只是, 根据点的输入的先后顺序表示,最先的最有可能是起点。 对于计算机来说, 因为其对于指令需要从前到后, 一条一条、线性地处理, 所以才呈现为 有先后顺序, 但是 本质上, 图是没有先后顺序可言的。其先后顺序是定义的

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

#include<iostream>
#include<string>
#include<map>
using namespace std;
const int maxn = 2010;
const int INF = 1000000000;


map<string, int> stringToInt;//string -》 int
map<int, string> intToString; 
map<string, int> Gang;
int G[maxn][maxn] = {0}; //存储边权
int weight[maxn] = {0}; //存储点权力 
bool vis[maxn] = {false};  //表示没有点遍历过 
int n, k,numPerson = 0; //表示点的编号, 从0 到 n-1; 

void DFS(int nowVisit, int &head, int &numMember, int &totalValue) {
	vis[nowVisit] = true; 
	numMember ++;//成员数增加
	if(weight[nowVisit] > weight[head]) {
		head = nowVisit; //点权最大的为头目 
	} 
	for(int i = 0; i < numPerson; i ++) {
		if(G[nowVisit][i] > 0) {
			totalValue += G[nowVisit][i];
			G[nowVisit][i] = G[i][nowVisit] = 0;
			if(vis[i] == false)//******这个个人认为没有必要, 因为如果G[nowVisit][i] > 0, 那么i必然没有被访问, 反证法, 如果访问量, 则G[i][nowVisit] == 0 
				DFS(i, head, numMember, totalValue);
		}	
	}
	
} 

void DFSTravel() {
	for(int i = 0; i < numPerson; i ++) {
		if(vis[i] == false) {
			int head = i, numMember = 0, totalValue = 0;
			DFS(i, head, numMember, totalValue);
			
			if(numMember > 2 && totalValue > k) {
//				printf("numMember == %d\n",numMember);
				Gang[intToString[head]] == numMember;
//				cout << Gang[intToString[head]] << endl;
			}
		}
	}
}

int change(string str) {
	if(stringToInt.find(str) != stringToInt.end()) {
		return stringToInt[str];
	} else {
		stringToInt[str] = numPerson;
		intToString[numPerson] = str;
		return numPerson ++;
	}
}



int main()
{
	int w;
	string str1, str2;
	cin >> n >> k;
	for(int i = 0; i < n; i ++) {
		cin >> str1 >> str2 >> w;
		int id1 = change(str1);
		int id2 = change(str2);
	//	cout << "Id1 == " << id1 << " and  " << "Id2 ==" << id2 <<endl;
		weight[id1] += w;
		weight[id2] += w;
		G[id1][id2] += w;
		G[id2][id1] += w;
	}
//	printf("OK");
	
	DFSTravel();
	cout << Gang.size() << endl;
	map<string, int> ::iterator it;
	for(it = Gang.begin(); it != Gang.end(); it ++) {
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}




题目链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值