zoj 3524(拓扑排序+多重背包)(好题)

Posted qlky

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zoj 3524(拓扑排序+多重背包)(好题)相关的知识,希望对你有一定的参考价值。

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 namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f

struct node
{
    int v,len;
}cur;

vector<node> mmap[705];

int n,m,w,x;

int tw[705],tv[705],cnt[705];

int st[705],result[705],flag[705];

long long dp[705][2005],power[705][2005],ans,dist;

void ptopo()
{
    for(int i =1;i<=n;i++) pf("%d ",result[i]);
    blank;
}

void Debug_InPut() {

    for (int i = 1; i <= n; ++i)
        for (int j = 0; j <= w; ++j)
            pf("(%lld %lld)%c",dp[i][j],power[i][j],j==w?\n: );
}

void init()
{
    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(int i = 0; i <= n; ++i)
        mmap[i].clear();
}

void topo()
{
    int i,j;
    int tot = -1,index=0;
    for(i=1;i<=n;i++)
    {
        if(cnt[i]==0) st[++tot] = i;
        //pf("%d ",st[tot]);
    }
    //blank;
    while(tot>=0)
    {
        int xx = st[tot--];
        result[++index] = xx;
        int s = mmap[xx].size();
        //pf("tot%d\n",tot);
        for(j=0;j<s;j++)
        {
            cur = mmap[xx][j];
            int y = 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();
}

void solve()
{
    int i,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++)
    {
        int xx = result[i];
        if(flag[xx]==0) continue;
        int s = mmap[xx].size();
        for(j=0;j<s;j++)
        {
            cur = mmap[xx][j];
            int tp = 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;
                    }
                    else
                        power[tp][k] = min(power[xx][k] + cur.len*k ,power[tp][k]);
                }
            }

            for (k = tw[tp]; k <= w; ++k) {
                //完全背包
                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) {
                //更新答案
                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();
        }
    }
}


int main()
{
    int i,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++)
        {
            int x,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(拓扑排序+多重背包)(好题)的主要内容,如果未能解决你的问题,请参考以下文章

排序(拓扑好题)

ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083))

ZOJ - 3780-Paint the Grid Again-(拓扑排序)

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

python多重继承之拓扑排序

建图思维拓扑排序建图+序列自动机——swerc Gym - 102501G 好题