http://blog.csdn.net/woshi250hua/article/details/7824773
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 1010#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3fstructnode{intV,len;} Cur;vector<node> mmap[705];intn,m,w,x;inttw[705],tv[705],cnt[705];intst[705],result[705],flag[705];Long Longdp[705][2005],power[705][2005],ans,dist;voidPtopo () { for(inti =1; i<=n;i++) PF ("%d", Result[i]); Blank;}voidDebug_input () { for(inti =1; I <= N; ++i) for(intj =0; J <= W; ++j) PF ("(%lld%lld)%c", Dp[i][j],power[i][j],j==w?'\ n':' ');}voidinit () {ans=0; Dist=0; Memset (DP,0,sizeof(DP)); memset (CNT,0,sizeof(CNT)); memset (Flag,0,sizeof(flag)); memset (Power,-1,sizeof(Power)); for(inti =0; I <= N; ++i) Mmap[i].clear ();}voidtopo () {inti,j; intTot =-1, index=0; for(i=1; i<=n;i++) { if(cnt[i]==0) St[++tot] =i; //PF ("%d", St[tot]); } //Blank; while(tot>=0) { intxx = st[tot--]; result[++index] =xx; ints =mmap[xx].size (); //PF ("tot%d\n", tot); for(j=0; j<s;j++) {cur=Mmap[xx][j]; inty =CUR.V; //PF ("xx%d y%d", xx,y);cnt[y]--; if(cnt[y]==0) {st[++tot] =y; //PF ("tot%d\n", tot); //PF ("y%d", y); } } } //Ptopo ();}voidsolve () {inti,j,k; for(i=0; i<=w;i++) {Power[x][i]=0; if(i>=Tw[x]) {Dp[x][i]= Max (dp[x][i],dp[x][i-tw[x]]+tv[x]); } if(Dp[x][i] >ans) ans= Dp[x][i],dist =0; } Flag[x]=1; for(i=1; i<=n;i++) { intxx =Result[i]; if(flag[xx]==0)Continue; ints =mmap[xx].size (); for(j=0; j<s;j++) {cur=Mmap[xx][j]; intTP =CUR.V; FLAG[TP]=1; for(k=0; k<=w;k++) { if(Dp[tp][k] <Dp[xx][k]) {Dp[tp][k]=Dp[xx][k]; POWER[TP][K]= Power[xx][k] + cur.len*K; } Else if(Dp[tp][k] = =Dp[xx][k]) { if(Power[tp][k] = =-1) {Power[tp][k]= Power[xx][k]+cur.len *K; } ElsePower[tp][k]= Min (Power[xx][k] + cur.len*K, power[tp][k]); } } for(k = tw[tp]; k <= W; + +k) {//Full Backpack if(Dp[tp][k] < dp[tp][k-tw[tp]]+TV[TP]) {Dp[tp][k]= DP[TP][K-TW[TP]] +TV[TP]; POWER[TP][K]= power[tp][k-TW[TP]]; } Else if(Dp[tp][k] = = dp[tp][k-tw[tp]]+TV[TP]) power[tp][k]= Min (power[tp][k],power[tp][k-TW[TP]]); } for(k =0; K <= W; ++k) {//Update Answer if(Dp[tp][k] >ans) ans= Dp[tp][k],dist =Power[tp][k]; Else if(Dp[tp][k] = =ans) Dist=min (dist,power[tp][k]); } //pf ("cur%d:\n", cur.v), Debug_input (); } }}intMain () {inti,j; while(~SF ("%d%d%d%d",&n,&m,&w,&x)) {init (); for(i=1; i<=n;i++) SF ("%d%d",&tw[i],&Tv[i]); for(i=0; i<m;i++) { intx,y,l; SF ("%d%d%d",&x,&y,&l); CUR.V=y; Cur.len=l; Cnt[y]++; MMAP[X].PB (cur); } topo (); Solve (); //pf ("ans%lld%lld\n", ans,dist);printf"%lld\n", Dist); } return 0;}
Zoj 3524 (topological sorting + multiple backpacks) (good question)