PATA 1034 Head of a Gang

insert image description here
insert image description here

Record the pits you planted
1. The total number of call records does not exceed 1000, so how many people are there at most? 2000!!!
2.

//给一个顶点DFS
void DFS(int nowMember,int &gangNum,int &head,int &totalTime){
    
    
    isTraver[nowMember]=true;
    gangNum++;
    if(weight[nowMember]>weight[head])
        head=nowMember;
    for(int i=0;i<numPerson;i++){
    
    
        if(G[nowMember][i]>0&&!isTraver[i]){
    
    
            totalTime+=G[nowMember][i];
            G[nowMember][i]=G[i][nowMember]=0;//避免多算
            DFS(i,gangNum,head,totalTime);
        }
    }
}

What is the problem if you write this way?
After the current node is visited, we visit its adjacent vertices. If the condition of the visit is written as

if(G[nowMember][i]>0&&!isTraver[i])

It means that there is a relationship between these two vertices, and the adjacent vertices have not been visited, so we add this edge weight to the total weight, and then DFS. Let's consider the following (circled)
insert image description here
route in this case is the red part, we have reached D, B has been visited, we must not be able to perform DFS on B at this time, but the BD side executes Our code will not be added, so our DFS and edge weights should be executed separately.

code show as below

for(int i=0;i<numPerson;i++){
    
    
        if(G[nowMember][i]>0){
    
    
            totalTime+=G[nowMember][i];
            G[nowMember][i]=G[i][nowMember]=0;//避免多算
            if(!isTraver[i])
                DFS(i,gangNum,head,totalTime);
        }
    }

3. It<type.end() is wrong when looping with an iterator, the end condition should be it!=type,end(), where type is the container type

#include <iostream>
#include <map>
#include <string>
using namespace std;

const int Maxn=2010;

int G[Maxn][Maxn]={
    
    0};//电话记录
bool isTraver[Maxn]={
    
    false};//是否被访问过
int weight[Maxn]={
    
    0};//每个人的相关的权值

//输入
int N,K;

map<string,int> strToint;
map<int,string> intTostr;
map<string,int> nameTonum;//每个头目对应的成员数量

int numPerson=0;//人数

//名字转编号
int change(string str){
    
    
    if(strToint.find(str)!=strToint.end()){
    
    //这个名字已经出现过了
        return strToint[str];
    }
    else{
    
    
        strToint[str]=numPerson;
        intTostr[numPerson]=str;
        return numPerson++;
    }
}

//给一个顶点DFS
void DFS(int nowMember,int &gangNum,int &head,int &totalTime){
    
    
    isTraver[nowMember]=true;
    gangNum++;
    if(weight[nowMember]>weight[head])
        head=nowMember;
    for(int i=0;i<numPerson;i++){
    
    
        if(G[nowMember][i]>0){
    
    
            totalTime+=G[nowMember][i];
            G[nowMember][i]=G[i][nowMember]=0;//避免多算
            if(!isTraver[i])
                DFS(i,gangNum,head,totalTime);
        }
    }
}

void dfsTraver(){
    
    
    for(int i=0;i<numPerson;i++){
    
    
        //帮派人数,头目,总时间
        int gangNum=0,head=i,totalTime=0;
        if(!isTraver[i]){
    
    
            DFS(i,gangNum,head,totalTime);
            if(gangNum>2&&totalTime>K)
                nameTonum[intTostr[head]]=gangNum;
        }
    }
}

int main()
{
    
    
    string str1,str2;
    cin>>N>>K;
    for(int i=0;i<N;i++){
    
    
        int id1,id2,time;
        cin>>str1>>str2;
        id1=change(str1);
        id2=change(str2);
        cin>>time;
        G[id1][id2]+=time,weight[id1]+=time;
        G[id2][id1]+=time,weight[id2]+=time;
    }

    dfsTraver();
    cout<<nameTonum.size()<<endl;
    for(map<string,int>::iterator it=nameTonum.begin();it!=nameTonum.end();it++)
        cout<<it->first<<" "<<it->second<<endl;

    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346906&siteId=291194637