PATA1034_图的遍历(难度:⭐️⭐️⭐️⭐️)

DFS和BFS都有,注意这个题要包含一个连通图的所有边的权值。

这个是DFS的做法


#include <bits/stdc++.h>
using namespace std;
int W[2010], G[2010][2010];
int idnum = 1, k, head, maxTime = 0;
bool vis[2010];
unordered_map<string, int> sToInt;
map<string, int> ans;
unordered_map<int, string> iToString;
int stoifunc (string s) {
    if (sToInt[s] == 0) {
        sToInt[s] = idnum;
        iToString[idnum] = s;
        return idnum++;
    }
    else return sToInt[s];
}
void DFS (int id, int &sum, int &cnt) {
    vis[id] = true;
    cnt++;
    if (W[id] > maxTime) {
        maxTime = W[id];
        head = id;
    }
    for (int i = 1; i < idnum; i++) {
        if (G[id][i] > 0) {
            sum += G[id][i];
            G[id][i] = G[i][id] = 0;
            if (vis[i] == false)
                DFS (i, sum, cnt);
        }
    }
}
void DFStrave () {
    for (int i = 1; i < idnum; i++) {
        int sum = 0, cnt = 0;
        maxTime = 0;
        if (vis[i] == false) {
            DFS (i, sum, cnt);
            if (sum > k && cnt > 2) {
                ans[iToString[head]] = cnt;
            }
        }
    }
}
int main() {
    fill (G[0], G[0] + 2010 * 2010, 0);
    string a, b;
    int n, cost;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> cost;
        int id1 = stoifunc(a), id2 = stoifunc(b);
        G[id1][id2] += cost;
        G[id2][id1] += cost;
        W[id1] += cost; W[id2] += cost;
    }
    DFStrave();
    printf ("%d\n", ans.size());
    for (auto it : ans) {
        printf ("%s %d\n", it.first.c_str(), it.second);
    }
}

这个是BFS的解法:


#include <bits/stdc++.h>
using namespace std;
int W[2010], G[2010][2010];
int idnum = 1, k, head, maxTime = 0;
bool vis[2010];
unordered_map<string, int> sToInt;
map<string, int> ans;
unordered_map<int, string> iToString;
int stoifunc (string s) {
    if (sToInt[s] == 0) {
        sToInt[s] = idnum;
        iToString[idnum] = s;
        return idnum++;
    }
    else return sToInt[s];
}
void BFS (int id, int &sum, int &cnt) {
    queue<int> q;
    q.push(id);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        vis[now] = true;
        cnt++;
        if (maxTime < W[now]) {
            maxTime = W[now];
            head = now;
        }
        for (int i = 1; i < idnum; i++) {
            if (G[now][i] > 0) {
                sum += G[now][i];
                G[now][i] = G[i][now] = 0;
                if (vis[i] == false) {
                    q.push(i);
                    vis[i] = true;
                }
            }
        }
    }
}
void BFStrave () {
    for (int i = 1; i < idnum; i++) {
        int sum = 0, cnt = 0;
        maxTime = 0;
        if (vis[i] == false) {
            BFS (i, sum, cnt);
            if (sum > k && cnt > 2) {
                ans[iToString[head]] = cnt;
            }
        }
    }
}
int main() {
    fill (G[0], G[0] + 2010 * 2010, 0);
    string a, b;
    int n, cost;
    scanf ("%d %d", &n, &k);
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> cost;
        int id1 = stoifunc(a), id2 = stoifunc(b);
        G[id1][id2] += cost;
        G[id2][id1] += cost;
        W[id1] += cost; W[id2] += cost;
    }
    BFStrave();
    printf ("%d\n", ans.size());
    for (auto it : ans) {
        printf ("%s %d\n", it.first.c_str(), it.second);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值