码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1034. Head of a Gang[bug]

时间:2014-11-25 00:05:49      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   for   on   div   

有一个两分的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;
}

 

PAT 1034. Head of a Gang[bug]

标签:style   blog   io   ar   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/lailailai/p/4119845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!