1034. Head of a Gang (30) <广搜>

12 篇文章 0 订阅

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 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
题意这次就没读懂,坡脚的英语。

题意:给出几窝帮派,找出帮派的头和帮派成员(大于2),帮派的头是通话时间最长的,并且总的通话时间要大于K。

这里要注意,不是每个人的通话时间相加,而是总的通话时间,

AAA BBB 10
BBB AAA 20
AAA CCC 40
例如,AAA的通话时间为70,BBB的通话时间为30,CCC的通话时间为40,总通话时70而不是140.这点要注意,除以2就行了

这题还需要用map进行字符串和数字之间的互换



我刚看到就想用并查集,其实差不多。总是感觉很麻烦吧。要遍历好几次。最后还是选择广搜

#include<iostream>  
#include<cstring>  
#include<cstdio>  
#include<queue>  
#include<stack>  
#include<algorithm>  
#include<vector> 
#include<map>
using namespace std;
int cnt=1;
int n,w;
int cou=0;
map<string,int> ma;
map<int,string> str;
vector<int> v[10110];
int fz[10101]={0},ttime[10110]={0};
typedef struct Node{
	string s;
	int len;
}Node;
Node node[10110];
int nod=0;
int toint(string s){
	if(ma[s]==0){
	   ma[s]=cnt;//字符串和数字之间都要有关系,相互的
	   str[cnt]=s;
	   return cnt++;
	}
	else return ma[s];
}
void f(int num){
	queue<int> que;
	que.push(num);
	int mmax=ttime[num],len=0,sum=0,xb=num;
	while(que.size()){
		int q=que.front();
		que.pop();
		if(fz[q]) continue;
		sum+=ttime[q];//每个人的时间相加
		fz[q]=1;
		len++;
		if(ttime[q]>mmax) {//找最大通话时间
			mmax=ttime[q];
			xb=q;
		}
		for(int i=0;i<v[q].size();i++){
			int nu=v[q][i];
			 que.push(nu);	
		}
	}
	if(len>2&&sum/2>w){
		cou++;
		node[nod].s=str[xb];//将满足的帮派和头存到新的结构体中,最后排序输出
		node[nod].len=len;
		nod++;
	}
}
bool cmp(Node n1,Node n2){
	return n1.s<n2.s;
}
int main(){

	cin>>n>>w;
	for(int i=0;i<n;i++){
		string a,b;
		int l;
		cin>>a>>b>>l;
		string c=a+b;
		int aa=toint(a);//字符串转INT
		int bb=toint(b);
		ttime[bb]+=l;//每个人的总通话时间
		ttime[aa]+=l;
		v[aa].push_back(bb);
		
	}
	for(int i=1;i<cnt;i++){
		f(i);
	} 
	cout<<cou<<endl;
	sort(node,node+nod,cmp);
	for(int i=0;i<nod;i++) cout<<node[i].s<<" "<<node[i].len<<endl;
	return 0;
}

这是第一次AC的代码,并没有想到总时间/2判断,我是新建一个map把两个人之间关联起来,纪录通话时间,求总的通话时间

#include<iostream>  
#include<cstring>  
#include<cstdio>  
#include<queue>  
#include<stack>  
#include<algorithm>  
#include<vector> 
#include<map>
using namespace std;
int cnt=1;
int n,w;
int cou=0;
map<string,int> ma;
map<int,string> str;
map<string,int> fuza;
vector<int> v[10110];
int fz[10101]={0},ttime[10110]={0};
typedef struct Node{
	string s;
	int len;
}Node;
Node node[10110];
int nod=0;
int toint(string s){
	if(ma[s]==0){
	   ma[s]=cnt;
	   str[cnt]=s;
	   return cnt++;
	}
	else return ma[s];
}
void f(int num){
	queue<int> que;
	que.push(num);
	int mmax=ttime[num],len=0,sum=0,xb=num;
	while(que.size()){
		int q=que.front();
		que.pop();
		if(fz[q]) continue;
		fz[q]=1;
		len++;
		if(ttime[q]>mmax) {
			mmax=ttime[q];
			xb=q;
		}
		for(int i=0;i<v[q].size();i++){
			int nu=v[q][i];
			if(fz[nu]==0){
			   que.push(nu);	
			}
			string c=str[q]+str[nu];//这里求总的时间
			sum+=fuza[c];
		}
	}
	if(len>2&&sum>w){
		cou++;
		node[nod].s=str[xb];
		node[nod].len=len;
		nod++;
	}
}
bool cmp(Node n1,Node n2){
	return n1.s<n2.s;
}
int main(){

	cin>>n>>w;
	for(int i=0;i<n;i++){
		string a,b;
		int l;
		cin>>a>>b>>l;
		
		fuza[a+b]=l;//这里,把a和b建立关系。
		int aa=toint(a);
		int bb=toint(b);
		ttime[bb]+=l;
		ttime[aa]+=l;
		v[aa].push_back(bb);
		
	}
	for(int i=1;i<cnt;i++){
		f(i);
	} 
	cout<<cou<<endl;
	sort(node,node+nod,cmp);
	for(int i=0;i<nod;i++) cout<<node[i].s<<" "<<node[i].len<<endl;
	return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值