您的位置:首页 > 其它

PAT_A 1034. Head of a Gang (30)

2017-04-04 09:04 561 查看

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

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

分析:

很明显这就是一个连通分支的题目(但有下边这两条使其变难)

使用连通分支算法前需要讲字符串转化成对应唯一的数字,才能更方便使用数组实现连通分支计算

加上两个限制条件 

the member mount of gang >2

the total weight >k

code:

字符数字转化函数,有同学用map做的,一开始我也采取map方式,但从value到key的转化时,就放弃map了–>对于不规则的字符串来说或者字符串较长,这种方式就不太好了,可以用两个map来做,key-value正好相反。

连通分支计算

我这是用的是un确定father的方法,另外用dfs也可以,就是对访问过的设置标记,这里推荐一个萌妹子的算法

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cstdio>
using namespace std;
int w[300000];
int f[300000];
vector<int> fc[300000];//the member of gang
//长度为3的char---->字符数字转化函数
void output(int a)
{
char tmp[4];
tmp[3]='\0';
for(int i=2;i>=0;i--)
{
tmp[i]=char('A'+a%100);
a/=100;
}
cout<<tmp;
}
int toInt(char*a)
{
int tmp=0;
for(int i=0;i<3;i++)
{
tmp*=100;
tmp+=(a[i]-'A');
}
return tmp;
}
//连通分支相关计算
int gf(int a)
{
if(a==f[a])
return a;
return f[a]=gf(f[a]);
}
void un(int a,int b)
{
int fa=gf(a);
int fb=gf(b);
if(fa>fb)
f[fa]=fb;
else
f[fb]=fa;
}
bool comp(pair<int,int> a,pair<int,int> b)
{
return a.first<b.first;
}
int main()
{
//freopen("in","r",stdin); //文件流绑定到标准输入
fill(w,w+30000,0);
for(int i=0;i<300000;i++)
f[i]=i;
int n,k,tmp,l,r;
char tmp1[4];
char tmp2[4];
tmp1[3]='\0';
tmp2[3]='\0';
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>tmp1>>tmp2>>tmp;
l=toInt(tmp1);
r=toInt(tmp2);
w[l]+=tmp;
w[r]+=tmp;
un(l,r);
}
for(int i=0;i<300000;i++)
if(w[i]>0)      fc[gf(i)].push_back(i);
vector<pair<int,int> >out;
pair<int,int> tmp_pair;
int tmp_w=0;
for(int i=0;i<300000;i++)
{
if(w[i]>0&&fc[i].size()>2)
{
tmp_pair.first=fc[i].at(0);
tmp_pair.second=fc[i].size();
tmp_w=w[fc[i].at(0)];
//find the head of gang
for(int j=1;j<fc[i].size();j++)
{
tmp_w+=w[fc[i].at(j)];
if(w[fc[i].at(j)]>w[tmp_pair.first])
{
tmp_pair.first=fc[i].at(j);
}
}
//the total relation w:the sum of member`s w divide 2
if(tmp_w/2>k)
out.push_back(tmp_pair);
}
}
cout<<out.size()<<endl;
if(out.size()==0)
return 0;
sort(out.begin(),out.end(),comp);
for(int i=0;i<out.size();i++)
{
output(out.at(i).first);
cout<<" "<<out.at(i).second<<endl;
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息