hdu3339In Action(最短路+01背包)

本文探讨了一种解决核武器破坏任务的最短距离算法,通过使用Floyd算法计算各发电站间的最短路径,并结合0/1背包问题解决策略,以最小化行动所需油料成本。实例分析了特定场景下的应用,旨在阻止潜在的核威胁。

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背包来解决,这里,我们把最大距离当做容量 ,dp 存储 能量值.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#define N 1000001
using namespace std;
int n,m;
int map[110][110];
int v[110];
int dp[1000100];
void Floy()//计算出0到各个发电站的最短距离,因为最多100个点,所以用floy
{
    for(int k=0;k<=n;k++)
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                if(map[i][j]>map[i][k]+map[k][j])
                {
                    map[i][j]=map[i][k]+map[k][j];
                }
            }
        }
    }
}
int main()
{
    int T,zz,xx,yy,flag;
    scanf("%d",&T);
    while(T--)
    {
        flag=0;
        scanf("%d%d",&n,&m);
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                map[i][j]=N;
                map[j][i]=N;
            }
            map[i][i]=0;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&xx,&yy,&zz);
            if(map[xx][yy]>zz)//有重边
            {
                map[xx][yy]=zz;
                map[yy][xx]=zz;
            }
        }
        Floy();
        int sum=0,sum1=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
            sum1+=v[i];//计算出发电场的总能量。
            if(map[0][i]!=N)//计算出所有可行点的距离。
            sum+=map[0][i];
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)//以总距离为背包容量,发电站的能量为利润。
        {
            for(int j=sum;j>=map[0][i];j--)
            {
                if(dp[j-map[0][i]]+v[i]>dp[j])
                {
                    dp[j]=dp[j-map[0][i]]+v[i];
                }
            }
        }
        for(int i=1;i<=sum;i++)
        {
            if(dp[i]>sum1/2.0)//必须炸掉一半多才可以阻止战争
            {
                flag=1;
                printf("%d\n",i);
                break;
            }
        }
        if(flag==0) printf("impossible\n");


    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值