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 ...
随机推荐
- javascript事件绑定1-模拟jquery可爱的东西
1.给对象添加事件attachEvent(兼容IE,不兼容ff.chrome) <html xmlns="http://www.w3.org/1999/xhtml"> ...
- ListView使用、ListView优化和遇到的问题
1.先写遇到的问题: a.ListView只显示一个item. listview只显示一个item,并且做了listview的点击事件监听打印 Bean 对象的属性和哈希值,发现只有显示的那个 Bea ...
- IIS7部署网站的一些细节问题。
1.不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况. 这个错误的原因是在 IIS 7中 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项.要取消锁定可以以 ...
- Visual Studio UI Automation 学习(一)
这几天需要研究自动化测试工具,因为团队开发使用visual studio,所以需要研究一下Visual studio自带的框架. 刚开始安装的时候,没有选自定义安装,所以安装完成后没有找到UI Aut ...
- 获取url后面的路径
function GetUrlRelativePath() { var url = document.location.toString(); var arrUrl = url.split(" ...
- 【转载】关于DBUtils中QueryRunner的一些解读
前面已经有文章说了DBUtils的一些特性, 这里再来详细说下QueryRunner的一些内部实现, 写的有错误的地方还恳请大家指出. QueryRunner类 QueryRunner中提供对sql语 ...
- Redis学习笔记(一)-持久化
一.RDB持久方式 RDB持久化是把当前进程的数据已快照的形式保存到硬盘的过程. 触发方式: 1.手动触发命令:save和bgsave save:阻塞式,内存较大的实例在执行过程中会造成长时间的阻塞, ...
- Dell R720修改远程管理口的密码
今天有个客户需要通过远程管理口来查看系统事件日志,但是他们把初始密码改过并且还给忘记了.后来我决定进操作系统(cent os)进行修改.整个过程很简单,进入系统后只需要三个步骤就解决问题了 1.安装软 ...
- mysql查询昨天 一周前 一月前 一年前的数据
mysql 昨天 一周前 一月前 一年前的数据 这里主要用到了DATE_SUB, 参考如下 代码如下: SELECT * FROM yh_contentwhere inputtime>DATE_ ...
- elasticsearch 权威指南搜索阅读笔记(四)
多索引多type搜索 分页搜索 每页5条 查询一到3页数据 第一页:http://127.0.0.1:9200/blogs2/product/_search?size=5&from=0 第二页 ...