hdu 4281(MTSP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4281
题意:给出N个点,第一个点是裁判,其他N-1个点需要裁判过去回答问题,每个点需要的时间不一样,而每个裁判最多能回答M分钟的问题。题目分两问,第一问是如何分配可以使使用的裁判数最少,第二问是如何分配裁判,使裁判走过的总路程和最少,裁判一开始都在1,最终也要回到1。
思路:首先我们可以把符合条件的集合预处理出来,然后对于第一问,可以用状态压缩解决,dp[state]表示该状态下的最少裁判数,对于第二问,就是多旅行商问题了,dp[i][j]表示状态为i,在位置j的最小花费,然后开一个best数组来保存最有状态,最后枚举子集,DP合并环即可。比如1~3三个点,裁判在1,那答案就是Min(1->2->3->1,1->2->1 + 1->3->1)。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define inf 1<<30 struct Point{
int x,y;
}point[]; int n,m,ans,val[],dp1[<<];
int dp[<<][],best[<<];
int Ok[<<];
int dist[][]; int Get_Dist(int i,int j)
{
return ceil(sqrt(double(point[i].x-point[j].x)*(point[i].x-point[j].x)+double(point[i].y-point[j].y)*(point[i].y-point[j].y)));
} int Judge(int state)
{
int sum=;
for(int i=;i<n;i++){
if(state&(<<i))sum+=val[i];
}
return sum<=m;
} int Solve()
{
fill(dp1,dp1+(<<n),inf);
dp1[]=;
for(int state=;state<(<<n);state++){
if(Ok[state]){
for(int i=;i<(<<n);i++){
if((state&i)==&&dp1[i]!=inf){
dp1[state|i]=min(dp1[state|i],dp1[i]+);
}
}
}
}
return dp1[(<<n)-];
} int TSP()
{
fill(best,best+(<<n),inf);
for(int i=;i<(<<n);i++)
for(int j=;j<n;j++)dp[i][j]=inf;
dp[][]=;
for(int state=;state<(<<n);state++){
if(Ok[state]){
for(int i=;i<n;i++)if(state&(<<i)){
if(dp[state][i]==inf)continue;
best[state]=min(best[state],dp[state][i]+dist[i][]);
for(int j=;j<n;j++)if(!(state&(<<j))){
dp[state|(<<j)][j]=min(dp[state|(<<j)][j],dp[state][i]+dist[i][j]);
}
}
}
}
for(int state=;state<(<<n);state++){
if(state&){
for(int substate=state&(state-);substate;substate=state&(substate-)){
best[state]=min(best[state],best[substate]+best[(state^substate)|]);
}
}
}
return best[(<<n)-];
} int main()
{
while(~scanf("%d%d",&n,&m)){
for(int i=;i<n;i++)scanf("%d%d",&point[i].x,&point[i].y);
for(int i=;i<n;i++)scanf("%d",&val[i]);
for(int i=;i<n;i++)
for(int j=;j<n;j++)dist[i][j]=Get_Dist(i,j);
for(int s=;s<(<<n);s++){
Ok[s]=Judge(s);
}
ans=Solve();
if(ans==inf){
puts("-1 -1");
continue;
}
printf("%d %d\n",ans,TSP());
}
return ;
}
hdu 4281(MTSP)的更多相关文章
- HDU 4281 (状态压缩+背包+MTSP)
Judges' response Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4281 Judges' response 状压dp+多旅行商问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4281 Judges' response Time Limit: 2000/1000 MS (Java ...
- hdu 4281 Judges' response(多旅行商&DP)
Judges' response Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- DP专题·三(01背包+完全背包)
1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostr ...
- [GodLove]Wine93 Tarining Round #8
比赛链接: http://vjudge.net/contest/view.action?cid=47644#overview 比赛来源: 2012 ACM/ICPC Asia Regional Tia ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
- 【DP专辑】ACM动态规划总结
转载请注明出处,谢谢. http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 ...
- dp专题训练
****************************************************************************************** 动态规划 专题训练 ...
- 【DP专辑】ACM动态规划总结(转)
http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强, ...
随机推荐
- Python之调用WebService:suds
suds官方: https://fedorahosted.org/suds/wiki/Documentation 互联网公开WSDL: http://www.webxml.com.cn/zh_cn/w ...
- jdbc事务处理和连接池
JDBC: * JDBC概念:Java DataBase Connectivity(Java数据库连接) SUN公司提供的一组连接数据库API. * JDBC开发步骤: * 1.注册驱动. * 2.获 ...
- How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensions”?
How can I fix “Compilation unit name must end with .java, or one of the registered Java-like extensi ...
- 【数据结构】book3_3 表达式求值
#include<iostream> #include <stdlib.h> using namespace std; typedef int Status; ; ; ; ; ...
- jquery-validation-1.13.1 自定义验证正则
/*** check Mobile***********************/ jQuery.validator.addMethod("isMobile", function( ...
- osg osgDB::Options noTexturesInIVEFile ForceReadingImage dds_flip
osgDB::writeNodeFile(node, path, new osgDB::Options("noTexturesInIVEFile")); noTexturesInI ...
- Centos7 ZooKeeper 安装过程
www.apache.org/dist/上可以下载Hadoop整个生态环境的组件,我下的Zookeeper3.4.6版本 我一般都是在一个虚拟机上将一.二步都做完,然后克隆出来,再到克隆出来的虚拟机上 ...
- stm32——NFC芯片--PN532的使用
stm32——NFC芯片--PN532的使用 一.NFC简介 NFC(Near Field Communication)近场通信,是一种短距高频的无线电技术,在13.56MHz频率运行于20厘米距离内 ...
- 开启后台 Service 闪退
04-29 15:36:23.395: E/ActivityThread(15275): Performing stop of activity that is not resumed: {com.e ...
- jpg Test