4 0 0 1 1 0 1 0 1 0 1 1 0 3 0 0 1 1 1 0 2 2 0 0
4.414214 No Solution
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <vector> #include <queue> #include <set> #include <algorithm> #define LL long long using namespace std; const int MAXN = 20; const double INF = 100000000.0; struct Node { int x, y; }Point[MAXN]; int fix[MAXN], start, target, N; double dis[MAXN], dp[1<<21]; double Dis(int a, int b) { double x = (double)(Point[a].x - Point[b].x); double y = (double)(Point[a].y - Point[b].y); return sqrt(x * x + y * y); } double solve(int s, int x) { int m = 0; for(int i=0;i<N;i++) if(s & (1 << i)) dis[m++] = Dis(i, x); sort(dis, dis + m); // cout << m << endl; if(m < 2) return -1; double ans = dis[0] + dis[1]; return ans; } int main() { while(scanf("%d", &N)!=EOF && N) { start = target = 0; for(int i=0;i<N;i++) { scanf("%d%d%d", &Point[i].x, &Point[i].y, &fix[i]); if(fix[i]) start |= (1 << i); target |= (1 << i); } for(int i=0;i<=target;i++) dp[i] = INF; dp[start] = 0; for(int s=start;s<=target;s++) { if(dp[s] == INF) continue; for(int i=0;i<N;i++) { if(!(s & (1 << i))) { double res = solve(s, i); //cout << s << ' ' << i << ' ' << res << endl; if(res >= 0) dp[s|(1<<i)] = min(dp[s|(1<<i)], dp[s] + res); // cout << dp[s] << ' ' << dp[s|(1<<i)] << endl; } } } if(dp[target] >= INF) printf("No Solution\n"); else printf("%.6lf\n", dp[target]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/moguxiaozhe/article/details/47723419