Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Here is my code.If you tell me where I am wrong, I'll very appreciate it.In Reply To:I think there's some problem of this problem Posted by:swun at 2008-08-01 13:08:50 #include <iostream> #include <cmath> #include <cstdio> using namespace std; int n, k, cost[205][35], arr[205]; struct node { int x, y; }ans[205]; int Dis(int x, int y) { int mid = (x + y) / 2; int sum = 0; for (int i = x; i <= y; i++) sum += abs(arr[i] - arr[mid]); return sum; } int main(void) { int t, tmp, ncas = 1, s, tmpn, tmpk; while (cin >> n >> k) { if (n==0 && k ==0) break; printf("Chain %d\n", ncas++); for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= k; i++) cost[i][i] = 0; for (int i = 1; i <= n; i++) cost[i][1] = Dis(1, i); for (int i = 3; i <= n; i++) { int jj = (i < k) ? i:k; for (int j = 2; j <= jj; j++) { tmp = Dis(j, i); for (int k = j; k < i; k++) { t = cost[k][j-1] + Dis(k+1, i); if (tmp > t) tmp = t; } cost[i][j] = tmp; } } tmp = cost[n][k]; s = 0; tmpn = n; tmpk = k; int i = tmpn-1; while (tmpk > 0 && i > 0) { for (i = tmpn-1; i > 0; i--) { if(tmp == (cost[i][tmpk-1] + Dis(i+1, tmpn))) break; } ans[s].x = i+1; ans[s].y = tmpn; tmpn = i; s++; tmp -= Dis(i+1, tmpn); tmpk--; } tmp = 1; for (int i = s-1; i >= 0; i--) { if(ans[i].x == ans[i].y) printf("Depot %d at restaurant %d serves restaurant %d\n", tmp, ans[i].x, ans[i].x); else printf("Depot %d at restaurant %d serves restaurants %d to %d\n", tmp, (ans[i].x+ans[i].y)/2,ans[i].x, ans[i].y); tmp++; } printf("Total distance sum = %d\n\n", cost[n][k]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator