hdoj--3339--In Action(最短路+01背包)
2016-01-30 15:05
429 查看
In Action
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4859 Accepted Submission(s): 1600
[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
[align=left]Author[/align]
Lost@HDU
[align=left]Source[/align]
HDOJ Monthly Contest – 2010.03.06
[align=left]Recommend[/align]
lcy | We have carefully selected several similar problems for you: 1217 1385 1548 3338 1546
n个电站,m条路,点0处出发,每一个电站都有一定的发电量p [ i ],也给出了m条路,a--b需要c的油,如果成功的控制一半以上的电量就视为胜利,现在求最少的耗油量,先用迪杰斯特拉处理路径,背包中存i升的油最多控制j的电量(dp[ i ] = j)然后找出最小的dp[ i ] > sum/2,输出i
ac代码
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f #define MAXN 110 int map[MAXN][MAXN],vis[MAXN],dis[MAXN],dp[11000],p[MAXN]; int n,m; void disjtra() { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); for(int i=0;i<=n;i++) dis[i]=map[0][i]; vis[0]=1; for(int i=1;i<=n;i++) { int minn=INF; int flag=0; for(int j=0;j<=n;j++) { if(!vis[j]&&minn>dis[j]) { minn=dis[j]; flag=j; } } vis[flag]=1; for(int j=0;j<=n;j++) { if(!vis[j]&&dis[flag]+map[flag][j]<dis[j]) { dis[j]=dis[flag]+map[flag][j]; } } } } int main() { int t,i,j; int a,b,c; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(map,INF,sizeof(map)); while(m--) { scanf("%d%d%d",&a,&b,&c); if(c<map[a][b]) map[a][b]=map[b][a]=c; } int sum=0; for(i=1;i<=n;i++) { scanf("%d",&p[i]); sum+=p[i]; } sum/=2; disjtra(); int v=0; for(i=1;i<=n;i++) { if(dis[i]!=INF) v+=dis[i]; } memset(dp,0,sizeof(dp)); for(i=1;i<=n;i++) { if(dis[i]!=INF) { for(j=v;j>=dis[i];j--) dp[j]=max(dp[j],dp[j-dis[i]]+p[i]); } } int flag=1; for(i=0;i<=v;i++) { if(dp[i]>sum) { flag=0; break; } } if(flag) printf("impossible\n"); else printf("%d\n",i); } return 0; }
相关文章推荐
- Android 第五天 (下午)
- Java中的Clone接口
- 如何在文件夹空白处打开当前目录的 CMD
- 005.nginx配置文件
- 下面是性能测试的主要概念和计算公式
- 封装轮播图
- 在adb shell中直接使用sqlite3命令操作数据库
- Web性能测试工具推荐
- 完蛋了,微软连个手机操作系统都不会写了,是退化了还是本身就不行
- 内核链表分析
- BZOJ 2783 JLOI 2012 树 搜索
- 通过Xcode7代码实时预览功能快速开发自定义控件(二)
- EL表达式/JSTL复习总结
- JPA Repository数据访问层
- xib今天遇到的各种问题
- springmvc整合dubbo分布式系统的搭建demo
- 安装WinCE6.0开发包遇到simplefileca(error)错误解决办法
- 初识 Angular 体会
- Apple Catching(POJ 2385)
- XenServer体系架构解析