如果两个点之间有多条边,那就相当于一个点连多个相同的点然后dfs跑一下累加和就行了记得记忆化搜索剪枝
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define mod 1000000007ll
#define maxn 100005
#define LL long long
using namespace std;
LL dp[maxn], ans;
LL a[maxn], b[maxn];
LL last[maxn * 2], Next[maxn * 2], edge[maxn * 2], l;
LL n, m;
void add(int a, int b)
{
edge[l] = b;
Next[l] = last[a];
last[a] = l;
l++;
}
void dfs(int pre)
{
if(dp[pre] != -1) return ;
dp[pre] = 0;
for(int i = last[pre]; i != -1; i = Next[i])
{
dfs(edge[i]);
dp[pre] += dp[edge[i]] + b[edge[i]];
dp[pre] %= mod;
}
ans += dp[pre] * a[pre];
ans %= mod;
}
int main()
{
while(scanf("%lld%lld", &n, &m)!=EOF)
{
memset(dp, -1, sizeof(dp));
memset(last, -1, sizeof(last));
for(int i = 1; i <= n; i++) scanf("%lld%lld", &a[i], &b[i]);
ans = 0;
l = 0;
while(m--)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for(int i = 1; i <=n ; i++) dfs(i);
printf("%lld\n", ans);
}
return 0;
}