c_ybt_奖金(拓扑排序求最长路+递推)

每位参加会谈的代表提出了自己的意见:“我认为员工 a 的奖金应该比 b 高!”。
总经理决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。
每位员工奖金最少为100元。

思路:入度为0的点是起点也是奖金为100的点,往后的点都是只加1会使总奖金最少

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m,in[N],f[N];
vector<int> g[N];
int topo() {
    queue<int> q; 
    int ans=0, cnt=0;
    for (int i=1; i<=n; i++) if (in[i]==0) {
        q.push(i);
        f[i]=100, ans+=100;
    }
    while(!q.empty()) {
        int u=q.front(); 
        q.pop(), cnt++;
        for (int v : g[u]) {
            if (--in[v]==0) {
                q.push(v);
                f[v]=f[u]+1, ans+=f[v]; //由于边权都是1,所以不需要if判断是不是最长路
            }
        }
    }
    return cnt==n ? ans : -1;
}
int main() {
    scanf("%d %d", &n,&m);
    for (int i=0; i<m; i++) {
        int u,v; scanf("%d %d", &u,&v);
        g[u].push_back(v), in[v]++;
    }
    int ans=topo();
    if (ans==-1) printf("Poor Xed");
    else printf("%d",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/wdt1/p/13909556.html