HDU 4281 (状态压缩+背包+MTSP)
Judges' response
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 741 Accepted Submission(s): 429
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.
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)
If it's impossible to serve all the contestants, please output -1 -1 instead.
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
题意:
第一问:n-1个人有问题需要裁判答复、每个人需要Ci的时间、每个裁判最多回答M时间的问题、问最小需要几个裁判。
第二问:每个人有个位置、所有裁判都在一个位置、问所有裁判回答完问题并回到原点加起来走过的距离最小是多少。
第一问和第二问是独立的,不一定在最少裁判的基础上来走。
解题:
第一问就是一个带有状态的01背包。
第二问是多旅行商问题(MTSP)。
AC代码:
/** @xigua */
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<deque>
#include<queue>
#include<set>
#include<string>
#include<map>
#include<climits>
#define inf LLONG_MAX
#define INF 9e7+5
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
const int maxn = 1e2 + 5;
const ll mod = 3e12 + 7;
const db eps = 1e-9;
int n, m, x[maxn], y[maxn], c[maxn];
int dis[maxn][maxn], dp[1<<16];
int sta[1<<16], len, best[1<<16], en[16][1<<16]; //en的第二维代表状态,比如en[j][i]代表在i状态下以j结尾的最小距离
bool xx[1<<16]; int get_dis(int x1, int x2, int y1, int y2) {
return ceil(sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
} void init() {
memset(xx, 0, sizeof(xx));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) dis[i][j] = get_dis(x[i], x[j], y[i], y[j]);
for (int i = 0; i < (1<<n); i++) {
best[i] = INF;
dp[i] = INF;
for (int j = 0; j < n; j++)
en[j][i] = INF;
}
en[0][1] = best[0] = len = dp[0] = 0;
} bool ok(int x) {//判断当前状态能否由一个裁判回答完
int sum = 0;
for (int i = 0; i < n; i++) {
if (x&(1<<i))
sum += c[i];
}
return m >= sum;
} void get_sta() {
for (int i = 0; i < (1<<n); i++) {
if (ok(i))
sta[++len] = i, xx[i] = 1;
}
} int solve_bag() {
for (int i = 1; i <= len; i++) {
for (int j = (1<<n) - 1; j >= 0; j--) {
if (!(sta[i] & j)) {
dp[sta[i]|j] = min(dp[sta[i]|j], dp[j] + 1);
}
}
}
return dp[(1<<n)-1] == INF ? -1 : dp[(1<<n)-1];
} int solve_dd() {
for (int i = 0; i < (1<<n); i++) {
if (xx[i]) {
for (int j = 0; j < n; j++) {
if (i&(1<<j)) {
best[i] = min(best[i], en[j][i] + dis[j][0]);
for (int k = 0; k < n; k++) {
if (!(i&(1<<k))) {
en[k][i|(1<<k)] = min(en[k][i|(1<<k)], en[j][i] + dis[j][k]);
}
}
}
}
}
}
for (int i = 1; i < (1<<n); i++)
if (i&1)
for (int j = i&(i-1); j; j = i&(j-1)) //枚举比当前低的每个状态
best[i] = min(best[i], best[(i-j)|1] + best[j|1]);
return best[(1<<n)-1];
} void solve() {
while (cin >> n >> m) {
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
for (int i = 0; i < n; i++) cin >> c[i];
init();
get_sta();
int ans1= solve_bag();
if (ans1 == -1) cout << "-1 -1\n";
else cout << ans1 << ' ' << solve_dd() << endl;
}
} int main() {
//cin.sync_with_stdio(false);
//freopen("tt.txt", "r", stdin);
//freopen("hh.txt", "w", stdout);
int t = 1;
//cin >> t;
while (t--) {
solve();
} return 0;
}
HDU 4281 (状态压缩+背包+MTSP)的更多相关文章
- HDU 4739 Zhuge Liang's Mines (状态压缩+背包DP)
题意 给定平面直角坐标系内的N(N <= 20)个点,每四个点构成一个正方形可以消去,问最多可以消去几个点. 思路 比赛的时候暴力dfs+O(n^4)枚举写过了--无意间看到有题解用状压DP(这 ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
- hdu 4739(状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4739 思路:状态压缩. #include<iostream> #include<cs ...
- HDU 3341 状态压缩DP+AC自动机
题目大意: 调整基因的顺序,希望使得最后得到的基因包含有最多的匹配串基因,使得所能达到的智商最高 这里很明显要用状态压缩当前AC自动机上点使用了基因的情况所能达到的最优状态 我最开始对于状态的保存是, ...
- hdu 2167(状态压缩基础题)
题意:给你一个矩阵,让你在矩阵中找一些元素使它们加起来和最大,但是当你使用某一个元素时,那么这个元素周围的其它八个元素都不能取! 分析:这是一道比较基础的状态压缩题,也是我做的第三道状态压缩的题,但是 ...
- hdu 1565(状态压缩基础题)
题意:容易理解. 分析:这是我做的状态压缩第二题,一开始超内存了,因为数组开大了,后来超时了,因为能够成立的状态就那么多,所以你应该先把它抽出来!!总的来说还是比较简单的!! 代码实现: #inclu ...
- HDU 2553 状态压缩
N皇后问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 3006(状态压缩)
The Number of set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 2489(状态压缩+最小生成树)
Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- Luogu网校听课笔记(自用
TG助学课——noilinux 8.2 深夜 Noi Linux的使用——darkflames的博客 #include<bits/stdc++.h> using namespace std ...
- thrift配置——windows客户端与linux服务端通信(C++)
windows客户端: 1.首先要安装boost库 下载源文件 2.安装boost之前先要安装python-3.4.0.amd64,很多地方没有说,弄了很久 3.运行bootstrap.bat 生成b ...
- 语句-python while,for
1.while语句-死循环 #死循环,while只有条件为假时才跳出循环 while True: print('一个小苹果') 2.while语句-带有计数格式的循环 #带有计数格式的循环 var = ...
- wpf 中AxShockwaveFlash重写以及屏蔽鼠标右键
在wpf中需要用到flash播放swf或者图片,需要使用 AxShockwaveFlashObjects.dll和ShockwaveFlashObjects.dll 在项目中使用的时候遇到 问题1.使 ...
- 力荐!35 个最好用的 Vue 开源库!
无论是开发新手还是经验丰富的老手,我们都喜欢开源软件包.对于开发者来说,如果没有这些开源软件包,很难想象我们的生活会变得多么疲惫不堪,而且靠咖啡度日也会成为家常便饭.所幸的是,随着 Vue.js 和 ...
- css width
转载:http://blog.csdn.net/dddddz/article/details/8631655
- 从输入url到浏览器显示页面的过程
总体来说有两个大的方面: 一.网络通信连接部分.二.页面渲染展示部分. 细分详细过程: (网络通信) 1.输入url. 2.DNS解析域名. 3.拿到IP地址后,浏览器向服务器建立tcp连接. 4.浏 ...
- Chef and Graph Queries CodeChef - GERALD07
https://vjudge.net/problem/CodeChef-GERALD07 可以用莫队+带撤销并查集做 错误记录: 1.调试时数组开小了,忘了改大就交了 2.88行和91行少了备份num ...
- 洛谷 P2023 [AHOI2009]维护序列 || 线段树加法和乘法运算
原理倒是非常简单.设原数为x,加法的lazytag为b,乘法的lazytag为a,操作数为c,那么原式为ax+b,乘上c后(ax+b)c=(ac)*x+b*c,加上c后(ax+b)+c=ax+(b+c ...