【題目描述】
由於無敵的凡凡在2005年世界英俊帥氣男總決選中勝出,Yali Company總經理Mr.Z心情好,決定給每位員工發獎金。公司決定以每個人本年在公司的貢獻爲標準來計算他們得到獎金的多少。
於是Mr.Z下令召開mm方會談。每位參加會談的代表提出了自己的意見:“我認爲員工a的獎金應該比b高!”Mr.Z決定要找出一種獎金方案,滿足各位代表的意見,且同時使得總獎金數最少。每位員工獎金最少爲100100元。
【輸入】
第一行兩個整數nn,mm,表示員工總數和代表數;
以下mm行,每行2個整數aa,bb,表示某個代表認爲第a號員工獎金應該比第b號員工高。
【輸出】
若無法找到合理方案,則輸出“Poor Xed
”;否則輸出一個數表示最少總獎金。
【輸入樣例】
2 1 1 2
【輸出樣例】
201
【提示】
【數據規模】
80%的數據滿足:n≤1000n≤1000,m≤2000m≤2000;
100%的數據滿足:n≤10000n≤10000,m≤20000m≤20000。
#include <bits/stdc++.h> using namespace std; void show(vector<vector<int>> &a) { for (int i = 1; i < a.size(); i++) { cout << i << ": "; for (int j = 0; j < a[i].size(); j++) { cout << a[i][j] << " "; } cout << endl; } } void show(vector<int> &a) { for (int i = 1; i < a.size(); i++) { cout << a[i] << " "; } cout << endl; } int topo(vector<vector<int>> &a) { int ans = 0; // 獎金 int cnt = 0; // 員工數量 int factor = 100; // 最少獎金 vector<bool> visited(a.size()); while (cnt < a.size() - 1) { vector<int> inDeg(a.size()); // 入度 for (int i = 1; i < a.size(); i++) { if (visited[i]) { continue; } for (int j = 0; j < a[i].size(); j++) { inDeg[a[i][j]] += 1; } } // show(inDeg); bool found = false; for (int i = 1; i < inDeg.size(); i++) { if (visited[i]) { continue; } if (inDeg[i] == 0) { visited[i] = true; ans += factor; cnt += 1; found = true; } } if (!found) { return -1; } factor += 1; } return ans; } int main() { // freopen("in.txt", "r", stdin); int n, m; // 頂點數n,邊數m scanf("%d%d", &n, &m); vector<vector<int>> a(n + 1); // 鄰接表 for (int i = 1; i <= m; i++) { int x, y; // x比y獎金多 scanf("%d%d", &x, &y); a[y].push_back(x); // y比x少 } // show(a); int ans = topo(a); if (ans < 0) { printf("Poor Xed\n"); } else { printf("%d\n", ans); } return 0; }