Bzoj 4010: [HNOI2015] food production (priority queue + topological sorting)

Source: Internet
Author: User

4010: [HNOI2015] Food production time Limit: 5 Sec Memory Limit: MB
Submit: 1199 Solved: 583
[Submit] [Status] [Discuss] Description

The famous gourmet Small A is invited to the ATM Grand Hotel for its tasting dishes.

The ATM hotel has a N-course menu for small A, and the hotel has a sequential number from 1 to n for the estimated quality of the dishes, with the highest estimated quality of the dishes numbered 1. Due to the problem of the taste collocation of the dishes, some dishes must be made before other dishes, specifically, there is a total of M bar such as "I dish" must be ' prior to the production of J "limit, we will be abbreviated as <i,j>. Now, the hotel would like to find an optimal order of the dishes, so that small a can try to eat the highest quality dishes: that is, (1) in order to meet all the restrictions on the premise, 1th dishes "as far as possible" priority production, (2) in order to meet all the restrictions, 1th dishes "as far as possible" priority, 2nd dishes " (3) In the case of all restrictions, 1th and 2nd dishes "as far as possible" priority, 3rd dishes "as far as possible" priority production, (4) in meeting all the restrictions, 1th and 2nd and 3rd dishes "as far as possible" priority, 4th dishes "as far as possible" first production, (5) and so on. Example 1: A total of 4 dishes, two restrictions on <3,1>, &LT;4,1&GT, then the order of production is 3,4,1,2. Example 2: A total of 5 dishes, two restrictions on <5,2>, &LT;4,3&GT, then the order of production is 1,5,2,4,3. Example 1, first consider 1, because there are restrictions <3,1> and <4,1&gt, so only after the production of 3 and 4 to make 1, and according to (3), 3rd should be "as far as possible" than the 4th priority, so the current can determine the first three courses of production sequence is 3,4,1; next consider 2, Determine the final order of production is 3,4,1,2.  Example 2, the first production of 1 is not against the limit, the next consideration of the 2 o'clock has a <5,2> limit, so the next first to make 5 and then make 2, the next 3 when there are <4,3> restrictions, so the next first make 4 and then make 3, so the final order is 1,5,2,4,3. Now you need to ask for the best order of the dishes to be made. No solution output "impossible!" (without quotation marks, uppercase letters, other lowercase) Input

The first line is a positive integer d, which represents the number of data groups.

Next is the Group D data. For each set of data: the first row of two positive integers separated by spaces N and M, respectively, indicating the number of dishes and the order limit of the entries. Next M-line, two positive integers x, y for each line, indicates that the "X-dishes must precede the production of the Y-dishes" limit. (Note: There may be exactly the same limit in the M-bar limit) Output

The output file contains only D rows, N integers per row, indicating the optimal order of dishes, or

"Impossible!" Represents no solution (without quotation marks). Sample Input3
5 4
5 4
5 3
4 2
3 2
3 3
0 S
2 3
3 1
5 2
5 2
4 3Sample Output1 5 3) 4 2
impossible!
1 5 2) 4 3HINT

"Sample Interpretation"


The second set of data also requires dishes 1 before the dishes 2 production, dishes 2 before the dishes 3 production, dishes 3 before
Dishes 1 are made, and this is impossible to satisfy anyway, thus leading to no solution.
100% of the data meets n,m<=100000,d<=3.

Source

[Submit] [Status] [Discuss] Priority queue + topology sequencing

Topological sorting in the solution is two ways, one is constantly looking for the entry into the 0 points into the queue, the team is in respect of all of his subsequent nodes into the degree--and the other is constantly looking for a point of 0 points to join the queue, when the team out of all of his predecessor node out--and finally according to the number of nodes to determine Because this problem requires a small number of nodes to come in front, all of us choose the second way to find a topological sort, each time the node number is thrown into the queue, the pop-up will be the number of points priority pop-up, and placed at the end of the topological sequence.

 #include <iostream> #include <cstdio> #include <cstring> #include < cmath> #include <queue> #define N 100003using namespace Std;int next[n],point[n],v[n],ins[n],ans[n],num,chus[ N];int n,m,t,tot;void Add (int x,int y) {tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y;} int main () {scanf ("%d", &t); for (int t=1; t<=t; t++) {scanf ("%d%d", &n,&m); Tot=0;memset (point,0,sizeof (point)); Memset (Next,0,sizeof (next)); Memset (ins,0, sizeof (INS)); memset (Chus,0,sizeof (Chus)); for (int i=1;i<=m;i++) {int x, Y, scanf ("%d%d", &x,&y); add (y,x); chus[x]++; }num=0;priority_queue<int> p;for (int i=1;i<=n;i++) if (!chus[i]) {num++; P.push (i);}  int T=0;while (!p.empty ()) {int now=p.top (); P.pop (); t++; Ans[n-t+1]=now;  for (int i=point[now];i;i=next[i]) {chus[v[i]]--;  if (!chus[v[i]]) {num++;  P.push (V[i]); }}}if (num!=n) printf ("impossible!\n"), else {for (int i=1;i<=n;i++) printf ("%d", ans[i]); printf ("\ n"); }}}


Bzoj 4010: [HNOI2015] food production (priority queue + topological sorting)

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.