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.

InputThe 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.OutputThe 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点出发,总共有编号1-n n个能量据点,如果要摧毁敌方,必须要占领能量据点的能量值达到总能量的一半以上,现在知道m条路径,以及坦克在m条路上的油耗,然后知道每个能量据点的能量值,问摧毁敌方所需的最少油耗.

题解:最短路+01背包,将每个能量据点看成背包容量,油耗看成价值,然后进行01背包求解

AC代码为:

#include<bits/stdc++.h> using namespace std; const int N = 105; const int INF = 99999999; int graph[N][N]; int low[N]; bool vis[N]; int w[N];  int dp[N*N];  int n,m; int dijkstra(int s) {     for(int i=1;i<=n;i++)     {         low[i] = graph[s][i];         vis[i] = false;     }     low[s] = 0;     vis[s] = true;     for(int i=1;i<n;i++)     {         int Min = INF;         for(int j=1;j<=n;j++)         {             if(Min>low[j]&&!vis[j])             {                 Min = low[j];                 s = j;             }         }         vis[s] = true;         for(int j=1;j<=n;j++)         {             if(low[j]>low[s]+graph[s][j]&&!vis[j])             {                 low[j] = low[s]+graph[s][j];             }         }     } } int main() {     int t;     scanf("%d",&t);     while(t--)     {         scanf("%d%d",&n,&m);         for(int i=0;i<=n;i++)         {             for(int j=0;j<=n;j++)             {                 if(i==j) graph[i][j] = 0;                 else graph[i][j] = INF;             }         }         for(int i=0;i<m;i++)         {             int a,b,c;             scanf("%d%d%d",&a,&b,&c);             if(c<graph[a][b])             graph[a][b]=graph[b][a] =c;         }         int sum = 0;         for(int i=1;i<=n;i++)         {             scanf("%d",&w[i]);             sum+=w[i];         }         dijkstra(0);         for(int i=1;i<=sum;i++) dp[i] = INF;         dp[0] = 0;         for(int i=1;i<=n;i++)         {             for(int v = sum;v>=w[i];v--)                 dp[v] = min(dp[v],dp[v-w[i]]+low[i]);         }         int sum1 = sum/2+1;         int Min = INF;         for(int i=sum1;i<=sum;i++) if(dp[i]<Min) Min = dp[i];         if(Min==INF) printf("impossible\n");         else printf("%d\n",Min);     } }

HDU-3339 IN ACTION(Dijkstra +01背包)的更多相关文章

  1. hdu 3339 In Action (最短路径+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  2. hdu3339 In Action(Dijkstra+01背包)

    /* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...

  3. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

  4. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  5. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  6. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  7. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  8. HDU 1864 最大报销额 0-1背包

    HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...

  9. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  10. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

随机推荐

  1. 数据仓库ETL案例学习(一)

    来自课程案例学习   某跨国食品超市的信息管理系统,每天都会记录成千上万条各地连锁超市的销售数据.基于大数据的背景,该公司的管理层决定建立FoodMart数据仓库,期望能从庞大的数据中挖掘出有商业价值 ...

  2. django 之创建自己的模板(使用案例)

    Django 创建自己的模板篇(实例) 此处需要创建模板,主要是对自己的模板进行扩展: 一般是扩展模板的tag和filter两个功能.可以用来创建你自己的tag和filter功能库. 创建模板库 分为 ...

  3. 花一天时间试玩vsphere6.7(EXSI)服务器版的vmware

    花一天时间试玩vsphere6.7(EXSI)服务器版的vmware 要注册账号(2019年11月14注册): 登陆网址:https://my.vmware.com/cn/group/vmware/h ...

  4. Oracle 数据库基础:数据查询与操作

    SELECT uname FROM TUser WHERE uname=‘admin’ SELECT 字段名列表 FROM 表名 WHERE 条件; 在Oracle数据库中,对象是属于模式的,每个账户 ...

  5. 关于Prometheus监控的思考:多标签埋点及Mbean

    使用 grafana+prometheus+jmx 作为普通的监控手段,是比较有用的.我之前的文章介绍了相应的实现办法. 但是,按照之前的实现,我们更多的只能是监控 单值型的数据,如请求量,tps 等 ...

  6. 一文看懂 K8s 日志系统设计和实践

    上一篇中我们介绍了为什么需要一个日志系统.为什么云原生下的日志系统如此重要以及云原生下日志系统的建设难点,相信DevOps.SRE.运维等同学看了是深有体会的.本篇文章单刀直入,会直接跟大家分享一下如 ...

  7. Mybatis实现数据的增删改查

    Mybatis实现数据的增删改查 1.项目结构(使用maven创建项目) 2.App.java package com.GetcharZp.MyBatisStudy; import java.io.I ...

  8. Linux注意事项

    一.学习 Linux 的注意事项 1. Linux 严格区分大小写 Linux 是严格区分大小写的,这一点和 Windows 不一样,所以操作时要注意区分大小写的不同,包括文件名和目录名.命令.命令选 ...

  9. connected datagram 与TCP连接的区别

    TCP连接流程是TCP协议的一部分,需要经过三次握手.而connected datagram虽然使用了socket的同样的函数connect,但是UDP协议并不包含连接流程,也就是UDP实际上并没有真 ...

  10. Linux安装telnet C/S 【白话文】

    1.安装telnet 和telnet-server yum -y install telnet yum -y install telnet-server 注意:在此安装过程中,会依赖解决xinetd的 ...