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. Convert Sorted List to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...

  2. Struts2拦截器总结

    拦截器的本质: 拦截器就是一个类,一个实现了超级接口Interceptor的类.Interceptor接口里定义了三个方法 init(),destory(),intercept().其中inercep ...

  3. bzoj 1303: [CQOI2009]中位数图

    题目链接 给n个数,一个值b, 统计所有以b为中位数的序列的个数.序列长度为奇数.数字在1-n之间, 每个数只出现一次. 如果一个数大于b, 那么将他赋值为1, 小于b赋值为-1, 记录数组中b出现的 ...

  4. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...

  5. 树的判断(poj nyoj hduoj)

    题目: http://ac.jobdu.com/problem.php?pid=1481 http://acm.nyist.net/JudgeOnline/problem.php?pid=129 ht ...

  6. NET Core MVC 在linux上的创建及发布

    NET Core MVC 在linux上的创建及发布 前言 ASP.NET core转眼都发布半月多了,社区最近也是非常活跃,虽然最近从事python工作,但也一直对.NET念念不忘,看过了园区大神们 ...

  7. 打造自己博客(wordpress)的wap手机版本

    这儿介绍我试用的两款插件:wordpress-mobile-edition和wp-t-wap.1.先说一下两者的区别. wordpress-mobile-edition插件使用后,可以用手机直接访问你 ...

  8. Android Layout Binder(在线将XML中View find出来,生成java代码的工具)

    废话不多说,这是地址:http://android.lineten.net/layout.php. 有图有真相,比如: 你的XML假如是这样: <?xml version="1.0&q ...

  9. mybatis-generator生成model和dao层代码

    .建立文件夹myibatisGen 2.下载mybatis-generator-core-1.3.1.jar或者其它版本的jar包,到myibatisGen文件夹下 3.为生成代码建立配置文件“gen ...

  10. retina屏实现border边框1px

    .border { position: relative; width: 300px; height: 200px; } .border:after { border: 1px solid #ff33 ...