HDU 3339 In Action(最短路+背包)题解
思路:最短路求出到每个点的最小代价,然后01背包,求出某一代价所能拿到的最大价值,然后搜索最后结果。
代码:
#include<cstdio>
#include<set>
#include<cmath>
#include<stack>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn = +;
const int INF = 0x3f3f3f3f;
int lowcost[maxn],cost[maxn][maxn];
int dp[],power[maxn];
bool vis[maxn];
int n,m;
void Dijkstra(int st){
for(int i = ;i <= n;i++){
lowcost[i] = cost[][i];
vis[i] = false;
}
lowcost[st] = ;
for(int i = ;i < n;i++){
int k = -,MIN = INF;
for(int j = ;j <= n;j++){
if(!vis[j] && lowcost[j] < MIN){
MIN = lowcost[j];
k = j;
}
}
if(k == -) break;
vis[k] = true;
for(int j = ;j <= n;j++){
if(!vis[j] && lowcost[j] > lowcost[k] + cost[k][j]){
lowcost[j] = lowcost[k] + cost[k][j];
}
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(cost,INF,sizeof(cost));
scanf("%d%d",&n,&m);
while(m--){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
cost[u][v] = cost[v][u] = min(cost[u][v],w);
}
for(int i = ;i <= n;i++)
scanf("%d",&power[i]);
int st = ;
for(int i = ;i <= n;i++){
st += power[i];
}
st = st / + ; Dijkstra();
int ALL = ;
for(int i = ;i <= n;i++){
if(lowcost[i] != INF)
ALL += lowcost[i];
}
memset(dp,,sizeof(dp));
for(int i = ;i <= n;i++){
if(lowcost[i] == INF) continue;
for(int j = ALL;j >= lowcost[i];j--){
dp[j] = max(dp[j],dp[j - lowcost[i]] + power[i]);
}
}
int Min = -;
for(int i = ;i <= ALL;i++){
if(dp[i] >= st){
Min = i;
break;
}
}
if(Min != -) printf("%d\n",Min);
else printf("impossible\n");
}
return ;
}
HDU 3339 In Action(最短路+背包)题解的更多相关文章
- 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 (最短路径+01背包)
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 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 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
http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...
- hdu 5534 Partial Tree(完全背包)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...
随机推荐
- MQTT的学习研究(二)moquette-mqtt 的使用之mqtt broker的启动
在MQTT 官网 (http://mqtt.org/software)中有众多MQTT的实现方式.具体参看官网,Moquette是基于Apache Mina 的模型的一个Java MQTT broke ...
- hibernate在Oracle中插入数据,默认字段被设置为null的问题解决
参考内容: http://blog.sina.cn/dpool/blog/s/blog_90629d5301014a5w.html 在数据库中一个字段的默认值为1,但是在插入数据后,本来该字段为空,值 ...
- centos7 安装后,出现Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]
PS:出现以上信息,是要求你阅读或者接收协议: Initial setup of CentOS Linux 7 (core)解决步骤如下: 1,输入[1],按Enter键阅读许可协议,2,输入[2], ...
- [转]ORM的优缺点
ORM[Object-Relation-Mapping]对象关系映射. 这个名词已经出来好几年了.已经不陌生. 以前在项目中针对相对复杂业务逻辑时一般采用领域模型驱动方式进行业务概述,分析和建模. ...
- Intellij IDEA常用配置详解
1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...
- Oracle等待事件之Enqueue(锁)
1.产生原因 表示与内部队列机制相关的等待,例如对保护内部资源或者组件的锁的请求等,一种并发的保护机制,该等待意味着在访问同样的数据库资源时需要等待其他会话已经获取的锁. 2.确定产生问题的对象 死锁 ...
- scrapy爬虫系列之二--翻页爬取及日志的基本用法
功能点:如何翻页爬取信息,如何发送请求,日志的简单实用 爬取网站:腾讯社会招聘网 完整代码:https://files.cnblogs.com/files/bookwed/tencent.zip 主要 ...
- 升级mac xcode打包证书报错 git 报错
reset tryAgain git 在钥匙串中找不到指定的项 重新配置公钥撕咬 SSH keys An SSH key allows you to establish a secure conne ...
- docker 数据管理数据卷
1,数据卷 数据卷是一个可供容器使用特殊目录,他将主机操作系统目录直接映射容器 1,在容器内创建一个数据卷 在使用docker run命令的时候,使用-v标记就可以创建一个数据卷,多次重复使用-v可以 ...
- day15(Mysql学习)
day15-MySQL 数据库 1 数据库概念(了解) 1.1 什么是数据库 数据库就是用来存储和管理数据的仓库! 数据库存储数据的优先: 可存储大量数据: 方便检索: 保持数据的一致性. ...