Puzzle Report: Hdu 2647 Reward (topological sort)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2647

Problem Descriptiondandelion ' s uncle is a boss of a factory. As the Spring Festival is coming, he wants to distribute rewards to his workers. Now he had a trouble about what to distribute the rewards. The workers would compare their rewards, and some one may has 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'll be at least 888, because it's a lucky number.
PresidentThe uncle of the Dandelion is the boss of the factory. with the advent of the Spring Festival, he wants to distribute prizes to his workers. Now he's having trouble assigning rewards. Workers compare their rewards, and some may ask for a bonus, just as a ' s reward exceeds B's. Dandelion to meet all the requirements, of course, he wants to use the least money. The reward for each piece will be at least 888, as this is a lucky number. Inputone line with a 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 the integers a and B, stands for a ' s reward should is more than B ' s.:a row has two integers n and m, representing the number of works and the number of requirements (n <= 10000,m <= 20000),
then the M line, each line contains two integers a and B, the reward for a should be more than B. Outputfor every case, print the least money dandelion ' s uncle needs to distribute. If It's impossible to fulfill all the works ' demands, print-1. For each case, print the dandelion's uncle needs to distribute the fewest money. If the requirements for all works are not met, print-1. Sample Input2 1Sample output1777-1problem-solving ideas: topological sequencing. Because the former reward more than the latter, but also require the least amount of money to give the reward, so only the former more than the latter 1 of the award. If you follow the general thinking before, the bonus point bonus is less, in the topology bonus node number, it is bound to affect the previous topology of the node number of the reward, because it is possible to reward more than 1 of the nodes, so it is not good to deal with the topology before the node number of the reward. Therefore, this question needs to think in turn, will reward the node number less points to the number of bonus nodes, so when the topology reward is less, the reward is more than the reward less than 1, and eventually add all the rewards plus 888*n. The data given by the topic is too large to avoid timeouts, using adjacency table approach. You can also look at this blog post on this topic: HDU 2647 reward "topological sort" AC Code:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=10010;4vector<int> VEC[MAXN];//adjacency table, where each node holds another endpoint of the edge to which it is attached5queue<int>que;6 intN,M,U,V,INDEG[MAXN],CNT[MAXN];//record the degree of each node, CNT records each node to receive the reward7 BOOLTopsort () {8     intnum=0;9      for(intI=1; i<=n;++i)if(! Indeg[i]) Que.push (i);//pre-processing, the number of nodes entering the 0 is first enqueuedTen      while(!Que.empty ()) { One         intNow=que.front (); Que.pop (); num++; A          for(unsignedintI=0; I<vec[now].size (); + +i) { -             if(--indeg[vec[now][i]]==0) Que.push (Vec[now][i]); -cnt[vec[now][i]]=cnt[now]+1;//just 1 more than the team element bonus the         } -     } -     if(num==n)return true;//return 1 If conditions are met -     Else return false; + } - intMain () + { A      while(cin>>n>>m) { at          for(intI=1; i<=n;++i) vec[i].clear ();//Clear All -memset (Indeg,0,sizeof(indeg));//clarity of all vertices 0 -memset (CNT,0,sizeof(CNT)); -          while(m--){ -Cin>>u>>v; -Vec[v].push_back (U);//v point to U inindeg[u]++;//the penetration of U plus 1 -         } to         if(Topsort ()) { +             intsum=0; -              for(intI=1; i<=n;++i) sum+=Cnt[i]; thecout<< (sum+888*n) <<Endl; *         } $         Elsecout<<"-1"<<Endl;Panax Notoginseng     } -     return 0; the}

Puzzle Report: Hdu 2647 Reward (topological sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.