"Topology" "Wide Search" CSU 1084 Direction-free graph (2016 Hunan province 12th session of college students computer Program design Competition)

Source: Internet
Author: User
Tags cas

Topic Links:

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1804

Main topic:

A directed acyclic graph (DAG) that has N points M bars have a forward edge (n,m<=105), each point has two values Ai,bi (ai,bi<=109), and Count (I,j) represents the number of scenarios that go from I to J.

Find the value of mod 109+7.

Topic Ideas:

"Topology" "Wide Search"

First, the equation is opened, each point I go to the J D[j] Once added AI, such a point by I to several times to add a few AI, the equivalent of Count (i,j) *ai, ultimately only required σ (Bj*d[j]) can.

Add this forward-free graph to the queue according to the topological order, so as to ensure that the second point is not updated after the answer (that is, it will not affect the previous value, so that the previous value can be pushed back)

Each point I will be the point I of the successor node D plus AI, and finally in accordance with the above to seek σ (Bj*d[j]) can be.

1 //2 //by Coolxxx3 //#include <bits/stdc++.h>4#include <iostream>5#include <algorithm>6#include <string>7#include <iomanip>8#include <map>9#include <stack>Ten#include <queue> One#include <Set> A#include <bitset> -#include <memory.h> -#include <time.h> the#include <stdio.h> -#include <stdlib.h> -#include <string.h> - //#include <stdbool.h> +#include <math.h> - #defineMin (a) < (b) ( A):(B)) + #defineMax (a) (a) > (b)? ( A):(B)) A #defineABS (a) ((a) >0? ( A):(-(a))) at #defineLowbit (a) (a& (a)) - #defineSqr (a) ((a) * (a)) - #defineSwap (a) (a) ^= (b), (b) ^= (a), (a) ^= (b)) - #defineMem (A, B) memset (A,b,sizeof (a)) - #defineEPS (1E-8) - #defineJ 10000 in #defineMoD 1000000007 - #defineMAX 0x7f7f7f7f to #definePI 3.14159265358979323 + #defineN 100004 - using namespacestd; thetypedefLong LongLL; * intCas,cass; $ intN,m,lll,ans;Panax Notoginseng LL Aans; - structXXX the { +     intnext,to; A }a[n]; the intLast[n],q[n],inch[n],aa[n],bb[n]; + LL D[n]; - voidAddintXinty) $ { $a[++lll].to=y; -a[lll].next=Last[x]; -last[x]=lll; the } - voidTuopu ()Wuyi { the     intI,l,r=0; -      for(i=1; i<=n;i++)if(!inch[i]) q[++r]=i; Wu      for(l=1; L<=r && r!=n;l++) -          for(i=last[q[l]];i;i=a[i].next) About             if(--inch[a[i].to]==0) $q[++r]=a[i].to; -      for(l=1; l<=n;l++) -          for(i=last[q[l]];i;i=a[i].next) -D[a[i].to]= (D[a[i].to]+d[q[l]]+aa[q[l])%MoD; A      for(i=1; i<=n;i++) +Aans= (Aans+1ll*d[i]*bb[i])%MoD; the } - intMain () $ { the #ifndef Online_judge theFreopen ("1.txt","R", stdin); the //freopen ("2.txt", "w", stdout); the     #endif -     inti,j,k; in     intx, y, z the //for (scanf ("%d", &cass); cass;cass--) the //for (scanf ("%d", &cas), cass=1;cass<=cas;cass++) About //while (~scanf ("%s", s+1)) the      while(~SCANF ("%d",&N)) the     { thelll=aans=0; Mem (Last,0); Mem (inch,0); Mem (d,0); +scanf"%d",&m); -          for(i=1; i<=n;i++) scanf ("%d%d", aa+i,bb+i); the          for(i=1; i<=m;i++)Bayi         { thescanf"%d%d",&x,&y); theAdd (x, y);inch[y]++; -         } - Tuopu (); theprintf"%lld\n", Aans); the     } the     return 0; the } - /* the // the  the //94 */
View Code

Topology "wide search" CSU 1084 with Direction-free graph (2016 Hunan province 12th session of university Students computer Program design contest)

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.