In Action

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

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
 
题解:01背包+最短路;
代码:
 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=<<;
struct Node{
int u,v,w;
};
Node dt[MAXM];
int edgnum,n;
int dis[MAXN];
int bag[];
int power[MAXN];
int add(int u,int v,int w){
dt[edgnum].u=u;dt[edgnum].v=v;dt[edgnum++].w=w;
}
void Bellman(){
mem(dis,INF);dis[]=;
for(int j=;j<=n;j++){
for(int i=;i<edgnum;i++){
int u=dt[i].u,v=dt[i].v,w=dt[i].w;
dis[v]=min(dis[v],dis[u]+w);
}
}
}
int main(){
int m,T;
scanf("%d",&T);
while(T--){
edgnum=;
scanf("%d%d",&n,&m);
int u,v,w;
while(m--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);
}
mem(bag,);
int V=,sum=;
for(int i=;i<=n;i++)scanf("%d",power+i),V+=power[i];
int flot=;
Bellman();
for(int i=;i<=n;i++){
if(dis[i]==INF){
continue;
}
sum+=dis[i];
}
for(int i=;i<=n;i++){
if(dis[i]!=INF)
for(int j=sum;j>=dis[i];j--){
bag[j]=max(bag[j],bag[j-dis[i]]+power[i]);
}
}
int i;
for(i=;i<sum;i++)if(bag[i]>V/){//不能是(v+1)/2。。。
flot=;
break;
}
if(flot)printf("%d\n",i);
else puts("impossible");
}
return ;
}

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) ...

  2. hdu3339In Action(最短路+01背包)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=259#problem/H Description Since 1945, whe ...

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

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

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

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

  5. HDU-3339 IN ACTION(Dijkstra +01背包)

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

  6. *HDU3339 最短路+01背包

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

  7. HDU 3339 最短路+01背包

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

  8. hdoj--3339--In Action(最短路+01背包)

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

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

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

随机推荐

  1. leetcode Contains Duplicate II python

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  2. VirtualBox扩展磁盘空间

    进入VB的安装目录, 输入命令 VBoxManage list hdds获得当前所有虚拟机的uuid 选择需要扩展的磁盘, 输入 VBoxManage modifyhd uuid –resize 81 ...

  3. The following classes could not be found: - android.support.v7.internal.widget.ActionBarOverlayLayou解决方案

    如图出现如下的错误的时候,一般都是升级Androdi Studio 后导致的,引入库不全,或者其他 东西缺少,可以如下解决方案: 或者如下的解决方案: 在布局文件的Design界面中,修改原来的App ...

  4. 一个用C++写的Json解析与处理库

    什么是Json?这个库能做什么? JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is e ...

  5. passwordauthentication yes

    ssh ip disconnected:no supported authentication methods available(server sent:publickey,gssapi-keyex ...

  6. 【踩坑】近来在Firefox上遇到的一些坑

    因为工作一年多以来,做的工作基本都是和webkit系列打交道. 先是做m站,后来做了两个app内嵌的hybrid项目,从来只考虑webkit前缀和相关的伪类. 最近四个多月开始做内部的管理系统,写写样 ...

  7. Jquery开发插件的方法

    Jquery未开发插件提供了两个方法: (1)Jquery.extend(object)    -为Jquery类本身添加新的方法;代码如下: $.extend({ add:function(a,b) ...

  8. SDK无法更新

    http://jingyan.baidu.com/article/da1091fbd232fe027949d653.html

  9. eclipse自动生成的appcompat_v7出错

    用eclipse新建Android工程时,自动生成的appcompat_v7出错,有个红色交叉,而且新建的Android工程有一个红色感叹号. 这时你去看看你新建的Android工程是不是没有生成R文 ...

  10. asp.net中实现MD5加密、解密的方法

    这个MD5加密.解密的方法会使用即可. 使用时的代码备忘:Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile(& ...