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
 
 
题目大致题意:
开着坦克去毁坏每个节点的发电站,发电站的能量已知,你从0节点出发,毁坏的能量达到总能量的一半以上即可。每个坦克只可以毁坏一个发电站。求需要走的最短距离是多少
输入:
第一行是组数
第二行是节点数 和 边数
下面 几行是 边  和 权
最后 是 每个节点 发电站 能量
输出
最小距离和,也就是坦克需要消耗的能量
 
解题思路:
因为每个坦克只能毁坏一个节点,所以要用到 背包 动态 来 寻找这个最小 距离。首先,我们根据给出的边,来找出0点到各个定点的最短距离,然后用0/1背包来解决,这里,我们把最大距离当做容量 ,dp 存储 能量值.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#define N 1000001
using namespace std;
int n,m;
int map[][];
int v[];
int dp[];
void Floy()//计算出0到各个发电站的最短距离,因为最多100个点,所以用floy
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(map[i][j]>map[i][k]+map[k][j])
{
map[i][j]=map[i][k]+map[k][j];
}
}
}
}
}
int main()
{
int T,zz,xx,yy,flag;
scanf("%d",&T);
while(T--)
{
flag=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
map[i][j]=N;
map[j][i]=N;
}
map[i][i]=;
}
for(int i=;i<m;i++)
{
scanf("%d%d%d",&xx,&yy,&zz);
if(map[xx][yy]>zz)//有重边
{
map[xx][yy]=zz;
map[yy][xx]=zz;
}
}
Floy();
int sum=,sum1=;
for(int i=;i<=n;i++)
{
scanf("%d",&v[i]);
sum1+=v[i];//计算出发电场的总能量。
if(map[][i]!=N)//计算出所有可行点的距离。
sum+=map[][i];
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)//以总距离为背包容量,发电站的能量为利润。
{
for(int j=sum;j>=map[][i];j--)
{
if(dp[j-map[][i]]+v[i]>dp[j])
{
dp[j]=dp[j-map[][i]]+v[i];
}
}
}
for(int i=;i<=sum;i++)
{
if(dp[i]>sum1/2.0)//必须炸掉一半多才可以阻止战争
{
flag=;
printf("%d\n",i);
break;
}
}
if(flag==) printf("impossible\n"); }
return ;
}

hdu3339In Action(最短路+01背包)的更多相关文章

  1. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

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

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

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

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

  4. HDU-3339 IN ACTION(Dijkstra +01背包)

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

  5. In Action(最短路+01背包)

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

  6. *HDU3339 最短路+01背包

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

  7. HDU 3339 最短路+01背包

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

  8. hdoj--3339--In Action(最短路+01背包)

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

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

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

随机推荐

  1. 很好用的php在线调试工具

    什么叫在线调试?就是在线上生产环境进行调试,假设有一天某个用户报某个页面某个数据怎么不对啊,看来线上出BUG了,于是你要迅速找出原因,首先看日志,可是悲剧的没有足够的日志让你确定线上BUG的原因,也许 ...

  2. 自己实现atoi

    bool myatoi(const char *s,int &num) { cout<<(&s)<<endl; num=; while (*s) { ') { ...

  3. Elasticsearch学习之相关度评分TF&IDF

    relevance score算法,简单来说,就是计算出,一个索引中的文本,与搜索文本,他们之间的关联匹配程度 Elasticsearch使用的是 term frequency/inverse doc ...

  4. js ==和===以及!= 和 !==的区别

    一.js == 与 === 的区别[转] 1. 对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型 ...

  5. C语言程序设计--宏和预处理

    C语言宏 宏定义常量 #include <stdio.h> #define SIZE 100 #define BANNER "WARNING:" int main(vo ...

  6. vue--动态路由和get传值

    动态路由: <template> <div id="News"> <v-header></v-header> <hr> ...

  7. Python之时间模块

    1,怎么打印时间戳 2,怎么打印日期 3,怎么把字符串转换成python认识的日期 把日期转换成字符串 字符串转换成日期格式 time.strptime("2017-5-16",& ...

  8. hdu4998 Rotate【计算几何】

    Noting is more interesting than rotation!  Your little sister likes to rotate things. To put it easi ...

  9. JAVA补充-接口

    1.接口详解 package com.neusoft.interfaced; /** * 接口: * 语法:interface 接口名{ * public static final 变量1=值1: * ...

  10. CNN中的池化层的理解和实例

    池化操作是利用一个矩阵窗口在输入张量上进行扫描,并且每个窗口中的值通过取最大.取平均或其它的一些操作来减少元素个数.池化窗口由ksize来指定,根据strides的长度来决定移动步长.如果stride ...