HDU 3339 In Action 最短路+01背包
题目链接:
题目
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背包的更多相关文章
- hdu3339In Action(最短路+01背包)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...
- HDU 3339 In Action【最短路+01背包】
题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...
- HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- hdu 3339 In Action (最短路径+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 3339 In Action
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- HDU-3339 IN ACTION(Dijkstra +01背包)
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...
- 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背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- Wireshark - 观察 ARP 地址解析过程
下面使用 Wireshark 分析 ARP 的工作过程.试验的机器:发送者机器(IP 地址:10.21.28.47,MAC 地址:68:f7:28:0f:32:2e)下文称为 HOSTA:目标机器(I ...
- .NET 的webservice例子
因为项目的需要,可能会经常性的需要调用接口,或者写一些接口.现在提供一些简单的例子给大家参考 写接口: [WebServiceBinding(ConformsTo = WsiProfiles.Basi ...
- 《Web编程入门经典》
在我还不知道网页的基础结构的时候,我找过很多本介绍Web基础的书籍,其中这本<Web编程入门经典>,我认为是最好的. 这本书内容很全面.逻辑很严谨.结构很清晰.语言文字浅显易懂. 看这本书 ...
- linux之Gcc使用
本篇文章摘自 http://blog.csdn.net/ljzcom/article/details/7213101 对于Gcc自带的很多命令行,只有具体编程里面用到了或者看到别人真真用到的,用的目的 ...
- 终于解决了我的DISCUZ 无法连接到您的服务器,可能您的服务器处于防火墙后端 论坛云平台的问题~
事由:由于前几天折腾备份,将论坛源文件误删了大部分,于是我重新下载了源码,传到了空间. 然后问题来了,我关闭纵横搜索提示“无法连接到您的服务器,可能您的服务器处于防火墙后端”,设置纵横搜索页一直显示“ ...
- 按照自己的理解实现比特交换协议(alternating-bit protocol)
一开始的思路是想写两个程序的,发送端和接收端.但是后来想了下,因为是模拟的,所以不用这么麻烦,直接自己定制场景(比如说丢包阿,包出错阿,超时之类的). 基本上是根据上图所写的,一个函数发包,一个函数接 ...
- MVC3缓存:使用页面缓存
在以前的WebForm的开发中,在页面的头部加上OutputCache即可启用页面缓存,而在MVC3中,使用了Razor模板引擎的话,该如何使用页面缓存呢?如何启用 在MVC3中要如果要启用页面缓存, ...
- DTcms列表隔行换色;loop自带行号
<%loop cdr2 bcategoryList%> <%if(cdr2__loop__id==1)%> <a class="no-bg" href ...
- delphi中的临界区
var fLock:TRTLCriticalSection; //定义临界区域 // 初始化 InitializeCriticalSection(fLock); //进入临界区 EnterCritic ...
- Hello World 的makefile模板及其分析
makefile模板: ifeq ($(KERNELRELEASE),) //判断KERNELRELEASE是否为空,只有执行make的当前目录为内核源代码目录时,该变量才不为空. KERNELDIR ...