思路:最短路求出到每个点的最小代价,然后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. Linux学习(四)档案与目录管理

    1. 目录与路径  1.1 相对路径与绝对路径  1.2 目录的相关操作: cd, pwd, mkdir, rmdir  1.3 关于执行文件路径的变量: $PATH2. 档案与目录管理  2.1 档 ...

  2. 在ubuntu14.04上漏洞环境vulndocker的DOCKER搭建

    一.下载git包以及安装: git clone https://github.com/leveryd/vulndocker.git cd vulndocker apt-get install uwsg ...

  3. 微信小游戏 Egret开发数据域官方Demo下载地址

    随着引擎的升级,伴随而来就是各种问题,使用官方调试过的Demo,少走弯路. Mark下 官方Demo

  4. 【Android】安卓中常用的图片加载方法

    一.通过相机选图片: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  5. WCF学习 (三)深入认识WCF契约

    什么是契约? 从SOA概念上讲,契约属于服务公开接口的一部分.一个服务契约,定义了服务端公开的服务方法,使用传输协议,可访问地址,传输的消息格式等内容.换句话说:契约描述了该服务的功能和作用,它告诉S ...

  6. 310实验室OTL问题(2)

    1.PyOptimization代码学习心得 (1).该部分由三个分块组成(按完成的相应的任务).第一部分,运行函数. 以optimization.py为入口程序,其思路是:首先获取路径,然后,初始化 ...

  7. docker-compose命令和yml文件配置

    docker-compose -f compose-server.yml up -d version: '3' services: eureka-server: image: mydocker/eur ...

  8. Linux创建Python虚拟环境

    Linux创建Python虚拟环境 安装 pip install virtualenv 基本使用 为一个工程创建一个虚拟环境: $ cd my_project $ virtualenv venv #v ...

  9. 170703、springboot编程之模板使用(thymeleaf、freemarker)

    官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度! thymeleaf的使用 1.在pom中增加thymeleaf支持 <dependency> <g ...

  10. 170531、FormData 对象的使用

    通过FormData对象可以组装一组用 XMLHttpRequest发送请求的键/值对.它可以更灵活方便的发送表单数据,因为可以独立于表单使用.如果你把表单的编码类型设置为multipart/form ...