1002
拓扑加dp, dpi表示能到达i点的所有ai 的和,然后乘一遍bi就可以了。 因为拓扑的时候多条路径就是加了几次
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
int a[maxn] ,b[maxn];
int n, m ;
vector<int>vec[maxn];
int d[maxn];
queue<int>q;
ll dp[maxn];int pre[maxn];
int main(){
while(scanf("%d%d" ,&n ,&m) != EOF){
for(int i = 1; i <= n ;i ++){
scanf("%d%d" ,&a[i], &b[i]); vec[i].clear(); d[i] = 0; pre[i] = 0;
dp[i] = 0 ;
}
int u ,v ;
for(int i = 1; i <= m ; i++){
scanf("%d%d" ,&u, &v);
vec[u].push_back(v); d[v]++;
}
while(!q.empty()) q.pop();
for(int i = 1;i <= n ; i++){
pre[i] = d[i];
if(d[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int now = q.front(); q.pop();
for(int i = 0 ; i < vec[now].size() ;i ++){
int v = vec[now][i];
dp[v] = (dp[v] + dp[now] + a[now]) % mod;
d[v] -- ; if(d[v] == 0) q.push(v);
}
}
ll ans = 0 ;
for(int i = 1; i <= n ;i ++){
ans = (ans + (dp[i] * b[i]) % mod) % mod;
}
printf("%lld\n", ans);
}
}
/*
3 3
1 1
1 1
1 1
1 2
1 3
2 3
2 2
1 0
0 2
1 2
1 2
2 1
500000000 0
0 500000000
1 2
*/