c_ybt_獎金(拓撲排序判環+遞推)
阿新 • • 發佈:2020-11-01
每位參加會談的代表提出了自己的意見:“我認為員工 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]; } } } 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; }