【拓撲】【寬搜】CSU 1084 有向非循環圖 (2016湖南省第十二屆大學生電腦程式設計競賽)

來源:互聯網
上載者:User

標籤:

題目連結:

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

題目大意:

  一個有向非循環圖(DAG),有N個點M條有向邊(N,M<=105),每個點有兩個值ai,bi(ai,bi<=109),count(i,j)表示從i走到j的方案數。

  求mod 109+7的值。

 

題目思路:

  【拓撲】【寬搜】

  首先將式子拆開,每個點I走到點J的d[j]一次就加上一次ai,這樣一個點被i走到的幾次就加上幾次ai,相當於count(i,j)*ai,最終只要求Σ(bj*d[j])即可。

  將這張有向非循環圖按照拓撲序排入佇列,這樣保證做到第i個點時在它之後不會再被更新答案(也就是說後面不會影響前面的值,這樣才能通過前面的值推出後面的)

  每個點I都把點I的後繼節點的d加上ai,最終按照上面那樣求Σ(bj*d[j])即可。

 

 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack>10 #include<queue>11 #include<set>12 #include<bitset>13 #include<memory.h>14 #include<time.h>15 #include<stdio.h>16 #include<stdlib.h>17 #include<string.h>18 //#include<stdbool.h>19 #include<math.h>20 #define min(a,b) ((a)<(b)?(a):(b))21 #define max(a,b) ((a)>(b)?(a):(b))22 #define abs(a) ((a)>0?(a):(-(a)))23 #define lowbit(a) (a&(-a))24 #define sqr(a) ((a)*(a))25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))26 #define mem(a,b) memset(a,b,sizeof(a))27 #define eps (1e-8)28 #define J 1000029 #define mod 100000000730 #define MAX 0x7f7f7f7f31 #define PI 3.1415926535897932332 #define N 10000433 using namespace std;34 typedef long long LL;35 int cas,cass;36 int n,m,lll,ans;37 LL aans;38 struct xxx39 {40     int next,to;41 }a[N];42 int last[N],q[N],in[N],aa[N],bb[N];43 LL d[N];44 void add(int x,int y)45 {46     a[++lll].to=y;47     a[lll].next=last[x];48     last[x]=lll;49 }50 void tuopu()51 {52     int i,l,r=0;53     for(i=1;i<=n;i++)if(!in[i])q[++r]=i;54     for(l=1;l<=r && r!=n;l++)55         for(i=last[q[l]];i;i=a[i].next)56             if(--in[a[i].to]==0)57                 q[++r]=a[i].to;58     for(l=1;l<=n;l++)59         for(i=last[q[l]];i;i=a[i].next)60             d[a[i].to]=(d[a[i].to]+d[q[l]]+aa[q[l]])%mod;61     for(i=1;i<=n;i++)62         aans=(aans+1LL*d[i]*bb[i])%mod;63 }64 int main()65 {66     #ifndef ONLINE_JUDGE67     freopen("1.txt","r",stdin);68 //    freopen("2.txt","w",stdout);69     #endif70     int i,j,k;71     int x,y,z;72 //    for(scanf("%d",&cass);cass;cass--)73 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)74 //    while(~scanf("%s",s+1))75     while(~scanf("%d",&n))76     {77         lll=aans=0;mem(last,0);mem(in,0);mem(d,0);78         scanf("%d",&m);79         for(i=1;i<=n;i++)scanf("%d%d",aa+i,bb+i);80         for(i=1;i<=m;i++)81         {82             scanf("%d%d",&x,&y);83             add(x,y);in[y]++;84         }85         tuopu();86         printf("%lld\n",aans);87     }88     return 0;89 }90 /*91 //92 93 //94 */
View Code

 

【拓撲】【寬搜】CSU 1084 有向非循環圖 (2016湖南省第十二屆大學生電腦程式設計競賽)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.