zoukankan      html  css  js  c++  java
  • HDU

    题目链接
    题目大意:你有n个人和m对关系,每对关系规定给前者的钱必须比后者多。问你给n个人的最少工资是多少。
      这题如果正向建图的话不太好做,因为我们要知道第一个人应该给多少好就得求从第一个点到最后一个点的距离,但是如果我们反向建图的话,就很好做了。我们反向建图,每条边连着的下一个点给的钱比当前的点多1就好。注意可能有些点的关系是平行的。

    const int maxn = 1e4+10;
    vector<int> e[maxn];
    int n, m, in[maxn]; ll ans = 0;
    bool topo() {
        queue<P> qe;
        for (int i = 1; i<=n; ++i)
            if (!in[i]) qe.push(P(i, 888));
        ans = 0; int cnt = 0;
        while(!qe.empty()) {
            ++cnt;
            P t = qe.front(); qe.pop();
            int u = t.first, w = t.second;
            ans += (ll)w; 
            for (auto v : e[u])
                if (--in[v] == 0) qe.push(P(v, w+1));
        }
        return cnt != n;
    }
    int main(void) {
        while(~scanf("%d%d", &n, &m)) {
            for (int i = 0, a, b; i<m; ++i) {
                scanf("%d%d", &a, &b);
                e[b].push_back(a);
                ++in[a];
            }
            if (topo()) printf("-1
    ");
            else printf("%lld
    ", ans);
            zero(in);
            for (int i = 0; i<=n; ++i) e[i].clear();
        }
        return 0;
    }
    
  • 相关阅读:
    2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
    JDBC连接MySQL数据库
    MySQL实习训练1
    I Hate It
    Just a Hook
    Mayor's posters
    asd的甩锅计划
    程序员的恋情
    Radical and array
    Highways
  • 原文地址:https://www.cnblogs.com/shuitiangong/p/12634388.html
Copyright © 2011-2022 走看看