题目链接:

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

Judges' response

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#### 问题描述
> The contest is running and the judges is busy watching the progress of the contest. Suddenly, N - 1 (N   You are asked to solve two problems:
>   1. At least how many judges should be sent so that they can serve all the contestants? (Because the judges have limited patience, each one of them cannot serve too many contestants.)
>   2. If there are infinite number of judges, how to assign the route for each judge so that the sum of their walking time is minimized? Each contestant i is reside in place (xi, yi), the judges are in place (x1, y1). Assuming the walking speed of the judge is 1.

输入

There are several test cases, Each case begin with two integer N, M(with the meaning in the above context, 2 <= N <= 16, 0 <= M <= 100000).

  Then N lines follow and line i will contain two numbers x, y(0 <= x, y <= 1000), indicating the coordinate of place i.

  Then another N lines follow and line i will contain numbers Ci(0 <= Ci <= 1000), indicating the time to solve contestant i's question. C1 will 0 as place 1 is for the judges.

  

  The distance between place i and place j is defined as ceil(sqrt((xi - xj) ^ 2 + (yi - yj) ^ 2)). (ceil means rounding the number up, e.g. ceil(4.1) = 5)

输出

  For each case, output two numbers. The first is the minimum number of judges for question 1. The second is the minimum sum of walking time for question 2.

  If it's impossible to serve all the contestants, please output -1 -1 instead.

样例输入

3 3

0 0

0 3

0 1

0

1

2

3 2

0 0

0 3

0 1

0

1

2

3 1

0 0

0 3

0 1

0

1

2

  

16 35

30 40

37 52

49 49

52 64

31 62

52 33

42 41

52 41

57 58

62 42

42 57

27 68

43 67

58 48

58 27

37 69

0

19

30

16

23

11

31

15

28

8

8

7

14

6

19

11

样例输出

1 6

2 8

-1 -1

8 467

题意

回答每个学生的问题需要ai的时间,每个助教的忍耐时间是m,一个助教接受的提问时间一定不能超过m,第一个问题是问最少需要多少个助教才能解决所有的学生的问题。第二个问题是说,给你助教的位置(第一个),和学生的位置,你现在有无限多的助教,这些助教解决完问题,最后要回到原先的点。问如何分配能使回答完所有的学生的问题之后所有的助教所走的路程总和最小。

题解

这题数据很小,可以考虑状压。

先把所有的总时间不超过m的学生的集合找出来,然后dp[i]表示接受状态为i的学生的问题需要最少的助教,j表示某个总和不超过m的子集,则有转移方程:dp[i]=min(dp[i],dp[i^j]+1)。

对于第二个问,我们也可以用第一个问题的方法来做dp2[i][0]表示走了状态为i的点,最后回到原来位置的最少路程,则,用这些子集(上个问就已经处理出来的那些集合),就能拼出走完所有的点所走的最短路程。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=10000000000000000LL;
const double eps=1e-9; const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=17; PII pt[maxn];
int arr[maxn];
int n,m;
VI vec;
int dp[1<<maxn];
int dp2[1<<maxn][maxn]; int dis(int i,int j) {
int x=pt[i].X-pt[j].X;
int y=pt[i].Y-pt[j].Y;
return ceil(sqrt(x*x+y*y));
} void tsp(int dp[][maxn]) {
rep(i,0,(1<<maxn)) rep(j,0,maxn) dp[i][j]=INF; dp[1][0]=0;
rep(i,2,(1<<n)) {
rep(j,0,n) {
if(!(i&(1<<j))) continue;
rep(k,0,n) {
if(k==j||(i&(1<<k))==0) continue;
dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+dis(k,j));
}
}
}
rep(t,0,vec.sz()) {
int i=vec[t];
if((i&1)==0) continue;
rep(j,1,n) {
dp[i][0]=min(dp[i][0],dp[i][j]+dis(0,j));
}
}
} void init() {
vec.clear();
} int main() {
while(scf("%d%d",&n,&m)==2&&n) { init(); rep(i,0,n) scf("%d%d",&pt[i].X,&pt[i].Y);
rep(i,0,n) scf("%d",&arr[i]); rep(i,1,(1<<n)) {
int sum=0;
rep(j,0,n) {
if(!(i&(1<<j))) continue;
sum+=arr[j];
}
if(sum<=m) vec.pb(i);
} rep(i,0,(1<<n)) dp[i]=INF;
dp[0]=0;
rep(i,0,(1<<n)) {
rep(j,0,vec.sz()) {
if((i|vec[j])!=i) continue;
dp[i]=min(dp[i],dp[i^vec[j]]+1);
}
} int ans1=dp[(1<<n)-1]; if(ans1>=INF){
prf("-1 -1\n"); continue;
} tsp(dp2); rep(i,0,(1<<n)) dp[i]=INF;
dp[0]=0;
rep(i,0,(1<<n)) {
rep(j,0,vec.sz()) {
if((i|vec[j])!=i) continue;
dp[i]=min(dp[i],dp[i^vec[j]]+dp2[1|vec[j]][0]);
}
} int ans2=dp[(1<<n)-1]; prf("%d %d\n",ans1,ans2); }
return 0;
} //end-----------------------------------------------------------------------

HDU 4281 Judges' response 状压dp+多旅行商问题的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  4. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  5. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  6. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  7. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  8. HDU 3362 Fix (状压DP)

    题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的, 要求总长度最短. 析:由于这个 n 最大才是18,比较小 ...

  9. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

随机推荐

  1. php数组函数array_column:不用循环就能提取多维数组内容

    作为一个有多年PHP开发经验的码农,我也是前段时间才发现PHP处理数组有这么好用的函数, 至此之前,我处理数组的数据基本都是使用循环,记录一下两个函数的用法: array_column() 函数 返回 ...

  2. SEO优化上首页之搜索引擎作弊案例与反作弊原理

    搜索引擎流量价值巨大,有不少人专门研究排名机制,利用搜索引擎漏洞作弊,寻求快速提高网站排名,进而获取更多的流量和利益,甚至有的网站优化公司专门提供作弊服务.搜索引擎为了杜绝这种情况,必须能过滤大量垃圾 ...

  3. 【翻译】R 中的设计模式

    目录 R 中的设计模式 不动点算法 包装器模式 接口模式 柯里化(Currying) 闭包(Closures) 缓存模式 计数器模式 R 中的设计模式 本文翻译自 Design Patterns in ...

  4. flex拖动图片

    <?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="htt ...

  5. 20145209刘一阳《JAVA程序设计》第五周课堂测试

    第五周课堂测试 1.下列关于内部类的说法,正确的是(ABD) A .其他类不可以用某个类的内部类声明对象. B .内部类字节码文件的名字格式是"外嵌类名$内部类名". C .内部类 ...

  6. 4 django篇

    1.django请求生命周期 wsgi, 他就是socket服务端,用于接收用户请求并将请求进行初次封装,然后将请求交给web框架 (Flask.Django) 中间件,帮助我们对请求进行校验或在请求 ...

  7. SQL Server 查询请求

    当SQL Server 引擎接收到用户发出的查询请求时,SQL Server执行优化器将查询请求(Request)和Task绑定,并为Task分配一个Workder,SQL Server申请操作系统的 ...

  8. Android Studio: Could not download junit.jar (junit:junit:4.12)

    下载了Android Studio 3.1.4,新建一个项目,选择Activity,结果gradle提示 Unable to resolve dependency for ':app@debugAnd ...

  9. 与(&)、或(|)等运算符理解及其特殊用途

    1.按位与运算符(&) 在与运算中两个开关是串联的,如果我们要开灯,需要两个开关都打开灯才会打开.理解为A与B都打开,则开灯,所以是1&1=1任意一个开关没打开,都不开灯,所以其他运算 ...

  10. php-7.1.11-64位

    php-7.1.11-Win32-VC14-x64.zip 链接:https://pan.baidu.com/s/1w8-fJo8-oWrriHyWpU5Fpg 提取码:bd0e 复制这段内容后打开百 ...