hdu 2647 Reward
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=2647
Reward
Description
Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.
Input
One line with two integers n and m ,stands for the number of works and the number of demands .(n≤10000,m≤20000)
then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.
Output
For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.
Sample Input
2 1
1 2
2 2
1 2
2 1
Sample Output
1777
-1
简单的拓扑排序。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<vector> 7 #include<queue> 8 #include<map> 9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::find; 13 using std::sort; 14 using std::map; 15 using std::pair; 16 using std::queue; 17 using std::vector; 18 using std::multimap; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 10010; 29 typedef unsigned long long ull; 30 struct Node { 31 int vex, val; 32 Node(int i = 0, int j = 0) :vex(i), val(j) {} 33 }; 34 struct TopSort { 35 vector<int> G[N]; 36 int topNum, inq[N]; 37 inline void init(int n) { 38 topNum = 0; 39 cls(inq, 0); 40 rep(i, n) G[i].clear(); 41 } 42 inline void built(int m) { 43 int a, b; 44 rep(i, m) { 45 scanf("%d %d", &a, &b); 46 --a, --b; 47 inq[a]++; 48 G[b].push_back(a); 49 } 50 } 51 inline void bfs(int n) { 52 int ans = 0; 53 queue<Node> q; 54 rep(i, n) { 55 if (!inq[i]) { q.push(Node(i, 888)); topNum++; } 56 } 57 while (!q.empty()) { 58 Node t = q.front(); q.pop(); 59 ans += t.val; 60 rep(i, sz(G[t.vex])) { 61 if (--inq[G[t.vex][i]] == 0) q.push(Node(G[t.vex][i], t.val + 1)), topNum++; 62 } 63 } 64 printf("%d\n", topNum == n ? ans : -1); 65 } 66 }work; 67 int main() { 68 #ifdef LOCAL 69 freopen("in.txt", "r", stdin); 70 freopen("out.txt", "w+", stdout); 71 #endif 72 int n, m; 73 while (~scanf("%d %d", &n, &m)) { 74 work.init(n); 75 work.built(m); 76 work.bfs(n); 77 } 78 return 0; 79 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥