题意:n个计算机通过电缆连接,怎么连接使用的电缆最少
mmp,死wa不过,memset(vis,0,sizeof(per)),太不小心了
#include <iostream> #include <memory.h> #include <stdio.h> #include<math.h> using namespace std; struct Node { int x; int y; Node() { x = 0; y = 0; } }; const int N = 9; Node per[N]; int res[N]; int n; double final = 0x7FFFFFFF; int vis[N]; double px(Node* n1, Node* n2) { return sqrt( 0.0 + (n1->x - n2->x) * (n1->x - n2->x) + (n1->y - n2->y) * (n1->y - n2->y)) + 16.00; } void dfs(int a[], int cur) { if (cur == n) { double sum = 0; for (int i = 0; i < n - 1; i++) sum += px(per + a[i], per + a[i + 1]); if (sum < final) { final = sum; memcpy(res, a, sizeof(res)); } return; } for (int i = 0; i < n; i++) { if (vis[i]) continue; vis[i] = 1; a[cur] = i; dfs(a, cur + 1); vis[i] = 0; } } int main() { //freopen("d:\1.txt", "r", stdin); int t = 1; while (cin >> n && n) { memset(per, 0, sizeof(per)); memset(vis, 0, sizeof(vis)); memset(res,0,sizeof(res)); final = 0x7FFFFFFF; int s, e; for (int i = 0; i < n; i++) { cin >> s >> e; Node p; p.x = s; p.y = e; per[i] = p; } int a[N]; dfs(a, 0); cout << "**********************************************************" << endl; cout << "Network #" << t << endl; for (int i = 0; i < n - 1; i++) { double tt = px(per + res[i], per + res[i + 1]); printf( "Cable requirement to connect (%d,%d) to (%d,%d) is %.2lf feet. ", per[res[i]].x, per[res[i]].y, per[res[i + 1]].x, per[res[i + 1]].y, tt); } printf("Number of feet of cable required is %.2f. ", final); t++; } return 0; }