BOJ 334 sweep topology sorting

Source: Internet
Author: User
Tags cmath

Very bare a topological sort.

/* ID:CUGB-WWJ prog:lang:c++ */#include <iostream> #include <vector> #include <list> #include <ma p> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> # Include <algorithm> #include <functional> #include <numeric> #include <utility> #include < sstream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include & lt;cctype> #include <string> #include <cstring> #include <cmath> #include <ctime> #define 
MAXN 11050 #define INF 1000000000 #define L (x) x<<1 #define R (x) x<<1|1 #define PI ACOs ( -1.0) #define EPS 1e-3
using namespace Std; struct Node {int V, next;}
EDGE[100 * MAXN];
int HEAD[MAXN], E, N, Tx[maxn], IN[MAXN], TXP[MAXN];
    void Insert (int x, int y) {edge[e].v = y;
    Edge[e].next = Head[x];
HEAD[X] = e++;
    } void Topsort () {queue<int>q; for (int i = 1; i<= N;
    i++) if (in[i] = = 0) {Q.push (i); txp[i] = Tx[i];}
        while (!q.empty ()) {int u = q.front ();
        Q.pop ();
            for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; TXP[V] = max (Txp[v], Txp[u] + tx[v]);
            Use Max to ensure that the task's prerequisite tasks can be completed in[v]--;
        if (in[v] = = 0) q.push (v);
    }} int ans = 0;  for (int i = 1; I <= n; i++) ans = max (Txp[i], ans);
Use Max to find out when the latest point is completed cout << ans << endl;
        } int main () {while (scanf ("%d", &n)! = EOF && n) {int t, x;
        Memset (Head,-1, sizeof (head));
        memset (in, 0, sizeof (in));
        memset (txp, 0, sizeof (TXP));
        e = 0;
            for (int i = 1; I <= n; i++) {scanf ("%d", &tx[i]);
            scanf ("%d", &t);
                while (t--) {scanf ("%d", &x);
                Insert (i, x);
            in[x]++;
      }  } topsort ();
} return 0;
 }


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.