http://codeforces.com/contest/284/problem/E
思维很重要呀 原来是一个背包呀
代码:
#include<iostream> #include<cmath> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<stack> #include<queue> #include<map> #include<set> #include<algorithm> #define LL long long using namespace std; const int INF=0x3f3f3f3f; const int MOD=1000000007; const int N=305; const int M=100005; int a[N],next[N]; bool head[N]; int dp[M]; vector< vector<int> >h; void packComplete(int cost,int V) { for(int v=cost;v<=V;++v) dp[v]=(dp[v]+dp[v-cost])%MOD; } int main() { //freopen("data.in","r",stdin); int n,q,t; while(cin>>n>>q>>t) { h.clear(); memset(next,0,sizeof(next)); for(int i=1;i<=n;++i) cin>>a[i]; memset(head,true,sizeof(head)); while(q--) { int l,r; cin>>l>>r; head[r]=false; next[l]=r; } int line[N],m; for(int i=1;i<=n;++i) if(head[i]==true) { int l=i; m=0; line[m++]=l; while(next[l]!=0) { l=next[l]; line[m++]=l; } h.push_back(vector<int>(line,line+m)); } int num=0; for(int unsigned i=0;i<h.size();++i) num+=h[i].size(); if(num<n) {cout<<"0"<<endl;continue;} memset(dp,0,sizeof(dp)); int V=t; int k=0; dp[0]=1; for(unsigned int i=0;i<h.size();++i) { int tmp=0; for(unsigned int j=0;j<h[i].size();++j) { int l=h[i][j]; if(k<=V) k=k+(h[i].size()-1-j)*a[l]; if(tmp>V) continue; tmp+=a[l]; if(tmp<=V) packComplete(tmp,V); } } if(k>V) {cout<<"0"<<endl;continue;} cout<<dp[V-k]<<endl; } return 0; }