HDU 3339 In Action 最短路+01背包

时间:2021-11-16 09:38:00

题目链接:

题目

In Action

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

问题描述

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.

输入

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.

输出

The minimal oil cost in this action.

If not exist print "impossible"(without quotes).

样例

input

2

2 3

0 2 9

2 1 3

1 0 2

1

3

2 1

2 1 3

1

3

output

5

impossible

题意

给你一无向图,每个节点有电力值,现在你从0点派出坦克(每单位的距离耗一单位油),每个坦克只有停在一个节点上才能摧毁电力值

问毁坏超过一半的电力的最小耗油量。

题解

跑0到所有点的最短路,然后01背包。

代码

#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; const int maxn = 111;
const int maxm = 10000 + 10;
const int INF = 0x3f3f3f3f; int mat[maxn][maxn];
int power[maxn];
int dp[maxn][maxm];
int n, m; void init() {
for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) {
mat[i][j] = INF;
if (i == j) mat[i][j] = 0;
}
memset(power, 0, sizeof(power));
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < maxn; i++) dp[i][0] = 0;
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &m);
init();
while (m--) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
if (mat[u][v] > w)
mat[u][v] = mat[v][u] = w;
}
int sum = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", power + i);
sum += power[i];
}
for (int k = 0; k <= n; k++) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (mat[i][j] > mat[i][k] + mat[k][j])
mat[i][j] = mat[i][k] + mat[k][j];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = power[i]; j < maxm; j++) {
dp[i][j] = min(dp[i][j], dp[i - 1][j]);
dp[i][j] = min(dp[i][j], dp[i - 1][j - power[i]] + mat[0][i]);
}
}
int ans = INF;
for (int i = sum / 2 + 1; i <= sum; i++) {
ans = min(ans, dp[n][i]);
}
if(ans<INF) printf("%d\n", ans);
else printf("impossible\n");
}
return 0;
}

HDU 3339 In Action 最短路+01背包的更多相关文章

  1. hdu3339In Action&lpar;最短路&plus;01背包)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...

  2. HDU 3339 In Action【最短路&plus;01背包】

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

  3. HDU 3339 In Action【最短路&plus;01背包模板&sol;主要是建模看谁是容量、价值】

     Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...

  4. hdu 3339 In Action &lpar;最短路径&plus;01背包&rpar;

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

  5. hdu 3339 In Action

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

  6. HDU-3339 IN ACTION(Dijkstra &plus;01背包)

      Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...

  7. HDU 3339 In Action(迪杰斯特拉&plus;01背包)

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

  8. hdu 3339 In Action&lpar;迪杰斯特拉&plus;01背包&rpar;

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

  9. HDU 3339 最短路&plus;01背包

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

随机推荐

  1. NPM小结

    nodejs的出现,可以算是前端里程碑式的一个事件,它让前端攻城狮们摆脱了浏览器的束缚,踏上了一个更加宽广的舞台.前端的可能性,从此更加具有想象空间. 随着一系列基于nodes的应用/工具的出现,工作 ...

  2. LeetCode&colon;Clone Graph

    题目如下:实现克隆图的算法  题目链接 Clone an undirected graph. Each node in the graph contains a label and a list of ...

  3. VMware ESXi 5&period;5创建虚拟机

    1.用鼠标右键单击连接到的IP地址,在弹出的快捷菜单中选择“新建虚拟机”选项,或者按“Ctrl+N”热键. 2.选择自定义 3.自定义服务器名称,并且名称最好是英文而且名称需要是唯一的. 4.选择本地 ...

  4. vue loadMore 上拉刷新不能实现的坑

    1.如果你写的代码没问题,但依然不能实现上拉刷新效果,那你有可能是缺少了overflow: scroll 2.如果上拉刷新一直在加载状态,需要调用this.$refs.loadmore.onBotto ...

  5. 每个 Python 程序员都要知道的日志实践

    在现实生活中,记录日志非常重要.银行转账时会有转账记录:飞机飞行过程中,会有黑盒子(飞行数据记录器)记录飞行过程中的一切.如果有出现什么问题,人们可以通过日志数据来搞清楚到底发生了什么. 对于系统开发 ...

  6. 【学亮IT手记】MySql行列转换案例

    create table score( name ), math int, english int ); ,); ,); ,); ,); SHOW tables; SELECT * from scor ...

  7. DEX文件类型和虚拟机(摘抄)

    DEX文件类型是Android平台上可执行文件的类型. Dalvik是Google公司自己设计用于Android平台的Java虚拟机.Dalvik虚拟机是Google等厂商合作开发的Android移动 ...

  8. python redis插件安装

    #tar xvzf redis-py-2.2.1.tar.gz #cd redis-py-2.2.1 #python setup.py install   附件: https://app.yinxia ...

  9. Spring Boot with Docker

    翻译自:https://spring.io/guides/gs/spring-boot-docker/ Spring Boot with Docker 这篇教程带你一步步构建一个Docker镜像用来运 ...

  10. TensorFlow------读取CSV文件实例

    TensorFlow之读取CSV文件实例: import tensorflow as tf import os def csvread(filelist): ''' 读取CSV文件 :param fi ...