现在的位置: 首页 > 综合 > 正文

hdu3339In Action(经典,最短路+01背包)

2018年02月22日 ⁄ 综合 ⁄ 共 2981字 ⁄ 字号 评论关闭
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,和n个发电站,每个发电站有能量P[i],现在基地想要使所有的发电站不能正常工作,那么得用坦克占领所有发电站的能量总和的一半多,那么要占领哪几个发电站就能拥有总能量的一半多,从而控制所有的发电站,占领K个发电站需K辆坦克.每辆坦克从基地出发,如果能实现就输出每辆坦克所花费的总和.
分析:
一:解决每个站到其地的最小距离. 二:从n个发电站中找出几个站的能量和超出总能量和的一半并且这几个站到基地的距离和最小.
第一个问题很好解决,要解决每二个问题,就可以用01背包了,把n个站到基地的总距离和看成背包的最大容量V,能量看成是价值,也就是求花最少的费用能得到的最大价值.注意了,每个包是恰好被装满.
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
#define INF 999999999
struct nnn
{
    int d,w;
}node[105];
int map[105][105],n;
void init()//初始化
{
    for(int i=0;i<=n;i++)
    {
        node[i].d=INF;
        for(int j=0;j<=n;j++)
        map[i][j]=INF;
    }
}
void spfa(int s)//计算最短路
{
    int inq[105]={0};
    queue<int>q;
    node[s].d=0;
    q.push(s);
    while(!q.empty())
    {
        s=q.front(); q.pop();
        inq[s]=0;
        for(int i=1;i<=n;i++)
        if(node[i].d>node[s].d+map[s][i])
        {
            node[i].d=node[s].d+map[s][i];
            if(!inq[i])
            inq[i]=1,q.push(i);
        }
    }
}
int main()
{
    int m,dp[100005],V,t,a,b,p,sum;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        init(); V=0; sum=0;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&p);
            if(map[a][b]>p)
            map[a][b]=map[b][a]=p;
        }
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&node[i].w); sum+=node[i].w;
        }
        sum/=2;//总能量的一半
        spfa(0);
        for(int i=1;i<=n;i++)
        if(node[i].d!=INF)
          V+=node[i].d;//算出最大容量
        for(int i=0;i<=V;i++) dp[i]=0;//背包初始化
        //-----经典的01背包计算-----
        for(int i=1;i<=n;i++)
        if(node[i].d!=INF)//有路可通时
        for(int v=V;v>=node[i].d;v--)
        if(dp[v-node[i].d]||v==node[i].d)//能恰好的必要条件(--重要细节--)
        if(dp[v]<dp[v-node[i].d]+node[i].w)
            dp[v]=dp[v-node[i].d]+node[i].w;
        int need=0;
        for(int v=1;v<=V;v++)
        if(dp[v]>sum)//当费用v得到的价值刚好能满足大于一半时,那么v就是最小费用
        {
            need=v; break;
        }
        if(need) printf("%d\n",need);
        else printf("impossible\n");
    }
}

抱歉!评论已关闭.