hdu 3339 In Action(迪杰斯特拉+01背包)
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5102 Accepted Submission(s):
1696

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.
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.
If not exist
print "impossible"(without quotes).
impossible
#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背包)的更多相关文章
- 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 ...
- 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 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 3790(两种权值的迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- hdu 1142(迪杰斯特拉+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 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 ...
随机推荐
- NKOJ3485 【2015多校联训4】数据
问题描述 Mr_H 出了一道信息学竞赛题,就是给 n 个数排序.输入格式是这样的:试题有若干组数据.每组数据的第一个是一个整数 n,表示总共有 n 个数待排序:接下来 n 个整数,分别表示这n 个待排 ...
- WCF 服务
1.代码 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Seriali ...
- 利用Factory-boy来生成实例数据
库和版本:Faker==2.0.0factory-boy==2.12.0 官方文档:https://factoryboy.readthedocs.io/en/latest/index.html 1. ...
- 廖雪峰Python总结4
面向对象编程 将计算机程序视为一系列的命令集合.包含: 数据 操作数据的函数 Python中,所有的数据类型都可以视为对象. 面向对象特点:封装,继承,多态. 类的函数和普通函数:类的第一个参数永远是 ...
- cat、head、tail、more和less命令(文件内容浏览)
一.cat命令 cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容. 注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容.因此,一般用more等命令分屏显 ...
- SpringBoot Cloud eureka 注册中心
SpringBoot Cloud是什么 Spring Cloud是一个分布式的整体解决方案. Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一 ...
- Nginx教程(7) 正向代理与反向代理【总结】 (转)
1.前言 最近工作中用到反向代理,发现网络代理的玩法还真不少,网络背后有很多需要去学习.而在此之前仅仅使用了过代理软件,曾经为了访问google,使用了代理软件,需要在浏览器中配置代理的地址.我只知道 ...
- thinkphp5.0 使用action()报Cannot redeclare app\home\controller\CheckSubstrs()错误
原因:Common公共类方法isMobile()内部定义了函数CheckSubstrs(),在使用action()时,会调用两次isMobile(),导致函数CheckSubstrs()重复定义 解决 ...
- COGS-2638 区间与,异或,询问max
本篇题解参考了这个博客 题目链接 我们利用线段树来维护区间第最大值,考虑如何修改 每一次进行与操作时只有z的二进制为0的位会产生影响 每一次进行或操作时只有z的二进制为1的位会产生影响 所以只要该区间 ...
- Android 高仿微信语音聊天页面高斯模糊效果
目前的应用市场上,使用毛玻璃效果的APP随处可见,比如用过微信语音聊天的人可以发现,语音聊天页面就使用了高斯模糊效果. 先看下效果图: 仔细观察上图,我们可以发现,背景图以用户头像为模板,对其进行了高 ...