$ Self ~ Problem ~ C ~ :~ Samsara $

Source: Internet
Author: User

Therefore, Clu + = long cond removes the strongly connected CPP.

Background:

Everyone in this \ (canman \) world knows that the greatest monk in the world -- \ (felling \) has ended the vast disaster of \ (canman, protects the \ (canman \) peace. At the end of the treasure, the near God \ (felling \) is in the final confrontation with the monks that fall into the magic path, the inner of the great disaster \ (neyii. Mastering the power of reincarnation, he can reverse the passage of reincarnation, resulting in an exclusive singularity explosion, more powerful than the general explosion several times.

Description:

It is known that the current \ (neyii \) opens the magic territory and the \ (felling \) is surrounded. \ (Felling \) has been greatly restricted in the magic Xinjiang, and even the time and space power are not allowed. However, magic Xinjiang is a dual-sided edge that is surrounded by \ (felling \) and \ (neyii \) exposes its meridian to \ (felling. \ (Felling \) knows that it will eventually fall, so I plan to make a desperate bet and use my own power of reincarnation to detonate the channels of \ (neyii!

It is known that the meridian map of the current \ (neyii \) is composed of hundreds of acupoints and meridian links, and the meridian is responsible for connecting each acupoint. It is well known that the flow of luck in the meridian is one-way, otherwise it will lead to a conflict of luck and serious consequences of the burst of the Meridian. If an acupoint is detonated, the meridians starting from this acupoint will be decompressed. However, if a acupoint does not have any meridians flowing into the supply, The acupoint will enter a closed Pulse State, making \ (felling \) unable to trigger its singularity explosion. And the energy contained in the \ (felling \) point will be returned \ (felling \). Now \ (felling \) has not been able to release the singularity explosion many times, he wants to at most \ (k \) Explosion, obtain as much energy as possible. Of course, you do not have to use \ (k \) for an explosion. The question is not guaranteed to have no loops, but there is no overlap. Ensure that the weight value is not negative and there is no self-ring.

Input Format:

In the first line, the three positive integers \ (n, m, k \) indicate the number of acupoints, the number of meridians, and the maximum number of explosions.

The second line contains \ (n \) integers \ (data [I] \), which respectively indicate the energy of the \ (I \) acupoint.

The following \ (M \) rows have three positive integers in each row. \ (X, Y \) indicates a one-way flow meridian from \ (x \) to \ (Y.

Output Format:

A row, an integer, indicates the maximum number of available energy.

Input example:
7 7 310 2 8 4 9 5 71 21 31 42 53 63 74 7
Output example:
24
Data size:

For \ (10 \) % data, \ (1 \ Leq n \ Leq 10, 1 \ Leq m \ Leq 20 \).

The meridian chart has no loops for the \ (30 \) % data.

For data of another \ (10 \) %, the inbound degree of only one vertex is \ (0 \).

For \ (100 \) % data: \ (1 \ Leq n \ Leq 10000 \), \ (1 \ Leq m \ Leq 500003 \), \ (1 \ Leq k \ leq100003 \)

All edge permissions \ (\ Leq 1000 \)

First, simplify the question:

You now have a General directed graph of \ (n \) vertex \ (M \) edge. You can cause an explosion at most \ (k \) points, each explosion can obtain the point right of this point. After the explosion, all outbound edges starting from this point will be decommissioned. If a vertex does not have an inbound edge, an explosion cannot be performed. Request maximization point and right.

First, we consider a directed acyclic graph.

You can find that if you want to detonate \ (now_1 \) and \ (now_2 \), if \ (now_2 \) is from \ (now_1, then we must first detonate \ (now_2 \) and then detonate \ (now_1 \) to gain greater value.

For example, if I want to detonate the \ (3 and \) \ (6 \) nodes, I must first detonate them \ (6 \), if I first detonate \ (3 \), \ (6 \) cannot be detonated.

If \ (now_1 \) and \ (now_2 \) are similar to a "parallel relationship", you do not need to consider each other. For example, \ (3 \) and \ (4 \).

Therefore, we can find that all the points except those with an inbound value of \ (0 \) on this \ (DAG \) can be selected. Because there is always a legal order for us to select all vertices. Therefore, on \ (DAG \), we only need to delete all the vertices with an inbound value of \ (0 \), and then sort \ (for \) to \ (k. \ (30 \) points.

So let's consider this situation.

We can find that, if we start to detonate from \ (4 \), then \ (4 \) then detonate \ (3 \), \ (3 \) \ (2 \), \ (2 \), followed by only one \ (1 \) not detonated. Of course, the same is true for a different starting point. Therefore, we only need to discard one vertex, so other vertices can be selected. Delete the smallest vertex.

Then, let's look at the general directed graph.

In this case, the original \ (\ {1, 2, 3, 4 \} \) loops can be selected, because an inbound edge \ (\ {5-> 1 \} \) is added, we find \ (\ {1, 2, 3, 4 \}\) it can be seen as a point.

Then the algorithm is obtained: For each strongly connected component:

If the strong Unicom component has an inbound level, all the points in the strong Unicom component can be selected.

If the strong Unicom component has no inbound traffic, you can select all other points after removing a vertex.

The algorithm flow is as follows:

  1. Point Reduction
  2. For each edge, if \ (scc_1 \) is run to \ (scc_2 \), \ (scc_2 \) has an inbound degree, flag \ (flag [scc_2] = 1 \)
  3. \ (For \) All \ (scc_ I \). If \ (flag [scc_ I] = 1 \), add all vertices in \ (SCC \) to the array. Otherwise, remove the vertices with the smallest vertices and add the remaining vertices to the array.
  4. For the array mentioned above, \ (k \) is large before \ (nth \ _ element \) sorting, and then vertex right is calculated.

The algorithm is actually a wonderful greedy.

Bidding Process:

#include <algorithm>#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <cmath>#include <map>#define For1(i, A, B) for(register int i = (A), i##_end_ = (B); i <= i##_end_; ++ i)#define For2(i, A, B) for(register int i = (A), i##_end_ = (B); i >= i##_end_; -- i)#define MEM(Now, K) memset(Now, K, sizeof(Now))#define CPY(Now, K) memcpy(Now, K, sizeof(Now))#define Debug(Now) (cerr << Now << endl)#define Min(A, B) (A < B ? A : B)#define Max(A, B) (A < B ? B : A)#define SCPY(A, B) strcpy(A, B)#define Inf 0x7fffffff#define RE register#define IL inline#define MAXN 100010#define MAXM 500010#define _X first#define _Y secondusing namespace std ;typedef unsigned long long ULL ;typedef pair<long long, int>  PLI;typedef pair<int, int> PII;typedef unsigned int UINT;typedef long double LDB;typedef long long LL ;typedef double DB ;IL int Read(){    LL X = 0, F = 1 ;  char ch = getchar() ;    while(ch < '0' || ch > '9'){ if(ch == '-') F = - 1 ; ch = getchar() ; }    while(ch <= '9' && ch >= '0') X = (X << 1) + (X << 3) + (ch ^ 48), ch = getchar() ;    return X * F ;}IL double DBRead(){    double X = 0, Y = 1.0 ; LL W = 0 ; char ch = 0 ;    while(! isdigit(ch)) { W |= ch == '-' ; ch = getchar() ; }    while(isdigit(ch)) X = X * 10 + (ch ^ 48), ch = getchar() ;    ch = getchar();    while(isdigit(ch)) X += (Y /= 10) * (ch ^ 48), ch = getchar() ;    return W ? - X : X ;}IL void Print(/*LL*/ LL X){     if(X < 0) putchar('-'), X = - X ;     if(X > 9) Print(X / 10) ; putchar(X % 10 + '0') ;     //cout << endl ;     //cout << " " ;}LL N, M, K, Data[MAXN], Ans ;struct Node{    LL From, To, Next ;}Edge[MAXM] ;LL Head[MAXN], Total ;void Add(LL F, LL T){    Total ++ ;    Edge[Total].From = F ;    Edge[Total].To = T ;    Edge[Total].Next = Head[F] ;    Head[F] = Total ;}LL Dfn[MAXN], Low[MAXN], Deep, Cnt, Flag[MAXN] ;LL Stack[MAXN], Insta[MAXN], Top ;LL Belong[MAXN], Est[MAXN], MIN[MAXN] ;LL Finally[MAXN], All ;void Tarjan(LL Now){    Dfn[Now] = Low[Now] = ++ Deep ;    Stack[++ Top] = Now ; Insta[Now] = 1 ;    for(LL i = Head[Now]; i; i = Edge[i].Next){        if(! Dfn[Edge[i].To]){            Tarjan(Edge[i].To) ;            Low[Now] = min(Low[Now], Low[Edge[i].To]) ;        }   else if(Insta[Edge[i].To])            Low[Now] = min(Low[Now], Dfn[Edge[i].To]) ;    }    if(Low[Now] == Dfn[Now]){        Cnt ++ ; LL Pass ;        do{            Pass = Stack[Top --] ;            if(Est[Cnt] > Data[Pass])                MIN[Cnt] = Pass, Est[Cnt] = Data[Pass] ;            Belong[Pass] = Cnt ;            Insta[Pass] = 0 ;        }while(Now != Pass) ;    }}bool CMP(LL X, LL Y){    return X > Y ;}int main() {    freopen("samsara.in", "r", stdin) ;    freopen("samsara.out", "w", stdout) ;    N = Read(), M = Read(), K = Read() ;    memset(Est, 127, sizeof(Est)) ;    for(LL i = 1; i <= N; i ++)        Data[i] = Read() ;    for(LL i = 1; i <= M; i ++){        LL F = Read(), T = Read() ;        Add(F, T) ;    }    for(LL i = 1; i <= N; i ++)    if(! Dfn[i]) Tarjan(i) ;    for(LL i = 1; i <= M; i ++){        if(Belong[Edge[i].From] != Belong[Edge[i].To])            Flag[Belong[Edge[i].To]] = 1 ;    }    for(LL i = 1; i <= Cnt; i ++)        if(! Flag[i])            Data[MIN[i]] = - 1 ;    for(LL i = 1; i <= N; i ++){        if(Data[i] == -1) continue ;        Finally[++ All] = - Data[i] ;    }    nth_element(Finally + 1, Finally + K + 1, Finally + All + 1) ;    for(LL i = 1; i <= K; i ++)        Ans +=  - Finally[i] ;    printf("%lld", Ans) ;    fclose(stdin) ; fclose(stdout) ;    return 0 ;  }

$ Self ~ Problem ~ C ~ :~ Samsara $

Related Article

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.