PAT 1034. Head of a Gang[bug]

有一个两分的case出现段错误,真是没救了,估计是要写bfs的形式,可能栈溢出了

#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

int G[1001][1001] = {0};

class Man {
public:
    int id;
    string name;
    vector<int> adj;
    bool visited;
    Man(string &n): name(n), visited(false){}
};

int get_gid(string &name, unordered_map<string, int>& n2i, int &gid, vector<Man>& mans) {
    int id = gid;
    auto iter = n2i.find(name);
    if (iter == n2i.end()) {
        n2i.insert(make_pair(name, gid++));
        mans.push_back(Man(name));
    } else {
        id = iter->second;
    }
    return id;
}

void dfs(vector<Man>& mans, int curi, int K, int &head, int &max_weight, int &count, int &rtotal) {
    if (mans[curi].visited) {
        return;
    }
    count++;
    mans[curi].visited = true;
    
    int weight = 0;
    int len = mans[curi].adj.size();
    for (int i=0; i<len; i++) {
        int r = G[curi][mans[curi].adj[i]];
        weight += r;
        if (mans[mans[curi].adj[i]].visited) {
            continue;
        }
        rtotal += r;
    }
    
    if (weight > max_weight) {
        head = curi;
        max_weight = weight;
    }
    for (int i=0; i<len; i++) {
        int r = G[curi][mans[curi].adj[i]];
        dfs(mans, mans[curi].adj[i], K, head, max_weight, count, rtotal);
    }
}

class MyCmp{
private:
    vector<Man>* mans;
public:
    bool operator()(const pair<int, int>& a, const pair<int, int> &b) {
        return (*mans)[a.first].name < (*mans)[b.first].name; 
    }
    MyCmp(vector<Man>* ms) {mans = ms;}
};

int main() {
    int N, K;
    scanf("%d%d", &N, &K);
    
    unordered_map<string, int> name2id;
    vector<Man> mans;

    int gid = 0;
    
    char name1[4];
    char name2[4];
    int time;
    
    int ida, idb;
    
    for (int i=0; i<N; i++) {
        scanf("%s%s%d", name1, name2, &time);
        string s1(name1);
        string s2(name2);
        ida = get_gid(s1, name2id, gid, mans);
        idb = get_gid(s2, name2id, gid, mans);
        if (!G[ida][idb]) {
            mans[ida].adj.push_back(idb);
        }
        if (!G[idb][ida]) {
            mans[idb].adj.push_back(ida);
        }
        G[ida][idb] += time;
        G[idb][ida] += time;
    }
    
    
    vector<pair<int, int> > heads;
    int count, head, max_weight, rtotal;
    for (int i=0; i<gid; i++) {
        if (mans[i].visited) {
            continue;
        }
        count = 0;
        max_weight = 0;
        rtotal = 0;
        dfs(mans, i, K, head, max_weight, count, rtotal);
        if (count > 2 && rtotal > K) {
            heads.push_back(make_pair(head, count));
        }
    }
    
    sort(heads.begin(), heads.end(), MyCmp(&mans));
    
    int len = heads.size();
    
    printf("%d\n", len);
    for (int i=0; i<len; i++) {
        printf("%s %d\n", mans[heads[i].first].name.c_str(), heads[i].second);
    }
    
    return 0;
}

 

posted @ 2014-11-24 22:52  卖程序的小歪  阅读(526)  评论(1编辑  收藏  举报