zoukankan      html  css  js  c++  java
  • HDOJ—3339 最短路+背包

    In Action

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2165    Accepted Submission(s): 713


    Problem Description

    Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the number of nuclear weapons have soared across the globe.
    Nowadays,the crazy boy in FZU named AekdyCoin possesses some nuclear weapons and wanna destroy our world. Fortunately, our mysterious spy-net has gotten his plan. Now, we need to stop it.
    But the arduous task is obviously not easy. First of all, we know that the operating system of the nuclear weapon consists of some connected electric stations, which forms a huge and complex electric network. Every electric station has its power value. To start the nuclear weapon, it must cost half of the electric network's power. So first of all, we need to make more than half of the power diasbled. Our tanks are ready for our action in the base(ID is 0), and we must drive them on the road. As for a electric station, we control them if and only if our tanks stop there. 1 unit distance costs 1 unit oil. And we have enough tanks to use.
    Now our commander wants to know the minimal oil cost in this action.
     
    Input
    The first line of the input contains a single integer T, specifying the number of testcase in the file.
    For each case, first line is the integer n(1<= n<= 100), m(1<= m<= 10000), specifying the number of the stations(the IDs are 1,2,3...n), and the number of the roads between the station(bi-direction).
    Then m lines follow, each line is interger st(0<= st<= n), ed(0<= ed<= n), dis(0<= dis<= 100), specifying the start point, end point, and the distance between.
    Then n lines follow, each line is a interger pow(1<= pow<= 100), specifying the electric station's power by ID order.
     
    Output
    The minimal oil cost in this action.
    If not exist print "impossible"(without quotes).
     
    Sample Input
     2
     2 3
     0 2 9
     2 1 3
     1 0 2
     1
     3
     2 1
     2 1 3
     1
     3
     
    Sample Output
    5 impossible
     
    题目大致题意:
    开着坦克去毁坏每个节点的发电站,发电站的能量已知,你从0节点出发,毁坏的能量达到总能量的一半以上即可。每个坦克只可以毁坏一个发电站。求需要走的最短距离是多少
    输入:
    第一行是组数
    第二行是节点数 和 边数
    下面 几行是 边  和 权
    最后 是 每个节点 发电站 能量
    输出
    最小距离和,也就是坦克需要消耗的能量
     
    解题思路:
    因为每个坦克只能毁坏一个节点,所以要用到 背包 动态 来 寻找这个最小 距离。首先,我们根据给出的边,来找出0点到各个定点的最短距离,然后用0/1背包来解决,这里,我们把最大距离当做容量 ,f 存储 能量值
     
     
    代码:
    View Code
    #include<stdio.h>
    #include<string.h>
    #define MAX 150
    #define INF 1<<20
    int map[MAX][MAX],n,dis[MAX];
    bool ben[MAX];
    
    void DJ(int k)
    {
        int u,i,j,min;
        for(i=0;i<=n;++i)
        {
            dis[i]=map[k][i];
            ben[i]=false;
        }
        ben[0]=true;
        for(i=1;i<=n;++i)
        {
            min=INF;
            u=0;
            for(j=0;j<=n;++j)
                if(!ben[j]&&min>dis[j])
                {
                    min=dis[j];
                    u=j;
                }
            ben[u]=true;
            for(j=0;j<=n;++j)
                if(!ben[j]&&dis[j]>min+map[u][j])
                    dis[j]=min+map[u][j];
        }
    }
    
    int main()
    {
        int T,k,power[MAX],i,j,w,sum,from,to,v,f[10005],flag;
        scanf("%d",&T);
        while(T--)
        {
    
            scanf("%d%d",&n,&k);
            for(i=0;i<=n;++i)
                for(j=0;j<=n;++j)
                    map[i][j]=i==j?0:INF;
            for(i=0;i<k;++i)
            {
                scanf("%d%d%d",&from,&to,&v);
                if(v<map[from][to])
                    map[from][to]=map[to][from]=v;
            }
            sum=0;
            for(i=1;i<=n;++i)
            {
                scanf("%d",&power[i]);
                sum+=power[i];
            }
            w=sum/2;
            DJ(0);
            sum=0;
            for(i=1;i<=n;++i)
                if(dis[i]!=INF)
                    sum+=dis[i];
            memset(f,0,sizeof(f));
            for(i=1;i<=n;++i)///////节点作为种类
                if(dis[i]!=INF)
                for(j=sum;j>=dis[i];--j)////////将距离当做容量
                    f[j]=f[j]>f[j-dis[i]]+power[i]?f[j]:f[j-dis[i]]+power[i];
            flag=0;
            for(i=0;i<=sum;++i)
                if(f[i]>w)
                {
                    flag=1;
                    break;
                }
            if(flag)
                printf("%d\n",i);
            else
                printf("impossible\n");
        }
        return 0;
    }
     
  • 相关阅读:
    前端页面弹框遮罩禁止页面滚动
    微信浏览器兼容问题
    判断是否为空对象的方法
    reflow和repaint理解总结
    45道CSS基础面试题(附答案)
    iframe跨域修改子页面内容或样式
    日期中前一天和后一天
    js事件绑定方法
    C#常用的数据格式转换
    C#代码和javascript函数相互调用
  • 原文地址:https://www.cnblogs.com/zibuyu/p/2652174.html
Copyright © 2011-2022 走看看