HDOJ 3339 In Action
最短路+01背包
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3857 Accepted Submission(s): 1229

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.
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).
2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3
5
impossible
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int n,m;
int dist[110],vis[110],power[110];
int g[110][110];
int dp[11000]; void dijkstra()
{
memset(dist,63,sizeof(dist));
memset(vis,0,sizeof(vis)); dist[0]=0; for(int j=0;j<=n;j++)
{
int mark=-1,mindist=INF;
for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
if(mindist>dist[i])
{
mindist=dist[i]; mark=i;
}
} if(mark==-1) break;
vis[mark]=1; for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
dist[i]=min(dist[i],dist[mark]+g[mark][i]);
}
}
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
memset(g,63,sizeof(g)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=min(g[a][b],c);
} dijkstra(); int sumd=0,sum=0; for(int i=1;i<=n;i++)
{
scanf("%d",power+i);
if(dist[i]!=INF) sumd+=dist[i];
sum+=power[i];
} memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
{
if(dist[i]==INF) continue;
for(int j=sumd;j>=dist[i];j--)
{
dp[j]=max(dp[j],dp[j-dist[i]]+power[i]);
}
}
int ans=-1,low=0,high=sumd,mid;
while(low<=high)
{
mid=(low+high)/2;
if(dp[mid]*2>sum)
ans=mid,high=mid-1;
else low=mid+1;
} if(ans==-1)
puts("impossible");
else printf("%d\n",ans);
}
return 0;
}
HDOJ 3339 In Action的更多相关文章
- 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 背包+flyod
In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=333 ...
- 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(迪杰斯特拉+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3339 In Action
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- 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背包,求出某一代价所能拿到的最大价值,然后搜索最后结果. 代码: #include<cstdio> #include<set> #i ...
随机推荐
- 分析AWR报告
1.AWR报告头信息 DB Name :数据库名字 DBid: 数据库id Elapsed:采样时间段 DB Time:用户操作花费的时间,不包括Oracle后台进程消耗的时间 DB Time远小于E ...
- 将python的程序包装成windows下的service
使用python编写的脚本应用程序,在运行的时候需要有python的运行环境,但是我们肯定是希望整个python程序能够像应用程序一样打包生成一个包括其运行环境的exe文件包,这是第一步,但是要想使用 ...
- Hdu-6242 2017CCPC-哈尔滨站 M.Geometry Problem 计算几何 随机
题面 题意:给你n个点,让你找到一个圆,输出圆心,和半径,使得有超过一半的点刚好在圆上.n<=1e5,题目保证了有解 题解:刚开始看着很不可做的样子,但是多想想,三点确定一个圆,三点啊! 现在有 ...
- 2.TinkPHP入门----控制器
1.控制器创建 命名规则:控制器名称+Controller+.class.php, 例如GoodsController.class.php UserController.class.php 控制器结 ...
- 第7章 性能和可靠性模式 Failover Cluster(故障转移群集)
上下文 您已经决定在设计或修改基础结构层时使用群集以提供高度可用的服务. 问题 您应该如何设计一个高度可用的基础结构层,来防止因单台服务器或它所运行的软件出现故障而导致的服务丢失? 影响因素 在设计高 ...
- 豆瓣项目(用react+webpack)
用豆瓣电影api的项目 电影列表组件渲染 步骤: 1. 发送Ajax请求 1.1 在组件的componentWillMount这个生命周期钩子中发送请求 1.2 发送ajax XMLHttpReque ...
- 如何拿到阿里算法校招offer
好多同学有问过怎么能拿到阿里算法类校招的offer,刚好看到这篇文章分享给大家,详情可以看原文链接,原文链接中有视频讲解. 师兄师姐的建议: 之前初学算法的时候上过的公开课和看过的书 1. Cours ...
- 几个概念:x86、x86-64和IA-32、IA-64
最近在学习操作系统方面的知识,学习操作系统难免要和CPU打交道,虽然现在CPU和操作系统不像计算机发展初期一样是绑定在一起的,但是大家都知道操作系统和CPU Architecture的联系是很紧密的, ...
- Visual Studio 编辑代码量大的文件时分屏技巧
- Spark的协同过滤.Vs.Hadoop MR
基于物品的协同过滤推荐算法案例在TDW Spark与MapReudce上的实现对比,相比于MapReduce,TDW Spark执行时间减少了66%,计算成本降低了40%. 原文链接:http://w ...