In Action(最短路+01背包)
2015-11-01 17:56
363 查看
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4764 Accepted Submission(s): 1569
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The minimal oil cost in this action.
If not exist print "impossible"(without quotes).
[align=left]Sample Input[/align]
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=110; const int MAXM=10010<<1; struct Node{ int u,v,w; }; Node dt[MAXM]; int edgnum,n; int dis[MAXN]; int bag[10010]; 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[0]=0; for(int j=0;j<=n;j++){ for(int i=0;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=0; 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,0); int V=0,sum=0; for(int i=1;i<=n;i++)scanf("%d",power+i),V+=power[i]; int flot=0; Bellman(); for(int i=1;i<=n;i++){ if(dis[i]==INF){ continue; } sum+=dis[i]; } for(int i=1;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=0;i<sum;i++)if(bag[i]>V/2){//不能是(v+1)/2。。。 flot=1; break; } if(flot)printf("%d\n",i); else puts("impossible"); } return 0; }
相关文章推荐
- Leetcode Algorithm No.268 Missing Number
- vs2010下Release版本调试代码设置
- ArrayQueue实现
- [leetcode 110]Balanced Binary Tree
- 常用的OpenCV函数速查
- 广义表算法库及其应用(1)——建立广义表算法库
- java中的302和sendRedirect的区别
- Spring学习笔记(二)
- Python使用urllib2 POST数组
- ubuntu,django-admin.py
- 今天你慕课了吗?
- 关于c中的%x及其它格式化符
- mysql创建修改删除表和库
- 检测出图片中的直线且标记出图片中的最长线段
- Java中的菜单
- 读‘Web前端开发工程师编程能力飞升之路’感触
- Http的使用及XML和JSON数据解析
- MVC怎么在当前视图中,传递参数给到另外一个视图?
- 链接样式表和导入样式区别
- UML第七集