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. 9、Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

  2. APP九宫格滑动解锁的处理

    写手机自动化测试脚本关于APP九宫格滑动解锁方面采用了appium API 之 TouchAction 操作. 先是用uiautomatorviewer.bat查询APP元素坐标: 手工计算九宫格每个 ...

  3. 2>&1和&>的区别

    简单记录下: COMMAND > /path/file 2>&1 COMMAND &> /path/file 这两个效果都是一样的,都是把正确的输入.错误输入存放到同 ...

  4. [C++]STL中的容器

    C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...

  5. BZOJ 4176 Lucas的数论 莫比乌斯反演+杜教筛

    题意概述:求,n<=10^9,其中d(n)表示n的约数个数. 分析: 首先想要快速计算上面的柿子就要先把d(ij)表示出来,有个神奇的结论: 证明:当且仅当a,b没有相同的质因数的时候我们统计其 ...

  6. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  7. 一次大量TIME_WAIT和Recv-Q 堵塞问题排查思路

    记录一下周末出现问题~     仅自己摘记不做任何参考. 第一天故障: 现象: 公司销售群和售后群炸了,说老后台(1.0版本)崩溃了,因为还有部门的业务没来得及迁移到新后台,我当时正在打农药哈哈~ 后 ...

  8. Red and Black(DFS深搜实现)

    Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...

  9. 2019寒假训练营寒假作业(三) MOOC的网络空间安全概论笔记部分

    目录 第五章 网络攻防技术 5.1:网络信息收集技术--网络踩点 信息收集的必要性及内容 网络信息收集技术 网络踩点(Footprinting) 网络踩点常用手段 5.2:网络信息收集技术 --网络扫 ...

  10. unity 学习记录

    世界第九条约定 缘起 嗯,其实一开始我知道unity是个弄游戏的,也知道好像神庙逃亡,炉石都是出自unity,然后舍友都报了,我也觉得这个东西挺高大上的,所以忍不住自己的双手,报了名,确实,这能学到很 ...