HDU 3339 In Action (dijkskra+01背包)

8 篇文章 1 订阅
2 篇文章 0 订阅

In Action

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


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
HDOJ Monthly Contest – 2010.03.06

题意:这个题意比赛过程一直没看懂。。(没有看出一个坦克只能占领一个地方),看懂很简单
       就是要用很多坦克去占领电网,所有坦克都在0位置,有n个电站编号为1~n,每个电站有它自己的能量值,坦克当且仅当停在上面才能破坏电站(要是再移动了那就不算占
领),现在要破坏其中一些电站,要让电网的总能量值损失一半以上,且要使所有坦克去目的地耗费的油量最少。

解题思路:先用Dijkstra算法求出0点到其它任何位置的最短距离,接下来就是0-1背包问题了,把每条路当作花费,目的地能量值当作价值。注意的是需要破坏一半以上的能量值。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int dp[10010];
int n,m,v[110],map[110][110],d[110],a[110];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0x3f3f,sizeof(map));
        scanf("%d %d",&n,&m);
        int s,e,load;
        for(int i=0;i<m;i++){
            scanf("%d %d %d",&s,&e,&load);
            map[s][e]=map[e][s]=min(map[s][e],load);
        }
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        memset(v,0,sizeof(v));
        for(int i=0;i<=n;i++) d[i]=(i==0 ? 0 : INF);
        for(int i=0;i<=n;i++){ //Dijkstra模板求固定点到任意点的最短路
            int x,ans=INF;
            for(int y=0;y<=n;y++) if(!v[y] && d[y]<=ans) ans=d[x=y];
            //cout << 1 << endl;
            v[x]=1;
            for(int y=0;y<=n;y++) d[y]=min(d[y],d[x]+map[x][y]);
            //cout << 2 << endl;
        }
        int load_sum=0,sum=0;
        for(int i=1;i<=n;i++)
            sum+=a[i];
        sum=sum/2+1; // 能量要超过一半以上,所以要加一
        for(int i=1;i<=n;i++)
            if(d[i]!=INF)
                load_sum+=d[i];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++) // 进行0-1背包
        {
            if(d[i]==INF) continue; // 如果路不存在,跳过
            for(int j=load_sum;j>=d[i];j--){
                 dp[j]=max(dp[j],dp[j-d[i]]+a[i]);
            }
        }
        int res=-1;
        for(int i=0;i<=load_sum;i++)
          if(dp[i]>=sum){ // 找到第一个满足条件的,就是需要最少的花费(也就是耗油量)
             res=i;
             break;
          }
        if(res==-1) printf("impossible\n");
        else printf("%d\n",res);
    }
    return 0;
}</span>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值