zoukankan      html  css  js  c++  java
  • poj 1062 昂贵的聘礼 夜

    http://poj.org/problem?id=1062

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<math.h>
    #include<string.h>

    using namespace std;
    struct next
    {
        int instead;
        int j;
        struct next *next;
    };
    struct head
    {
        struct next *next;
        int need;
        int L;
    }mem[101];
     void build(int i,int j,int instead)
    {
        struct next *f=new next;
        f->j=j;
        f->instead=instead;
        f->next=mem[i].next;
        mem[i].next=f;
    }
    int m,ans;
    //bool hadinstead[101];
    void find(int i,int need,int big,int small)
    {
        //cout<<i<<endl;
        if(i==1)
        {
            if(need<ans)
            ans=need;
            return ;
        }
        struct next *f;
        f=mem[i].next;
        while(f!=NULL)
        {
                int I=max(big,mem[f->j].L);
                int J=min(small,mem[f->j].L);
                if(I-J<=m&&need+f->instead<mem[f->j].need)
                {
                    //cout<<(f->j)<<I<<" "<<J<<endl;
                    find(f->j,need+f->instead,I,J);
                }
                f=f->next;
        }
    }
    void free(int n)
    {
        struct next *f;
        for(int i=1;i<=n;i++)
        {
            while(mem[i].next!=NULL)
            {
                f=mem[i].next;
                mem[i].next=mem[i].next->next;
                delete(f);
            }
        }
    }
    int main()
    {
        //freopen("date.txt" ,"r",stdin);
        int n,k;
        int i,j,instead;
        scanf("%d%d",&m,&n);
            //memset(hadinstead,true,sizeof(hadinstead));
            for(i=1;i<=n;i++)
            {
                scanf("%d%d%d",&mem[i].need,&mem[i].L,&k);
                //if(k==0)
                //hadinstead[i]=false;
                while(k--)
                {
                    scanf("%d%d",&j,&instead);
                    build(j,i,instead);
                }
            }
            ans=mem[1].need;
            for(i=2;i<=n;i++)
            {

                //if(!hadinstead[i])
                find(i,mem[i].need,max(mem[i].L,mem[1].L),min(mem[i].L,mem[1].L));
            }
            printf("%d\n",ans);
            free(n);
        return 0;
    }

  • 相关阅读:
    2019ICPC徐州 H.Yuuki and a problem
    wprintf 输出中文
    bit数组
    Vs2010 Atl工程手工添加连接点
    dll非模态窗口不响应按钮消息
    VC中给控件添加ToolTip
    在Dialog中添加工具条
    在Dialog中添加状态栏
    Vc添加快捷键
    在VC中调用COM组件的方法
  • 原文地址:https://www.cnblogs.com/liulangye/p/2415344.html
Copyright © 2011-2022 走看看