zoukankan      html  css  js  c++  java
  • 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
    ", len);
        for (int i=0; i<len; i++) {
            printf("%s %d
    ", mans[heads[i].first].name.c_str(), heads[i].second);
        }
        
        return 0;
    }
  • 相关阅读:
    F广搜
    Python中range和xrange的异同之处
    数组中出现次数超过一半的数字
    iOS开发之剖析&quot;秘密&quot;App内容页面效果(一)
    Balloon Comes!
    scikit-learn: isotonic regression(保序回归,非常有意思,仅做知识点了解,但差点儿没用到过)
    C#数据缓存介绍及Caching通用帮助类整理
    SVN Working copying &#39;xxxxx/xxxx/xxxx&#39; locked
    读书笔记-APUE第三版-(7)进程环境
    UVA 10555
  • 原文地址:https://www.cnblogs.com/lailailai/p/4119845.html
Copyright © 2011-2022 走看看