In Action

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

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:  3338 1546 3333 3341 3335 
 
 //187MS    616K    1856 B    C++
/* 题意:
题意挺重要的。一个源点0,源点每到一个站可得到一个pow[i],求得到总pow值一半以上时最短行走距离。 最短路+01背包:
首先最短路求出源点0到每一个点的距离,然后得出n个点,每个点有一个距离花费d,和一个价值权值w,
以到所有的点最短路程和作为背包容量,距离作为花费,权值作为价值,01背包。
当dp[j]>sum_pow/2+1时的距离和j为所求。 */
#include<iostream>
#include<queue>
#include<vector>
#define N 105
#define inf 0x7ffffff
using namespace std;
typedef struct node{
int d,w;
}node;
node p[N];
int vis[N];
int d[N];
int dp[N*N];
vector<node>V[N];
int n;
int Max(int x,int y)
{
return x>y?x:y;
}
void spfa(int s)
{
for(int i=;i<=n;i++)
d[i]=inf;
d[s]=;
vis[s]=;
queue<int>Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();
Q.pop();
vis[u]=;
int m=V[u].size();
for(int i=;i<m;i++){
int v=V[u][i].d;
int w=V[u][i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
int main(void)
{
int t,m;
int u,v,w,pow;
scanf("%d",&t);
while(t--)
{
int sd=,sw=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
node q={v,w};
V[u].push_back(q);
q.d=u;
V[v].push_back(q);
}
for(int i=;i<=n;i++){
scanf("%d",&p[i].w);
sw+=p[i].w;
}
spfa();
for(int i=;i<=n;i++){
p[i].d=d[i];
if(d[i]!=inf)
sd+=p[i].d;
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
for(int j=sd;j>=p[i].d;j--)
dp[j]=Max(dp[j],dp[j-p[i].d]+p[i].w);
int ans=;
for(int i=;i<=sd;i++)
if(dp[i]>=sw/+){
ans=i;break;
}
if(ans==) puts("impossible");
else printf("%d\n",ans);
}
return ;
}

hdu 3339 In Action (最短路径+01背包)的更多相关文章

  1. HDU 5234 Happy birthday --- 三维01背包

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  2. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  3. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  4. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  5. HDU 1864 最大报销额 0-1背包

    HDU 1864 最大报销额 0-1背包 题意 现有一笔经费可以报销一定额度的发票.允许报销的发票类型包括买图书(A类).文具(B类).差旅(C类),要求每张发票的总额不得超过1000元,每张发票上, ...

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

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

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

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

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

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

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

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

随机推荐

  1. CF480Div2掉分记

    rating 1900+参加只有Div2的比赛也记rating了.还以为yyc报名没打会惨惨,原来不交题好像就不算参加.. 本来太晚了不想打,不过有Sinogi大佬带我还是打一打吧,apio之前练练手 ...

  2. 优步UBER司机全国各地奖励政策汇总 (3月7日-3月13日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. 北京Uber优步司机奖励政策(12月2日)

    用户组:人民优步(适用于12月2日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...

  4. Mybatis之关联查询及动态SQL

    前言 实际开发项目中,很少是针对单表操作,基本都会联查多表进行操作,尤其是出一些报表的内容.此时,就可以使用Mybatis的关联查询还有动态SQL.前几篇文章已经介绍过了怎么调用及相关内容,因此这里只 ...

  5. LeetCode: 54. Spiral Matrix(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix/description/ 2. 题目要求 给定一个二维整型数组,返回其螺旋顺序列表,例如: 最后 ...

  6. Linux命令应用大词典-第45章 服务器配置

    45.1 ssh-agent:存储用于公钥验证的私钥 45.2 ssh-add:添加RSA或DSA身份的认证代理 45.3 ssh-keyscan:收集主机公钥 45.4 sshd:运行sshd守护进 ...

  7. Java初始化方法:类、容器

    Java初始化方法:类.容器   初始化类(非final): Struts2的DefaultActionMapper类中:      public DefaultActionMapper() {   ...

  8. [SHELL]结构化命令之条件语句

    1.if-then语句  #!/bin/bash username="root" if grep $username /etc/passwd then echo "the ...

  9. cronolog:日志分割工具

    一. 引言 因为tomcat的catalina.out日志无法按照日期自动创建,因此采用cronnlog分割. 二. 安装与配置 1.安装cronolog: yum install -y cronol ...

  10. [C++] Fucntions

    Statements A break statements terminate the nearest wile, do while, for or switch statement. A break ...