有向无环图- CSU1804(拓扑排序)

来源:互联网 发布:库卡机器人编程手册 编辑:程序博客网 时间:2024/05/22 03:02

题目链接

题意:

 给出序列a,b。并给出每个有向边的起点和终点,count(i,j)表示i 到j的不同路径的数量,要求求出下面这个式子的值

http://acm.csu.edu.cn/OnlineJudge/upload/201608/x1804.PNG.pagespeed.ic.6kRN4cVcL5.png

思路:用vector存起来有边指向每个点的点,d[i]表示从i出去的有向边的数量。

           把∑count(i, j)* b[j]算出来,从后往前累加,最后乘上a[i]求和即可。

代码:

// He  renders  landscapes  with  great  skill  and  artistry.#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <iterator>#include <cctype>#include <sstream>#include <string>#include <vector>#include <set>#include <map>#include <stack>#include <deque>#include <queue>#include <list>#include <functional>#include <ctime>#include <bitset>//#pragma comment(linker, "/STACK:102400000, 102400000")#define debug     puts("+******************************************************+")#define Min(a, b) ( (a < b) ? a : b )#define Max(a, b) ( (a > b) ? a : b )#define lc         o<<1#define rc         o<<1|1#define lson       L, M, lc#define rson       M + 1, R, rc#define mem0(x)   memset(x, 0, sizeof x)#define mem1(x)   memset(x, -1, sizeof x)#define memf(x)   memset(x, false, sizeof x)#define pb        push_back#define pf        push_front#define LB        lower_bound#define UB        upper_bound#define PQ        priority_queue#define fr(x)     freopen("x", "r", stdin )#define fw(x)     freopen("x", "w" , stdout)#define all(a)    a.begin(), a,end()#define X         first#define Y         second#define MP        make_pair#define Abs(x)    ( x >= 0 ) ? x : ( -x )#define MAXS      50000 + 8#define MAXT      10000 + 8#define MAXL      500000 + 8#define INF       0x3f3f3f3f#define INFL      1000000000000000000#define inf       -(1<<30)#define EPS       1e-10#define PI        acos(-1.0)#define sqr(x)    (x * x)using namespace std;typedef long long          LL;typedef unsigned long long uLL;typedef double             DB;typedef long double        LD;typedef pair<int, int >   pii;typedef pair<LL, LL>       pll;const int MOD   = 1e9 + 7;const int N     = 1e5 + 8;const int maxn  = 1e3 + 8;const int dx[]  = { -1, 1,  0, 0 };const int dy[]  = {  0, 0, -1, 1 };struct Node{    LL x;    int id;    char ch;    bool operator < ( const Node & n) const    {        return x < n.x;    }} no[N];int n, m;int a[N], b[N];//void pushUp(int o)//{//    seg[o] = Min( seg[lc],  seg[rc] );//}//void build(int L, int R, int o)//{//    if ( L == R ) {//       seg[o] = pre[L]; return;//    }//    int M = (L  + R) >> 1;//    build( lson );//    build( rson );//    pushUp(o);//}//int query(int l, int r, int L, int R, int o)//{//    if (  l <= L && r >= R  ) return seg[o];//    int  M = (L + R) >> 1;//    int ans = INF;//    if ( l <= M  ) ans = Min(ans, query( l, r, lson));//    if ( r > M  ) ans = Min(ans, query( l, r, rson));//    return ans;//}//void RMQ_init()//{//    for (int i = 1;i <= n; i++)//        d[i][0] = pre[i];//    for (int j = 1;  (1 << j) <= n; j++) {//          for ( int i = 1; i  + (1 << j) - 1 <= n; i++) {//               d[i][j] = Min( d[i][j-1], d[i + (1 << (j -1) ) ][j - 1] );//          }//    }//}////int RMQ(int L, int R)//{//    int k = 0;//    while ( (1 <<(k+ 1) ) <= R - L +  1 ) k++;//    return Min( d[L][k],  d[R -  (1 << k)  + 1 ][k] );//}vector<int> G[N];int d[N];LL ans[N];int main(){    //freopen("codecoder.in", "r", stdin);    //freopen("out.txt", "w", stdout);   // fw(out.txt );     //ios::sync_with_stdio(true);     while ( ~scanf("%d%d", &n, &m)) {            for (int i = 0 ;i <= n; i++) G[i].clear();           for (int i = 1;i <= n; i++) {                scanf("%d%d", &a[i], &b[i]);           }           mem0(d);           mem0(ans);           int u, v;           for (int i = 1; i <= m; i++) {                scanf("%d%d", &u, &v);                G[v].pb(u);                d[u]++;           }           queue<int> q;           for (int i = 1; i <= n; i++) {                if ( d[i] == 0 ) q.push( i );           }           while ( !q.empty() )           {               int v = q.front(); q.pop();               for (int i = 0; i < G[v].size(); i++) {                    int u = G[v][i];                    ans[u] = (ans[u] + ( ans[v]  +  b[v] ) % MOD) % MOD;                    d[u]--;                    if ( !d[u] ) q.push( u );               }           }           LL res = 0;           for (int i = 1; i <= n; i++) {               res = (res + (LL)ans[i] * a[i] % MOD ) %MOD;           }           printf("%lld\n",  res);     }    return 0;}


0 0
原创粉丝点击