Description
The contest is running and the judges is busy watching the progress of the contest. Suddenly, N - 1 (N <= 16) contestants hand up their hand at the same time. The judges should go to answer the contestants' question one by one. The judges already foresee that answering contest i's question would cost Ci minutes. In order to serve all the contestant, each judges is assigned to serve some subset of the contestants. As the judges have limited patience, each one of them can serve the contestants for no more than M minutes.
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.
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.
Input
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)
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)
Output
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.
If it's impossible to serve all the contestants, please output -1 -1 instead.
Sample Input
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
Sample Output
1 6
2 8
-1 -1
8 467
大意:有n个文件,有这n个文件的坐标,要求将他们都串起来,并且连得和不超过m,如果超过则judge要+1,问最少的裁判人数以及把他们连起来回到本身的最小的环的大小。
~~新手感觉好难。虽然注释了,但是还是得自己多写写状态压缩和TSP问题
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int inf = 0x3f3f3f3f; struct edge{ int x; int y; int w; }a[20]; int b[1<<16],best[1<<16],temp[20][1<<16], ok[1<<16]; int dis[20][20],dp[1<<16]; int n,m; int maxn; int check(int x) { int sum = 0; for(int i = 0 ; i < n; i++){ if(x&(1<<i)) sum += a[i].w; } if(sum <= m) return 1; return 0; }//枚举出所有符合条件的状态 int main() { while(~scanf("%d%d",&n,&m)){ for(int i = 0; i < n ; i++){ scanf("%d%d",&a[i].x,&a[i].y); } for(int i = 0; i < n ; i++) scanf("%d",&a[i].w); maxn = (1<<n)-1; int len = 0; for(int i = 0 ; i <= maxn ; i++){ if(check(i)){ ok[i] = 1; b[len++] = i; } //记录下满足条件的状态 else ok[i] = 0; } for(int i = 0; i < n ; i++){ for(int j = 0; j < n; j++){ dis[j][i] = dis[i][j] = ceil(sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y))); } }//记录消费 for(int i = 0; i <= maxn; i++) dp[i] = inf; dp[0] = 0; for(int i = 0; i < len ; i++){ for(int j = maxn ; j >= 0 ; j--){ if(!(b[i]&j)) {//b[i]表示用所走的状态 dp[b[i]|j] = min(dp[b[i]|j],dp[j]+1);//dp记录走了(b[i]|j)距离后的人数 } } } for(int i = 0 ; i <= maxn ; i++) best[i] = inf; for(int i = 0 ; i <= n ; i++) for(int j = 0 ; j <= maxn ; j++) temp[i][j] = inf; temp[0][1] = 0;//temp表示当前位置在i,以j状态走的情况的所走的路 if(dp[maxn] == inf){ printf("-1 -1 "); continue; } else { for(int i = 0; i <= maxn; i++){ if(ok[i]){ for(int j = 0 ; j < n; j++){ if(i&(1<<j)){ best[i] = min(best[i],temp[j][i]+dis[j][0]); for(int k = 0 ;k < n ; k++) if(!(i&(1<<k)))//跟新当前位置 temp[k][i|(1<<k)] = min(temp[k][i|(1<<k)],temp[j][i]+dis[j][k]); } } } } for(int i = 1; i <= maxn; i++){ if(i & 1){ for(int j = i&(i-1); j;j = i&(j-1))//得到j的全排列 best[i] = min(best[i],best[(i-j)|1] + best[j]);//(i-j)|1:因为要影响后面的跟新,|1的话路程为0不影响,把i拆成两半(应该能证明拆的分数就是所需的最少的人数) } } } printf("%d %d ",dp[maxn],best[maxn]); } return 0; }