最短路+01背包

In Action

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3857    Accepted Submission(s): 1229

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
 

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int INF=0x3f3f3f3f; int n,m;
int dist[110],vis[110],power[110];
int g[110][110];
int dp[11000]; void dijkstra()
{
memset(dist,63,sizeof(dist));
memset(vis,0,sizeof(vis)); dist[0]=0; for(int j=0;j<=n;j++)
{
int mark=-1,mindist=INF;
for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
if(mindist>dist[i])
{
mindist=dist[i]; mark=i;
}
} if(mark==-1) break;
vis[mark]=1; for(int i=0;i<=n;i++)
{
if(vis[i]) continue;
dist[i]=min(dist[i],dist[mark]+g[mark][i]);
}
}
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
memset(g,63,sizeof(g)); scanf("%d%d",&n,&m); for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]=g[b][a]=min(g[a][b],c);
} dijkstra(); int sumd=0,sum=0; for(int i=1;i<=n;i++)
{
scanf("%d",power+i);
if(dist[i]!=INF) sumd+=dist[i];
sum+=power[i];
} memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)
{
if(dist[i]==INF) continue;
for(int j=sumd;j>=dist[i];j--)
{
dp[j]=max(dp[j],dp[j-dist[i]]+power[i]);
}
}
int ans=-1,low=0,high=sumd,mid;
while(low<=high)
{
mid=(low+high)/2;
if(dp[mid]*2>sum)
ans=mid,high=mid-1;
else low=mid+1;
} if(ans==-1)
puts("impossible");
else printf("%d\n",ans);
}
return 0;
}

HDOJ 3339 In Action的更多相关文章

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

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

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

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

  3. hdu 3339 In Action 背包+flyod

    In Action Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=333 ...

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

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

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

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

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

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

  7. hdu 3339 In Action

    http://acm.hdu.edu.cn/showproblem.php?pid=3339 这道题就是dijkstra+01背包,先求一遍最短路,再用01背包求. #include <cstd ...

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

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

  9. HDU 3339 In Action(最短路+背包)题解

    思路:最短路求出到每个点的最小代价,然后01背包,求出某一代价所能拿到的最大价值,然后搜索最后结果. 代码: #include<cstdio> #include<set> #i ...

随机推荐

  1. Spring Boot 版本支持对应JDK

    转自:http://www.cnblogs.com/oumi/p/9241424.html 一.Spring Boot 版本支持 Spring Boot Spring Framework Java M ...

  2. BZOJ 3060 Kruskal

    思路: 把from&to都>k的直接加边 剩下的如果是一棵树就加. 否则ans++ (我的代码写的是反着的 不过意思都一样) //By SiriusRen #include <cs ...

  3. First step in troubleshooting complex issues: Define and scope your issue properly

    最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...

  4. seo在前端网页制作的应用

    学习了慕客网上的“SEO在网页制作中的应用‘’,下面来进行小小的学习总结,顺便梳理下知识.所谓学而不思则罔思而不学则殆.下面开始正文. 一.搜索引擎的工作原理 搜索引擎的基本工作原理包括如下三个过程: ...

  5. 关于用户禁用Cookie的解决办法和Session的图片验证码应用

    当用户通过客户端浏览页面初始化了Session之后(如:添加购物车,用户登陆等),服务器会将这些session数据保存在:Windows保存在C:\WINDOWS\Temp的目录下,Linux则是保存 ...

  6. 【Oracle】回收站

    ☆回收站概念 oracle从10g开始,引入回收站(Recycle Bin)概念.回收站的全称叫:Tablespace Recycle Bin.回收站是一个逻辑区域,oracle并没有为它分配物理空间 ...

  7. apache出现You don't have permission to access / on this server提示的解决方法

    在apache的配置文件httpd.conf里定义了对网站根默认的访问权限 #<Directory />    Options FollowSymLinks    AllowOverrid ...

  8. python第三方模块大杂烩

    Python单元测试框架之pytest---如何执行测试用例 unittest单元测试框架实现参数化 (用例有相似参数断言时使用,可以精简代码) python中标示符作用详解 一篇文章让你彻底搞清楚P ...

  9. opengl渲染时画面抖动

    渲一个大尺寸模型的时候模型的细节部分一直在闪烁.尝试: 1. 纹理用mipmap,失败. 2. 开启msaa,失败. 3. 相机近时不闪,越远闪的越厉害,怀疑是深度争夺,就把远裁剪平面调大,失败. - ...

  10. (转)基于MVC4+EasyUI的Web开发框架经验总结(1)-利用jQuery Tags Input 插件显示选择记录

    http://www.cnblogs.com/wuhuacong/p/3667703.html 最近花了不少时间在重构和进一步提炼我的Web开发框架上,力求在用户体验和界面设计方面,和Winform开 ...