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

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<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 110
int map[MAXN][MAXN],vis[MAXN],dis[MAXN],dp[11000],p[MAXN];
int n,m;
void disjtra()
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
for(int i=0;i<=n;i++)
dis[i]=map[0][i];
vis[0]=1;
for(int i=1;i<=n;i++)
{
int minn=INF;
int flag=0;
for(int j=0;j<=n;j++)
{
if(!vis[j]&&minn>dis[j])
{
minn=dis[j];
flag=j;
}
}
vis[flag]=1;
for(int j=0;j<=n;j++)
{
if(!vis[j]&&dis[flag]+map[flag][j]<dis[j])
{
dis[j]=dis[flag]+map[flag][j];
}
}
}
}
int main()
{
int t,i,j;
int a,b,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
memset(map,INF,sizeof(map));
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])
map[a][b]=map[b][a]=c;
}
int sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
sum+=p[i];
}
sum/=2;
disjtra();
int v=0;
for(i=1;i<=n;i++)
{
if(dis[i]!=INF)
v+=dis[i];
}
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
if(dis[i]!=INF)
{
for(j=v;j>=dis[i];j--)
dp[j]=max(dp[j],dp[j-dis[i]]+p[i]);
}
}
int flag=1;
for(i=0;i<=v;i++)
{
if(dp[i]>sum)
{
flag=0;
break;
}
}
if(flag)
printf("impossible\n");
else
printf("%d\n",i);
}
return 0;
}
hdoj--3339--In Action(最短路+01背包)的更多相关文章
- HDU 3339 In Action 最短路+01背包
题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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 ...
- HDOJ 3339 In Action
最短路+01背包 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU-3339 IN ACTION(Dijkstra +01背包)
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- In Action(最短路+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- React Component(dva)
Stateless Functional Components(3种方式) class App extends React.Component function App() const App= Re ...
- Django models 常用字段类型
1.CharField字符串字段,存较短的字符串,长文本要用TextField.必须的参数:max_length 字符的最大长度2.TextField容量很大的文本字段.admin中用 <tex ...
- Android常见错误整理
1.当我new class的时候,提示以下错误: Unable to parse template "Class" Error message: This template did ...
- SQL优化--使用 EXISTS 代替 IN 和 inner join来选择正确的执行计划
在使用Exists时,如果能正确使用,有时会提高查询速度: 1,使用Exists代替inner join 2,使用Exists代替 in 1,使用Exists代替inner join例子: 在一般写s ...
- PAC学习理论:机器学习那些事
参考翻译,有大量删除和修改,如有异议,请拜访原文.一定要看英文原文!!!. 本文转载于:深度译文:机器学习那些事 英文[原题]A Few Useful Things to Know About Mac ...
- 人脸检测的harr检测函数
眼球追踪需要对人脸进行识别,然后再对人眼进行识别,判断人眼张合度,进而判断疲劳... 解析:人脸检测的harr检测函数使用方法 代码理解: 利用训练集,检测出脸部,画出框 void CAviTestD ...
- 【转载】使用JSONObject生成和解析json
1. json数据类型 类型 描述 Number 数字型 String 字符串型 Boolean 布尔型 Array 数组 Object 对象 null 空值 (1)json中不区分整数.小数等类型, ...
- Vue解决跨域之反向代理
目录 : config/index.js module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPa ...
- CSS设置input默认样式
HTML <ul class="box"> <li> <input type="checkbox" name="vehi ...
- 亚马逊免费服务器搭建Discuz!论坛过程(一)
1:申请 目前亚马逊服务器免费12个月,需要一张信用卡即可免费注册领取. 地址:https://aws.amazon.com/cn/free/ 2: 创建实例 2.1进入控制台:https://ap- ...