In Action

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=3339

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

HINT

题意

给你一个图,每个图都有权值,然后让你派出多辆坦克,破环至少占权值一半的城市,派出坦克的代价就是从0点到那些城市的距离

求最少代价

题解:

点数很少,注意有重边

直接跑一发flyod,然后再来个背包dp

然后就好了~

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline void P(int x)
{Num=0;if(!x){putchar('0');puts("");return;}while(x>0)CH[++Num]=x%10,x/=10;while(Num)putchar(CH[Num--]+48);puts("");
}
//**************************************************************************************int dis[110][110];
int dis1[110];
int power[110];
int dp[maxn];
int main()
{//test;int t=read();for(int cas=1;cas<=t;cas++){int n=read(),m=read();for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)dis[i][j]=inf;for(int i=0;i<m;i++){int a=read(),b=read(),c=read();dis[a][b]=dis[b][a]=min(dis[a][b],c);}int sum,aim;sum=aim=0;for(int i=1;i<=n;i++){power[i]=read();aim+=power[i];}for(int k=0;k<=n;k++){for(int i=0;i<=n;i++){for(int j=0;j<=n;j++){dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);}}}for(int i=1;i<=n;i++){dis1[i]=dis[0][i];if(dis1[i]<inf)sum+=dis1[i];}memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++)for(int j=sum;j>=dis1[i];j--)dp[j]=max(dp[j],dp[j-dis1[i]]+power[i]);int ans=inf;for(int i=0;i<=sum;i++){if(dp[i]>=(aim/2+1)&&dp[i]<ans){ans=i;break;}}if(ans>=inf)printf("impossible\n");elseprintf("%d\n",ans);}
}

转载于:https://www.cnblogs.com/qscqesze/p/4564391.html

hdu 3339 In Action 背包+flyod相关推荐

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

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

  2. hdu 1171 dp(多重背包)

    View Code //hdu 1171 dp(多重背包)//题意:把所有物品的价值尽量分为相等的两份,不能等分的话 //后面那份可以稍小于前面的 //求出价值总和后,令价值的一半为背包容量,让背包尽 ...

  3. hdu 3449 Consumer 01背包

    http://acm.hdu.edu.cn/showproblem.php?pid=3449 这个题AC的有点稀里糊涂(是1A过的),采用的01背包的方法: 思路:定义了两个数组用来存储最终结果和但购 ...

  4. HDU 2639(01背包求第K大值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...

  5. HDU 2844 Coins 多重背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2844 Coins Time Limit: 2000/1000 MS (Java/Others)Mem ...

  6. hdu 5234 Happy birthday 背包 dp

    Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  7. HDU - 3466(01背包理解)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:给你一些钱 m ,然后在这个国家买东西, 共有 n 件物品,每件物品价格P价值V还有一个很 ...

  8. hdu 5501(贪心+01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5501 现在有A1,B1,C1和A2,B2,C2这两道题,如果先做1再做2的得分是A1-B1*C1+A2 ...

  9. hdu 3033(分组背包)

    题意:有S款运动鞋,一个n件,总钱数为m,求不超过总钱数且每款鞋子至少买一双的情况下,使价值最大.如果 有一款买不到,就输出"Impossible". 解题思路:分组背包,和背包九 ...

最新文章

  1. [Python学习] 专题五.列表基础知识 二维list排序、获取下标和处理txt文本实例
  2. 全分布式集群搭建总结
  3. 国外少儿PYTHON编程书推荐
  4. 基于Maven的spring_security入门
  5. Android中视图重绘的方法
  6. Linux内存管理:ARM64体系结构与编程之cache(2):cache一致性
  7. mysql 外键约束_MySQL之外键约束(FOREIGN KEY)
  8. vs 2005 多語言
  9. Azure PowerShell (13) 批量设置Azure ARM Network Security Group (NSG)
  10. Raki的读paper小记:Model Zoo: A Growing “Brain” That Learns Continually
  11. rpcbind服务没法开启问题
  12. R 计算时间序列自相关性教程
  13. 数据库课程设计:医院信息管理系统(pycharm+MySQL)
  14. jpg在线转换pdf
  15. Android 校正系统时间的三种解决方案
  16. 微信id修改服务器繁忙,微信终于可以修改ID了! 但,你可能不行......
  17. RHCA回忆录---RH236介绍
  18. 如何设置计算机自动连接宽带,宽带连接怎么设置,怎么设置宽带自动连接
  19. Linux-打包、压缩命令
  20. 【bzoj1502】 NOI2005—月下柠檬树

热门文章

  1. Web前端开发面试题---HTML+CSS
  2. java应用程序中判断用户输入的一个整数是否在已知数组里。
  3. Android音频底层调试-基于tinyalsa
  4. 改变UITableView选中行高亮的颜色
  5. 解决VS2010链接错误:LINK fatal error LNK1123: 转换到 COFF
  6. MikroTik RouterOS获取在线终端和在线IP总数并自动对IP做限速(转)
  7. 全方面了解和学习PHP框架
  8. 也谈大公司病1——正确是最大的错误
  9. web.config 中SessionState的配置 [转]
  10. 【性能优化】 之 并行执行