POJ 3687 Labeling Balls "topological sort priority queue"

Source: Internet
Author: User

Test instructions: Give n individuals, M weight relationship, to meet the severity of the given and meet the number of small as far as possible in the preceding sequence

Because the input is a heavier than B, but we're looking for a lighter one, so we need to build it backwards.

This article http://blog.csdn.net/scf0920/article/details/28108243 the reverse Build

And then use the priority queue to implement the see this article

Http://ycool.com/post/u9ahrwg#algo3

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten #definemod=1e9+7; One using namespacestd; A  -typedefLong LongLL; - Const intmaxn=1005; the intG[MAXN][MAXN], out[MAXN],ANS[MAXN]; -  - intMain () { -     intt,n,m,u,v,tmp,i,j; +scanf"%d",&t); -      while(t--){ +Memset (G,0,sizeof(g)); AMemset out,0,sizeof( out)); atCin>>n>>m; -          for(i=0; i<m;i++){ -Cin>>u>>v; -             if(!G[u][v]) { -g[u][v]=1; -                  out[u]++; in             } -         } topriority_queue<int>Q; +          for(i=1; i<=n;i++){ -             if( out[i]==0) Q.push (i); the         } *          $          for(i=n;i>=1; i--){Panax Notoginseng             if(Q.empty ()) Break; -U=q.top (); Q.pop ();//Take out the largest number in the queue theAns[u]=i;//make the large number as far back as possible +              for(j=1; j<=n;j++){ A                 if(G[j][u]) { theg[j][u]=0; +                      out[j]--; -                     if( out[j]==0) Q.push (j); $                 } $                  -             } -         } the          -         if(i) printf ("-1\n");Wuyi         Else{ theprintf"%d", ans[1]); -              for(i=2; i<=n;i++) printf ("%d", Ans[i]); Wuprintf"\ n"); -         }         About     } $     return 0; -}
View Code

Go---Go----the topological sort of winter vacation learn to forget 7788 said = = This problem of the inverse of the map to understand

POJ 3687 Labeling Balls "topological sort priority queue"

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.