1. 程式人生 > >程式設計基礎23 圖的深度優先搜尋

程式設計基礎23 圖的深度優先搜尋

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

一,思路

    先要做的是對人名進行編號,即構造一個int_to_str的map,然後就是解決無向圖來回都有權值的問題,解決方法就是構建一個weight[]陣列,下標即是人名的編號,在輸入的時候對於一組的兩個人名同時進行weight的相加,然後在遍歷到鄰接矩陣時出現兩個中的其一時,把鄰接矩陣的權值歸為0,然後把返回來的對稱的位置也歸為0,防止返回。  

    對人名編號程式碼:

int charge(string str) {
	if (str_to_int.find(str) != str_to_int.end()) {
		return str_to_int[str];
	}
	else {
		int_to_str[NumPersons] = str;
		str_to_int[str] = NumPersons;
		return NumPersons++;
	}
}

    在深度搜索的時候需要求三個變數,head(當前遍歷到的通話時間最長的那個),nowPerson,nowWeight,並且這三者都要引用,因為dfs完一個結點時,需要利用第三個變數。

二,程式碼

#include<iostream>
#include<string>
#include<map>
using namespace std;
const int max_n = 2100;
int NumPersons = 0;
int N = 0;
int	K = 0;
int weight[max_n] = { 0 };
bool vis[max_n] = { false };
int G[max_n][max_n] = { 0 };
map<string, int> str_to_int;
map<int, string> int_to_str;
map<string, int> Gang;
int charge(string str) {
	if (str_to_int.find(str) != str_to_int.end()) {
		return str_to_int[str];
	}
	else {
		int_to_str[NumPersons] = str;
		str_to_int[str] = NumPersons;
		return NumPersons++;
	}
}
void dfs(int nowVisit, int &head, int &nowPersons, int &nowWeight) {
	nowPersons++;
	vis[nowVisit] = true;
	if (weight[nowVisit] > weight[head]) {
		head = nowVisit;
	}
	for (int i = 0; i < NumPersons; i++) {
		if (G[nowVisit][i] > 0) {
			nowWeight += G[nowVisit][i];
			G[i][nowVisit] = G[nowVisit][i] = 0;
			if (vis[i] == false) {
				dfs(i, head, nowPersons, nowWeight);
			}
		}
	}
}
void dfs_travels() {
	int nowVisit = 0;
	int nowPersons = 0, nowWeight = 0;
	int head = 0;
	for (int i = 0; i < NumPersons; i++) {
		if (vis[i] == false) {
			nowPersons = 0, nowWeight = 0; head = i;
			dfs(i, head, nowPersons, nowWeight);
			if (nowPersons> 2 && nowWeight > K) {
				Gang[int_to_str[head]] = nowPersons;
			}
		}
	}
}
int main() {
	string str1, str2;
	int Time = 0;
	int id1 = 0, id2 = 0;
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		cin >> str1 >> str2 >> Time;
		id1 = charge(str1);
		id2 = charge(str2);
		weight[id1] += Time;
		weight[id2] += Time;
		G[id1][id2] += Time;
		G[id2][id1] += Time;
	}
	dfs_travels();
	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;
}