(邻接矩阵DFS实现)1034 Head of a Gang (30 分)

1 篇文章 0 订阅
1 篇文章 0 订阅

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

思路与注意点

  • 题目给出的输入是无向图边的个数(通话个数),每个满足条件连通图边权之和的最小权值。(也就是犯罪团伙总通话最小时间),以及每条边的结点及对应权值。
    如AAA BBB value 指的是AAA 打给BBB的电话时间。

  • 我们需要在这些通话时间大于K值的犯罪团伙,并输出犯罪团伙中通话时间最长人的名字以及犯罪团伙总人数(连通块中结点个数)

需要注意的点

  • 在输入的过程中记录每一个人的通话时间也就是点权。
  • 输入的过程中累加相同结点的双向通话时间,如AAA->BBB和 BBB-> AAA的时间。
  • 用计数器来累加结点的编号,做名字和编号的映射转换
  • 结点个数最多为2*N
  • dfs遍历 遍历每条边而非每个结点
    图的dfs访问可以把连通块的信息记录下来,但是dfs特点是每个结点只访问一次,而我们需要的是访问所有的边,未防止漏解(在含有环的时候会出现漏接 如第一个样例F->G->H->F)需要在先累加边权在去判断vis是否为0然后进行递归访问下一节点。同时为了防止重复累加边权,再没每访问一条边后需要对边权清零,下一次访问这条无向边的时候对累加总边权就没有影响。

代码实现

#include<iostream>
#include<string>
#include<map>
using namespace std;
const int maxn=2010; //数据范围 
int nodes[maxn][maxn]={0}; //存储整个图 
bool vis[maxn];  
map<int,int> connect;//连通块 
map<string,int> Gang;//团伙  head的名字 -> 团伙中包含的人数 
map<string ,int> stringToInt;
map<int,string> intToString;
int weight[maxn]={0};
int numPerson,n,k;//顶点数,边数,边权和下限 
void dfs(int nowvist,int &cnum,int & head,int & totalvalue){  //要加引用 不然不改变值 
	
	cnum++;
	vis[nowvist]=1;
	if(weight[nowvist]>weight[head]){
		head=nowvist;
	}	
	for(int i=0;i<numPerson;i++){
		if(nodes[nowvist][i]>0){
			totalvalue+=nodes[nowvist][i];
			nodes[nowvist][i]=nodes[i][nowvist]=0;
			if(vis[i]==false){
				dfs(i,cnum,head,totalvalue);
			}
		}
	}
}//访问单个连通块 


void DFSTrave(){
	for(int i=0;i<numPerson;i++){
		if(vis[i]) continue;
		int cnum=0,head=i,totalvalue=0;
		dfs(i,cnum,head,totalvalue);
	//	cout<<"connnect Gang nowvist"<<nowvist<<"连通块结点数"<<cnum<<"head:"<<head<<"totalvalue"<<totalvalue<<endl;
		if(cnum>2&&totalvalue>k){
			Gang[intToString[head]]=cnum;
		}
	}
} 

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;
	freopen("test.txt","r",stdin);
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>str1>>str2>>w;
		int id1=change(str1);
		int id2=change(str2);
		weight[id1]+=w;
		weight[id2]+=w; //点权
		nodes[id1][id2]+=w; 				//更新边权 
		nodes[id2][id1]+=w;
	}
	DFSTrave();
	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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接矩阵是一种常用的图的表示方法,它使用一个二维数组来表示图中各个节点之间的连接关系。在邻接矩阵中,行和列别代表图中的节点,而矩阵中的元素表示节点之间的边的存在与否。 DFS(深度优先搜索)是一种图遍历算法,它从图的某个节点开始,沿着一条路径尽可能深入地访问节点,直到无法继续深入为止,然后回溯到上一个节点,继续访问其他未被访问过的节点,直到所有节点都被访问过为止。 在Java中,可以使用邻接矩阵实现DFS算法。下面是一个简单的Java代码示例: ```java import java.util.Stack; public class Graph { private int[][] adjacencyMatrix; private int numVertices; public Graph(int numVertices) { this.numVertices = numVertices; adjacencyMatrix = new int[numVertices][numVertices]; } public void addEdge(int source, int destination) { adjacencyMatrix[source][destination] = 1; adjacencyMatrix[destination][source] = 1; } public void dfs(int startVertex) { boolean[] visited = new boolean[numVertices]; Stack<Integer> stack = new Stack<>(); stack.push(startVertex); while (!stack.isEmpty()) { int currentVertex = stack.pop(); System.out.print(currentVertex + " "); visited[currentVertex] = true; for (int i = 0; i < numVertices; i++) { if (adjacencyMatrix[currentVertex][i] == 1 && !visited[i]) { stack.push(i); } } } } public static void main(String[] args) { Graph graph = new Graph(5); graph.addEdge(0, 1); graph.addEdge(0, 2); graph.addEdge(1, 3); graph.addEdge(1, 4); System.out.println("DFS traversal starting from vertex 0:"); graph.dfs(0); } } ``` 上述代码中,Graph类表示图,使用邻接矩阵来存储图的连接关系。addEdge方法用于添加边,dfs方法用于进行深度优先搜索遍历。在main方法中,创建一个图对象,并添加边,然后调用dfs方法进行遍历。 希望以上代码能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值