思路:最短路求出到每个点的最小代价,然后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(最短路+背包)题解的更多相关文章

  1. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  2. hdu 3339 In Action (最短路径+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. HDU 3339 In Action(迪杰斯特拉+01背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3339 In Action Time Limit: 2000/1000 MS (Java/Others) ...

  4. HDU 3339 In Action【最短路+01背包】

    题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=3339] In Action Time Limit: 2000/1000 MS (Java/Other ...

  5. HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】

     Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...

  6. hdu 3339 In Action 背包+flyod

    In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=333 ...

  7. hdu 3339 In Action(迪杰斯特拉+01背包)

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

  9. hdu 5534 Partial Tree(完全背包)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5534 题解:这题一看有点像树形dp但是树形dp显然没什么思路.然后由于这里的约束几乎没有就 ...

随机推荐

  1. LeetCode——Search a 2D Matrix II

    Description: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix ...

  2. video事件

    /** video播放器*/ * @ src: 指定所要嵌入视频.文档的URL. * @ poster: 视频预览图像 * @ autoplay: 视频自动播放 * @ loop: 循环播放 * @ ...

  3. iOS - 常用iOS的第三方框架

    图像:1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...

  4. linux :故障提示:Error:No suitable device found: no device found for connection "System eth0"

    解决:1./etc/udev/rules.d/70-persistent-net.rules 中的mac地址2.网卡配置文件ifcfg-eth0中的mac地址3.ifconfig中的mac地址以上三个 ...

  5. 系统事件管理(Events) ---- HTML5+

    模块:events Events模块管理客户端事件,包括系统事件,如扩展API加载完毕.程序前后台切换等. 比如说:网络的链接的和断开这种事件,系统从前台走到后台这种事件: 不包括:点击和滑动页面事件 ...

  6. 试验性的Numpy教程(译)

    Python中Numpy模块学习: 转自:http://my.oschina.net/u/175377/blog/74406

  7. vuls漏洞扫描工具

    Vuls 是一款适用于 Linux/FreeBSD 的漏洞扫描程序,无代理,采用 Go 语言编写,对于系统管理员来说,每天必须执行安全漏洞分析和软件更新都是一个负担. 为避免生产环境宕机,系统管理员通 ...

  8. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  9. ar的主流算法

    基于无标志AR:代表作是PTAM/M,Mixare,将是AR未来的发展方向 跟踪技术可以大致分成两大类,一类是基于特征的跟踪(Feature Based Tracking),比如通过跟踪从输入图像中抽 ...

  10. Dubbo学习记录

    参考资料: 官网 Dubbo详细介绍与安装使用过程