hdoj 3339 In Action 【最短路 + 01背包】
来源:互联网 发布:人脸识别网络考勤系统 编辑:程序博客网 时间:2024/06/07 01:34
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4781 Accepted Submission(s): 1577
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.
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).
If not exist print "impossible"(without quotes).
Sample Input
22 30 2 92 1 31 0 2132 12 1 313
Sample Output
5impossible
思路:先求出0点到各点的最短路径,统计总电量sum,sum/2+1是摧毁所需的最小电量,然后是01背包解决。(背包容量===》最短路径的总和;物品===》每条最短路径;价值===》电量值)。
代码:
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3fusing namespace std;int map[110][110];int vis[110];int power[110];//电量值; int low[110];int dp[10010];int n,m;//n个发电站,m条路; void init(){for(int i=0;i<=n;i++){for(int j=0;j<=n;j++){if(i==j)map[i][j]=map[j][i]=0;elsemap[i][j]=map[j][i]=INF;}}}void dijkstra(int x){int next,i,j,min;memset(vis,0,sizeof(vis));for(i=0;i<=n;i++){low[i]=map[x][i];}vis[x]=1;for(i=0;i<n;i++){min=INF;next=0;for(j=0;j<=n;j++){if(min>low[j]&&!vis[j]){min=low[j];next=j;}}vis[next]=1;for(j=0;j<=n;j++){if(!vis[j]&&low[j]>low[next]+map[next][j]){low[j]=low[next]+map[next][j];}}}}int main(){int T;int i,j;int st,ed,dis; scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);init();for(i=0;i<m;i++){scanf("%d%d%d",&st,&ed,&dis);if(map[st][ed]>dis)//去重边; map[st][ed]=map[ed][st]=dis;}int sum=0;for(j=1;j<=n;j++){scanf("%d",&power[j]);sum+=power[j];}sum/=2;dijkstra(0);int v=0;for(i=1;i<=n;i++){if(low[i]!=INF){v+=low[i];//背包容量; }}memset(dp,0,sizeof(dp));for(i=1;i<=n;i++){if(low[i]!=INF){for(j=v;j>=low[i];j--){dp[j]=dp[j]>dp[j-low[i]]+power[i]?dp[j]:dp[j-low[i]]+power[i];}}}int f=1;for(i=0;i<=v;i++){if(dp[i]>sum){f=0;break;}}if(f) printf("impossible\n");else printf("%d\n",i);}return 0;}
0 0
- HDOJ 3339 In Action (最短路+01背包)
- hdoj 3339 In Action【最短路+01背包】
- hdoj 3339 In Action 【最短路 + 01背包】
- hdoj--3339--In Action(最短路+01背包)
- HDOJ 3339 In Action(最短路+01背包)
- In Action(最短路+01背包)
- hdu3339 In Action 最短路+01背包
- hdu In Action-最短路+01背包
- hdu 3339 In Action spfa最短路+01背包
- hdu 3339 In Action(最短路+01背包)
- HDU-3339 In Action 最短路+01背包
- hdu 3339 In Action 最短路+01背包
- Hdu 3339 In Action[最短路+01背包]
- hdu 3339 In Action(最短路+01背包)
- HDU 3339 In Action 最短路+01背包
- HDU 3339 In Action 最短路+01背包
- hdu 3339 In Action(最短路spfa+01背包)
- HDU - 3339 In Action (最短路Dijkstra+01背包)
- iOS 获取网络状态 判断3G 、4G类型
- MySQL 5.6.26免安装版配置
- 使用shell脚本删除当前目录下.c文件对应的可执行文件
- ip欺骗与盗用原理
- Vs2010 中的 编译错误CS0433
- hdoj 3339 In Action 【最短路 + 01背包】
- BuildConfig 中 DEBUG的作用
- Github添加图片说明
- 黑马程序员——JAVA基础----语法(三)-- 方法和数组
- Hello World
- iOS GCD浅谈
- pdf与其他格式文件在线互相转换
- 今天在给同事改程序的时候,发现了java中Runtime
- .net dataset添加列,循环重新设置数据