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显然没什么思路.然后由于这里的约束几乎没有就 ...
随机推荐
- stringstream读入每行数据
做了下阿里的编程测试题,就30分钟,不是正常的输入输入,直接给一个数组作为输入. 于是带想题和处理数据花了20分钟,最后10分钟搞一个dij模版, 竟然只过了66%,应该是我数组开小了. 题目数据量没 ...
- strace命令的使用
author: headsen chen date: 2018-08-28 21:25:48 跟踪一个命令的过程: [root@zabbix-test ~]# yum -y install st ...
- Lucene中最简单的索引和搜索示例
package com.jiaoyiping.lucene; import org.apache.lucene.analysis.standard.StandardAnalyzer; import o ...
- 微信小程序5.2.2版本,找不着resource下exml皮肤
问题描述: egret engine 5.2.2 原来5.1.11好好的,一升级就跪了 新建一个项目,找不到皮肤... 已发到论坛问去了,现在只能手动复制皮肤到小游戏目录下... 解决方案: 卸载重新 ...
- 网络流SAP+gap+弧优化算法
poj1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54962 Accept ...
- postgresql----字符串函数与操作符
函数 返回值类型 描述 示例 结果 string||string text 字符串连接 select 'Post'||'gresql'||' good!'; Postgresql good! stri ...
- FZU 2105 Digits Count
Problem 2105 Digits Count Accept: 444 Submit: 2139 Time Limit: 10000 mSec Memory Limit : 2621 ...
- Dubbo学习记录
参考资料: 官网 Dubbo详细介绍与安装使用过程
- xplan-打印执行顺序
-- ------------------------------------------------------------------------------------------------- ...
- talib 中文文档(十四):Math Transform Functions 数学变换
Math Transform Functions ACOS - Vector Trigonometric ACos 函数名:ACOS 名称:acos函数是反余弦函数,三角函数 real = ACOS( ...