传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=3339

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7869    Accepted Submission(s): 2674

Problem Description

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.
 
Input
The first line of the input contains a single integer T, specifying the number of testcase in the file.
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.
 
Output
The minimal oil cost in this action.
If not exist print "impossible"(without quotes).
 
Sample Input
2
2 3
0 2 9
2 1 3
1 0 2
1
3
2 1
2 1 3
1
3
 
Sample Output
5
impossible
 
Author
Lost@HDU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  1217 1385 1548 3338 2112 
 
分析:
题目意思:
n个发电站,m条路,每条路有各自的距离,每个发电站有各自的发电量,现在需要炸毁它们,一辆坦克只能炸毁一个发电站,而且需要炸毁的发电厂的发电量需要大于所有发电站所产生的总电量的一半,求坦克走的最短距离。
 
做法:
根据这个图,找到0到其他点的最短路(迪杰斯特拉)
然后每个发电厂的功率看成物品价值
0到每个点的距离看成物品的花费
背包容量是花费的总和
然后就是01背包
注意当dis==INF的时候,表示不连通,物品花费不要加入到背包容量中(调试了很久)
md
code:
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
#include<vector>
using namespace std;
typedef long long LL;
#define max_v 10005
#define INF 99999999 int dp[max_v];
int v[max_v];
int cost[max_v]; int e[max_v][max_v];
int n,m;
int used[max_v];
int dis[max_v];
void init()
{
memset(used,,sizeof(used));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
e[i][j]=INF;
}
dis[i]=INF;
}
}
void Dijkstra(int s)
{
for(int i=; i<=n; i++)
{
dis[i]=e[s][i];
}
dis[s]=;
for(int i=; i<=n; i++)
{
int index,mindis=INF;
for(int j=; j<=n; j++)
{
if(used[j]==&&dis[j]<mindis)
{
mindis=dis[j];
index=j;
}
}
used[index]=;
for(int j=; j<=n; j++)
{
if(dis[index]+e[index][j]<dis[j])
dis[j]=dis[index]+e[index][j];
}
}
}
void ZeroOnePack_improve(int n,int c)
{
memset(dp,,sizeof(dp));
for(int i=; i<=n; i++)
{
for(int j=c; j>=cost[i]; j--)
{
dp[j]=max(dp[j],dp[j-cost[i]]+v[i]); }
}
}
int main()
{
int t;
int a,b,c;
int sum;
int sumv;
int flag;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
n++;
init();
for(int i=; i<m; i++)
{
scanf("%d %d %d",&a,&b,&c);
a++;
b++;
if(e[a][b]>c)
e[a][b]=e[b][a]=c;
}
sumv=;
for(int i=; i<=n-; i++)
{
scanf("%d",&v[i]);//价值
sumv+=v[i];
} Dijkstra();
int k=;
sum=;
for(int i=; i<=n; i++)
{
cost[k++]=dis[i];//花费
if(dis[i]!=INF)//wa点
sum+=dis[i];
}
ZeroOnePack_improve(k-,sum);
flag=;
for(int i=; i<=sum; i++)
{
if(dp[i]>=sumv/+)
{
flag=;
printf("%d\n",i);
break;
}
}
if(flag==)
printf("impossible\n");
}
return ;
}

HDU 3339 In Action(迪杰斯特拉+01背包)的更多相关文章

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

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

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

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

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

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

  4. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  8. hdu 1142(迪杰斯特拉+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. hdu 1595 find the longest of the shortest(迪杰斯特拉,减去一条边,求最大最短路)

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

随机推荐

  1. 如何迎接新的 .NET 时代

    看完.NET 基金会. Roslyn 编译器 ,应该已经能慢慢了解,现在所谓的“.NET 开源”.“.NET Open Source”并不是完全把现有的 .NET Framework 整个打开(虽然这 ...

  2. Spring入门(四)— 整合Struts和Hibernate

    一.Spring整合Struts 1. 初步整合 只要在项目里面体现spring和 strut即可,不做任何的优化. struts 环境搭建 创建action public class UserAct ...

  3. Struts 类型转换之局部和全局配置

    我们碰到过很多情况,就是时间日期常常会出现错误,这是我们最头疼的事,在struts2中有一些内置转换器,也有一些需要我们自己配置. 我们为什么需要类型转换呢? 在基于HTTP协议的Web应用中 客户端 ...

  4. dukuwiki简单教程

    =====请先阅读下面的说明,有助于你快速入门===== * DokuWiki(也就是我们通常称谓的wiki) 支持一些简单的标记语言, 以尽最大可能使文档看上去更友好. * 你可以把它理解为一种和c ...

  5. 浏览器组成、线程及event loop

    浏览器组成 User interface: a. Every part of the browser display, except the window. b. The address bar, b ...

  6. Java 社区论坛 - Sym 1.5.0 发布

    简介 Sym 是一个用 Java 写的实时论坛,欢迎来 体验!(如果你需要搭建一个企业内网论坛,请使用 SymX) 非常详细的 Sym 功能点脑图 如果你在搭建或者二次开发时碰到问题,欢迎加 Q 群 ...

  7. npdp

    我报名比较晚,等缴费最后期限,才缴费,下定决心,开始正式的备考. 我的工作比较忙,备考时间特比较短,从拿到书到考试只有一个月了,心理慌慌的. 在岳老师的帮助下,完成了报名资格申请.拿到备考计划,就赶紧 ...

  8. DOM介绍

    什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节点组成的. 解析过程 ...

  9. 最近选购MP3而有感便携追求音质的一些心得

    之前的创新小石头MP3的耳机接口松动了.考虑到它已经服役了4年了.所以我准备重新买一个.而小石头出色的外放,我决定让给宝宝当玩具. 选购心得MP3的时候,原来的主导思想,是在低价位的里面考虑一台国际品 ...

  10. Git在eclipse中的配置

    1:git在eclipse中的配置 windows - >preferences->team->git->configuration 点击add Entry key值:输入 u ...