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

来源:互联网 发布:手机域名注册证书 编辑:程序博客网 时间:2024/06/03 17:00

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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 1:
2AAA 3GGG 3
Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10
Sample Output 2:
0
题意这次就没读懂,坡脚的英语。

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

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

AAA BBB 10BBB AAA 20AAA 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);//字符串转INTint 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;}



原创粉丝点击