标签:排序 memset 数列 == 方便 ack put time 表示
Time Limit: 5 Sec Memory Limit: 128 Mb Submitted: 716 Solved: 298
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
4 4 250000014
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #include<vector> #include<queue> using namespace std; #define LL long long const int maxn = 1e5+7; const int mod = 1e9+7; vector<int>e[maxn]; int a[maxn],b[maxn],d[maxn]; int ans[maxn]; int main(){ int n,m; while(cin >> n >> m){ for(int i=1;i<=n;i++){ e[i].clear(); } memset(d,0,sizeof(d)); memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++){ cin >> a[i] >> b[i]; } for(int i=1;i<=m;i++){ int u,v; cin >> u >> v; e[v].push_back(u); d[u]++;//终点为u的路径的条数 } queue<int> q; for(int i=1;i<=n;i++){ if(d[i] == 0){//把终点为i的路径数为0的点加入队列 q.push(i); } } while(!q.empty()){ int v = q.front(); q.pop(); for(int i=0;i<e[v].size();i++){ int u = e[v][i]; ans[u] = (ans[u] + (ans[v] + b[v])%mod)%mod; //之所以是加b[v],是因为乘是相当于整体而言,一条就是1*b[v]相当于加b[v] d[u]--; if(d[u] == 0){ q.push(u); } } } LL res = 0; for(int i=1;i<=n;i++){ res = (res + 1LL*ans[i]*a[i]%mod)%mod; } cout << res << endl; } return 0; }
标签:排序 memset 数列 == 方便 ack put time 表示
原文地址:http://www.cnblogs.com/l609929321/p/7265267.html