hdoj 3339 In Action【最短路+01背包】
2015-11-02 17:52
288 查看
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4777 Accepted Submission(s): 1575
[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
[align=left]Sample Output[/align]
5 impossible
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int inf = 0x3f3f3f; const int mx = 123; int vis[mx], dis[mx], map[mx][mx], p[mx]; int dp[mx*2000]; int n,m; void dij(int st) { memset(vis, 0, sizeof(vis)); int i,j,k; for(i = 0; i <= n; i++) dis[i] = map[st][i]; for(k = 0; k <= n; k++) { int minn = inf; for(i = 0; i<=n; i++) { if(dis[i] < minn&& !vis[i]) { minn = dis[i]; j = i; } } vis[j] = 1; for(i = 0; i <= n; i++) { if(!vis[i] && dis[i] > dis[j]+map[j][i]) dis[i] = dis[j]+map[j][i]; } } } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n, &m); int i,j,k; for(i = 0; i <= n; i++) { for( j = 0; j <= n; j++) map[i][j] = inf; map[i][i] = 0; } int a, b, c; while(m--) { scanf("%d%d%d", &a, &b, &c); if(map[a][b] > c) map[a][b] = map[b][a] = c; } int sum = 0; for(i = 1; i<=n; i++) { scanf("%d" , &p[i]); sum += p[i]; } dij(0); for(i = 1; i <= sum; i++) dp[i] = inf; dp[0] = 0; for(i = 1; i <= n; i++) { for(j = sum; j >= p[i]; j--) { dp[j] = min(dp[j], dp[j-p[i]]+dis[i]); } } int minn = inf; int ave = sum/2+1;//一定要比一半多一! for(i = ave; i <= sum; i++) { if(minn > dp[i]) minn = dp[i]; } if(minn == inf) puts("impossible"); else printf("%d\n",minn); } return 0; }
相关文章推荐
- Navicat远程无法连接mysql问题解决(附MAC上NavicatPremium11.1.8破解中文版下载链接)
- openstack i版常用命令
- ios 系统NavigationBar隐藏
- 关于HTTP请求体格式
- JAVA中HashMap和HashTable的区别
- RabbitMQ的使用
- 查找网站后台N种方法总结
- 我之见解字符串
- url中传递中文参数出现乱码的问题
- 黑马程序员——OC基础知识
- Quartz 任务调度(二)
- android四大组件之-Broadcast的使用、Content Provider的使用
- java(基础)学习第一步:安装jdk,配置环境变量
- Android 开发笔记
- android单向无限循环的轮播图效果。
- [Linux] - Docker制作nginx+php
- Apache Kafka源码分析 - PartitionStateMachine
- 修改归并排序实现逆序对计数
- maven 插件生成mybatis相关代码
- springMVC+MyBatis搭建心得