zoukankan      html  css  js  c++  java
  • HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门:

    http://acm.hdu.edu.cn/showproblem.php?pid=3339

    In Action

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


    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
     
    Author
    Lost@HDU
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1217 1385 1548 3338 2112 
     
    分析:
    题目意思:
    n个发电站,m条路,每条路有各自的距离,每个发电站有各自的发电量,现在需要炸毁它们,一辆坦克只能炸毁一个发电站,而且需要炸毁的发电厂的发电量需要大于所有发电站所产生的总电量的一半,求坦克走的最短距离。
     
    做法:
    根据这个图,找到0到其他点的最短路(迪杰斯特拉)
    然后每个发电厂的功率看成物品价值
    0到每个点的距离看成物品的花费
    背包容量是花费的总和
    然后就是01背包
    注意当dis==INF的时候,表示不连通,物品花费不要加入到背包容量中(调试了很久)
    md
    code:
    #include <iostream>
    #include <cstdio>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    #include<math.h>
    #include<memory>
    #include<queue>
    #include<vector>
    using namespace std;
    typedef long long LL;
    #define max_v 10005
    #define INF 99999999
    
    int dp[max_v];
    int v[max_v];
    int cost[max_v];
    
    int e[max_v][max_v];
    int n,m;
    int used[max_v];
    int dis[max_v];
    void init()
    {
        memset(used,0,sizeof(used));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                e[i][j]=INF;
            }
            dis[i]=INF;
        }
    }
    void Dijkstra(int s)
    {
        for(int i=1; i<=n; i++)
        {
            dis[i]=e[s][i];
        }
        dis[s]=0;
        for(int i=1; i<=n; i++)
        {
            int index,mindis=INF;
            for(int j=1; j<=n; j++)
            {
                if(used[j]==0&&dis[j]<mindis)
                {
                    mindis=dis[j];
                    index=j;
                }
            }
            used[index]=1;
            for(int j=1; j<=n; j++)
            {
                if(dis[index]+e[index][j]<dis[j])
                    dis[j]=dis[index]+e[index][j];
            }
        }
    }
    void ZeroOnePack_improve(int n,int c)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            for(int j=c; j>=cost[i]; j--)
            {
                    dp[j]=max(dp[j],dp[j-cost[i]]+v[i]);
    
            }
        }
    }
    int main()
    {
        int t;
        int a,b,c;
        int sum;
        int sumv;
        int flag;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d",&n,&m);
            n++;
            init();
            for(int i=0; i<m; i++)
            {
                scanf("%d %d %d",&a,&b,&c);
                a++;
                b++;
                if(e[a][b]>c)
                    e[a][b]=e[b][a]=c;
            }
            sumv=0;
            for(int i=1; i<=n-1; i++)
            {
                scanf("%d",&v[i]);//价值
                sumv+=v[i];
            }
    
            Dijkstra(1);
            int k=1;
            sum=0;
            for(int i=2; i<=n; i++)
            {
                cost[k++]=dis[i];//花费
                if(dis[i]!=INF)//wa点
                   sum+=dis[i];
            }
            ZeroOnePack_improve(k-1,sum);
            flag=0;
            for(int i=0; i<=sum; i++)
            {
                if(dp[i]>=sumv/2+1)
                {
                    flag=1;
                    printf("%d
    ",i);
                    break;
                }
            }
            if(flag==0)
                printf("impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    ORB随便记一记
    POJ 树的直径和重心
    LeetCode 834. Sum of Distances in Tree
    LeetCode 214. Shortest Palindrome
    DWA局部路径规划算法论文阅读:The Dynamic Window Approach to Collision Avoidance。
    坐标变换
    论文阅读:hector_slam: A Flexible and Scalable SLAM System with Full 3D Motion Estimation.
    POJ 尺取法
    POJ 博弈论
    oracle锁表
  • 原文地址:https://www.cnblogs.com/yinbiao/p/9439216.html
Copyright © 2011-2022 走看看