FZU-1719 Spy Network (strong connectivity component)

Source: Internet
Author: User

The main idea: give a map, and the value of n points (after buying this point, the point of departure from the point will be dyed)

Ask at least how much to spend to buy some, in order to make all the points of this picture is dyed
If not all points are dyed, the output cannot be dyed at the minimum value of the point

Problem-solving ideas: First Find all the strong connected components, and then find the minimum value of all the points within each strong connected component, because the point within the same strong connected component only need to buy one can be fully dyed
Then the point is shrunk, connected by a bridge, to form a new picture (the point below is the indentation of the strongly connected component)
Find all points with an entry level of 0, because points with a degree of 0 are not dyed at any other point, so they can only buy at points with a degree of 0

#include <cstdio>#include <cstring>#define N 3010#define M 8010#define INF 0x3f3f3f3f#define min (a) (a) < (b)? (a): (b))structedge{int  from, to, next;} E[M];intN, Tot, p, M, Dfs_clock, scc_cnt, top;intHead[n], Cost[n], pre[n], sccno[n], stack[n], Lowlink[n],inch[N],value[N];voidAddedge (intUintV) {e[tot].to = v; E[tot]. from= u;    E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {memset (cost,0x3f,sizeof(cost)); scanf"%d", &p);intNo, Money; for(inti =0; I < P; i++) {scanf ("%d%d", &no, &money);    Cost[no] = money; } scanf ("%d", &m); Memset (Head,-1,sizeof(head)); tot =0;intU, v; for(inti =0; I < m; i++) {scanf ("%d%d", &u, &v);    Addedge (U, v); }}voidDfsintu) {pre[u] = lowlink[u] = ++dfs_clock; Stack[++top] = u;intV for(inti = Head[u]; I! =-1; i = e[i].next) {v = e[i].to;if(!pre[v])            {DFS (v);        Lowlink[u] = min (Lowlink[u], lowlink[v]); }Else if(!sccno[v])        {Lowlink[u] = min (Lowlink[u], pre[v]); }    }if(Lowlink[u] = = Pre[u]) {scc_cnt++; while(1) {v = stack[top--]; SCCNO[V] = scc_cnt;if(v = = u) Break; }    }}voidSolve () {memset (PRE,0,sizeof(pre)); memset (Sccno,0,sizeof(SCCNO)); Dfs_clock = scc_cnt = top =0; for(inti =1; I <= N; i++)if(!pre[i]) DFS (i); for(inti =1; I <= scc_cnt; i++) {value[I] = INF;inch[I] =1; } for(inti =1; I <= N; i++) {value[Sccno[i]] = min (value[Sccno[i]], cost[i]); }intU, V, MiniD; for(inti =0; i < tot; i++) {u = sccno[e[i]. from]; v = sccno[e[i].to];if(U! = V) {inch[V] =0; }    }BOOLFlag =false; for(inti =1; I <= N; i++) {u = sccno[i];if(inch[U] &&value[u] = = INF) {MiniD = i; Flag =true; Break; }    }if(flag) {printf ("no\n%d\n", MiniD);return; }intAns =0; for(inti =1; I <= scc_cnt; i++) {if(inch[i]) ans + =value[i]; } printf ("yes\n%d\n", ans);}intMain () { while(SCANF ("%d", &n)! = EOF) {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

FZU-1719 Spy Network (strong connectivity component)

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.