In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5102    Accepted Submission(s):
1696

Problem 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
 
Author
Lost@HDU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar
problems for you:  1385 3338 1102 3342 1598 
 
先用迪杰斯特拉求出从0点到各点的最短距离,由于需要大于一半总能量,所以需要判断是否取这个点,使用01背包解决。
 
题意:第一个数为数据组数,然后输入n,m,代表有n个发电站,m条路,然后给出m条路和该路消耗的油,然后是n行,每行是相应的发电站的能量,要注意,每个发电站必须停一辆坦克才能控制这个发电站,只有控制的总能量超过一半能使其瘫痪
 
附上代码:
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#define M 105
#define inf 0x3f3f3f3f
using namespace std;
int map[M][M],dis[M],vis[M];
int dp[];
int mins(int a,int b)
{
return a>b?b:a;
}
int main()
{
int i,j,n,m,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
for(i=; i<=n; i++)
for(j=; j<=n; j++)
if(i==j) map[i][j]=;
else map[i][j]=inf;
int a,b,c;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}
vis[]=;
for(i=; i<=n; i++)
dis[i]=map[][i];
int min,t;
for(i=; i<=n; i++) //迪杰斯特拉
{
min=inf;
for(j=; j<=n; j++)
if(!vis[j]&&min>dis[j])
{
min=dis[j];
t=j;
}
vis[t]=;
for(j=; j<=n; j++)
if(!vis[j]&&map[t][j]<inf)
if(dis[j]>dis[t]+map[t][j])
dis[j]=dis[t]+map[t][j];
}
int s[M],sum=;
for(i=; i<=n; i++)
{
scanf("%d",&s[i]);
sum+=s[i];
}
for(i=; i<=sum; i++)
dp[i]=inf;
dp[]=;
for(i=; i<=n; i++) //01背包
for(j=sum; j>=s[i]; j--)
dp[j]=mins(dp[j],dp[j-s[i]]+dis[i]);
int x=sum/+,mm=inf;
for(i=x; i<=sum; i++) //选出最小的距离
if(dp[i]<mm)
mm=dp[i];
if(mm<inf)
printf("%d\n",mm);
else
printf("impossible\n");
}
return ;
}

hdu 3339 In Action(迪杰斯特拉+01背包)的更多相关文章

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

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (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 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  8. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. 【模板】Tarjan缩点,强连通分量 洛谷P2341 [HAOI2006]受欢迎的牛 [2017年6月计划 强连通分量01]

    P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...

  2. node安装镜像和webpack

    先安装node 安装指南:https://npm.taobao.org/

  3. JDBC vs Hibernate(转)

    jdbc和Hibernate区别 刚开始学习JAVA时,认为Hibernate是一个很神圣的东西,好像是会了SSH,就能走遍全世界一样.记得曾经在枫叶面试的时候,我们几个同学出还说这个公司怎么这么的落 ...

  4. hive拉链表取数

    例如,一个借款用户在hive上的拉链表.(end_dt存放逻辑与普通介绍的拉链表不一致) 需要拉去它在2019-05-01日的状态, 取数逻辑是: select * from tb where sta ...

  5. ORACLE 使用笔记

    ORACLE TRUNC()函数 TRUNC():类似截取函数,按指定的格式截取输入的数据. 1.[trunc(for dates)]TRUNC()函数处理日期 语法格式:TRUNC(date[,fm ...

  6. RestFul 与 RPC

    原文地址:https://blog.csdn.net/u014590757/article/details/80233901 RPC.REST API深入理解 一:RPC RPC 即远程过程调用(Re ...

  7. Python要如何实现(列表)排序?

    排序,是许多编程语言中经常出现的问题.同样的,在Python中,如何是实现排序呢?(以下排序都是基于列表来实现) 一.使用Python内置函数进行排序 Python中拥有内置函数实现排序,可以直接调用 ...

  8. PHP学习(运算符)

    PHP运算符一般分为算术运算符.赋值运算符.比较运算符.三元运算符.逻辑运算符.字符串连接运算符.错误控制运算符. 算术运算符 主要是用于进行算术运算的,例如:加法运算.减法运算.乘法运算.除法运算 ...

  9. kubernetes1.4新特性:支持两种新的卷插件

    背景介绍 在Kubernetes中卷的作用在于提供给POD持久化存储,这些持久化存储可以挂载到POD中的容器上,进而给容器提供持久化存储. 从图中可以看到结构体PodSpec有个属性是Volumes, ...

  10. 大数据技术之Hive

    第1章 Hive入门 1.1 什么是Hive Hive:由Facebook开源用于解决海量结构化日志的数据统计. Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提 ...