HDU-3339 IN ACTION(Dijkstra +01背包)
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背包)的更多相关文章
- hdu 3339 In Action (最短路径+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu3339 In Action(Dijkstra+01背包)
/* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...
- hdu 3339 In Action
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- HDU 5234 Happy birthday --- 三维01背包
HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- HDU 1864 最大报销额 0-1背包
HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...
- HDU 3339 In Action(迪杰斯特拉+01背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 3339 In Action【最短路+01背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
随机推荐
- Apache Jmeter进行服务器压力测试
1.前言 最近项目遇到一个问题:其他公司对接我们系统,请求量太大的时候,返回单给对方就是丢失格式,大概十几万中总有那么十几单会出现格式错误! 所以我们老大就叫我用apache jmeter来进行并发测 ...
- 虚拟机添加硬盘RAID5并分区、格式化、挂载使用
当全新安装了一块新的硬盘设备后,为了更充分.安全的利用硬盘空间首先要进行磁盘的分区,然后格式化,最后挂载使用. 1.开启虚拟机之前,先添加硬盘设备,在这里我添加了5块硬盘(5块磁盘,3块做RAID5, ...
- CentOS7 reset脚本,用于初始化新的虚拟机
能用,有待完善 CentOS7测试 哈哈 #!/bin/bash #************************************************************** #Au ...
- PHP 将数据从 Laravel 传送到 vue 的四种方式
在过去的两三年里,我一直在研究同时使用 Vue 和 Laravel 的项目,在每个项目开发的开始阶段,我必须问自己 “我将如何将数据从 Laravel 传递到 Vue ?”.这适用于 Vue 前端组件 ...
- java编程思想第四版第五章总结
1. 构造器 构造器的一个重要的作用: 保证对象被使用之前初始化了. 构造器是一种特殊类型的方法, 因为他没有返回值.这与返回值为空(void)明显不同.对于空返回值,尽管方法本身不会自动返回什么, ...
- PL真有意思(四):控制流
前言 对大多数计算模型而言,顺序都是基本的东西,它确定了为完成所期望的某种工作,什么事情应该最先做,什么事应该随后做,我们可以将语言规定顺序的机制分为几个类别: 顺序执行 选择 迭代 过程抽象 递归 ...
- 2019-9-9:渗透测试,基础学习,windows基础命令,笔记
windows系统基础命令学习 1,命令提示符界面进入方法 方法一: 某分区按住shift,右键单击选择在此处打开windows powershell,进入之后输入cmd 方法二:标题栏输入 方法三: ...
- PL真有意思(六):子程序和控制抽象
前言 在之前我们把抽象定义为一种过程,程序员可以通过它将一个名字与一段可能很复杂的程序片段关联起来.抽象最大的意义就在于,我们可以从功能和用途的角度来考虑它,而不是实现. 在大多数程序设计语言中,子程 ...
- Selenium+Java(五)iframe/frame多表单处理
前言 如果网页中使用了frame,则在使用Selenium定位元素时需要切换到对应的frame,否则会定位不到需要的元素. 切换到需要切换的frame中 driver.switchTo().frame ...
- Kotlin Coroutines在Android中的实践
Coroutines在Android中的实践 前面两篇文章讲了协程的基础知识和协程的通信. 见: Kotlin Coroutines不复杂, 我来帮你理一理 Kotlin协程通信机制: Channel ...