In Action(最短路+01背包)

时间:2021-11-16 09:38:12

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
In Action(最短路+01背包)
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 最短路&plus;01背包

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

  2. hdu3339In Action&lpar;最短路&plus;01背包)

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

  3. HDU 3339 In Action【最短路&plus;01背包】

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

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

     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 &plus;01背包)

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

  6. &ast;HDU3339 最短路&plus;01背包

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

  7. HDU 3339 最短路&plus;01背包

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

  8. hdoj--3339--In Action(最短路&plus;01背包)

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

  9. hdu 3339 In Action &lpar;最短路径&plus;01背包&rpar;

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

随机推荐

  1. javaweb学习总结&lpar;十&rpar;——HttpServletRequest对象&lpar;一&rpar;

    一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...

  2. wordpress内存不足问题&OpenCurlyDoubleQuote;Fatal error&colon;out of memoryin etc&period;&period;&period;”

    2016年3月22日wordpress用户名及密码登录,出现: “ Fatal error: Out of memory (allocated 19136512) (tried to allocate ...

  3. 问题:-&lbrack;UIViewController &lowbar;loadViewFromNibNamed&colon;bundle&colon;&rsqb; loaded the &quot&semi;BlueView&quot&semi; nib but the view outlet was not set&period;

    问题:-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "BlueView" nib but the vie ...

  4. css制作导航栏的上下三角

    1)先完成一个导航条 <style type="text/css"> .nav-ul{ list-style: none; } .nav-ul li{ width: 1 ...

  5. AS3的数据类型和定义

    AS3的数据类型分: 基元数据类型:Boolean  int(整数)  Number(长的浮点数)  unit(很大的正整数)  String 复杂数据类型:Arrary  Date  Error   ...

  6. &lbrack;VC&plus;&plus;&rsqb;用CTime类得到当前日期、时间、星期,格式化&lpar;详细讲解&rpar;

    用CTime类得到当前日期.时间.星期,格式化(详细讲解)2009/05/12 09:48 A.M.① 定义一个CTime类对象 CTime time; ② 得到当前时间 time = CTime:: ...

  7. Block Demo

    1.预定义Block typedef  void(^myblock1)(int a,int b); 2.将Block做为类的属性 @property(nonatomic,strong) myblock ...

  8. &period;net core 依赖注入扩展,实现随处控制反转

    在使用.net core时,依赖注入,主要使用通过构造函数注入.小编将通过扩展方式,实现在类中各个地方可以控制反转,获取实例. 1.首先自定义扩展类 using Microsoft.AspNetCor ...

  9. C&num;多线程编程序--聊聊线程

    这篇文章主要给您讲解几个基本问题,什么是线程?为什么要用线程?线程万能?这几个问题.我这篇博客是在该系列另外几篇博客之后写的,之所以不往下继续写,而是回到最初的问题,是因为我感觉到我没有很好的讲解开头 ...

  10. VB&period;NET版机房收费系统---报表

    报表,即报告情况的表格,简单的说:报表就是用表格.图表等格式来动态显示数据,可以用公式表示为:"报表 = 多样的格式 + 动态的数据". 在没有计算机以前,人们利用纸和笔来记录数据 ...