算法设计与分析_第4章_实验报告

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
using namespace std;
const int maxn = 105; const double pi = acos(-1.0); const double EARTH_RADIUS = 6378.137;
struct point {
double x, y; };
point pp[maxn];
double dfs(int L, int R) {
if (dp[L][R] != -1.0) return dp[L][R]; if (R - L == 1) return 0; int k = L + 1; for (int i = k + 1; i < R; i++) {
if (dis(pp[L], pp[i]) + dis(pp[R], pp[i]) < dis(pp[L], pp[k]) + dis( pp[R], pp[k])) k = i;
1
1.最优三角剖分 (贪心法)
算法简介:
定义 dp[i][j] 为子多边形 i, i+ 1, …j 的最优值。 则 dp[i][j] = max(d[i][k] + dp[k][j] + w(I, j , k) | I < k < j ) 根据启发式信息选取断点 vk, 选取使 dist(v0, vk) + dist(vk,vn) 最小的 vk.
结果输出:
第一组输入(20 个顶点):
4
The Min_value is: 454030.591857 The mapping with point 0 and point 19 is : 18 The mapping with point 0 and point 18 is : 17 The mapping with point 0 and point 17 is : 16 The mapping with point 0 and point 16 is : 15 The mapping with point 0 and point 15 is : 14 The mapping with point 0 and point 14 is : 13 The mapping with point 0 and point 13 is : 12 The mapping with point 0 and point 12 is : 11 The mapping with point 0 and point 11 is : 10 The mapping with point 0 and point 10 is : 9 The mapping with point 0 and point 9 is : 8 The mapping with point 0 and point 8 is : 7 The mapping with point 0 and point 7 is : 6 The mapping with point 0 and point 6 is : 1 The mapping with point 1 and point 6 is : 2 The mapping with point 2 and point 6 is : 3 The mapping with point 3 and point 6 is : 5 The mapping with point 3 and point 5 is : 4 绘图结果如下:
int main() {
3
freopen("triangulation_in_2.txt", "r", stdin); freopen("triangulation_out_2.txt", "w", stdout); double x, y, mid; int n; while(scanf("%lf %lf %lf %d", &mid, &x, &y, &n) == 4) {
}
double d = dfs(L, k) + dfs(k, R) + dis(pp[L], pp[k]) + dis(pp[R], pp[k]) + dis(pp[L], pp[R]);
dp[L][R] = d; rec[L][R] = k;
return dp[L][R]; }
void out_dfs(int L, int R) {
第二组输入(29 个顶点):
5
The Min_value is: 341549.669272 The mapping with point 0 and point 27 is : 26 The mapping with point 0 and point 26 is : 25 The mapping with point 0 and point 25 is : 24 The mapping with point 0 and point 24 is : 1 The mapping with point 1 and point 24 is : 2 The mapping with point 2 and point 24 is : 3 The mapping with point 3 and point 24 is : 4 The mapping with point 4 and point 24 is : 5 The mapping with point 5 and point 24 is : 6 The mapping with point 6 and point 24 is : 7 The mapping with point 7 and point 24 is : 8 The mapping with point 8 and point 24 is : 9 The mapping with point 9 and point 24 is : 10 The mapping with point 10 and point 24 is : 11 The mapping with point 11 and point 24 is : 12 The mapping with point 12 and point 24 is : 13 The mapping with point 13 and point 24 is : 14 The mapping with point 14 and point 24 is : 15 The mapping with point 15 and point 24 is : 16 The mapping with point 16 and point 24 is : 17 The mapping with point 17 and point 24 is : 18 The mapping with point 18 and point 24 is : 19 The mapping with point 19 and point 24 is : 20 The mapping with point 20 and point 24 is : 21
pp[n].x = x; pp[n].y = y; } for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) dp[i][j]= -1.0; clr(rec, -1); dfs(0, n); printf("The Min_value is: %lf\n", dp[0][n]); out_dfs(0, n); return 0; }
double dp[maxn][maxn]; int rec[maxn][maxn];
double rபைடு நூலகம்d(double LatOrLon) {
return LatOrLon*pi/180.0; }
2
double dis(point A, point B) { //将对应的经纬度转化为弧度
double radLat1 = rad(A.y); double radLat2 = rad(B.y); double radlng1 = rad(A.x); double radlng2 = rad(B.x); //利用正弦余弦公式求距离 double s = acos(cos(radLat1) * cos(radLat2) * cos(radlng1 - radlng2) + s in(radLat1) * sin(radLat2)); s = s * EARTH_RADIUS * 1000; return s; }
if (rec[L][R] == -1) return ; printf("The mapping with point %d and point %d is : %d \n", L, R, rec[L] [R]); out_dfs(L, rec[L][R]); out_dfs(rec[L][R], R); }
时间复杂度由动态规划的 O(n^3) 降到了 O(n^2)
源程序:
//采用记忆化搜索的方法
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #define clr(x, y) memset(x, y, sizeof x)
算法设计与分析
第四章上机作业报告
io07@qq.com
陈思嘉 | 2013211310(班级)| 2013211445(学号)
目录 1.最优三角剖分 (贪心法) ................................................ 2
算法简介: ...................................................................... 2 源程序: .......................................................................... 2 结果输出: ...................................................................... 4 2.Huffman 编码................................................................ 8 算法思路: ...................................................................... 8 源程序: .......................................................................... 8 结果输出: ..................................................................... 11 3. Dijkstra 单源最短路径 .............................................. 13 算法简述: ................................................................. 13 源程序: ........................................................................ 13 结果输出: .................................................................... 16 4. kruskal 最小生成树 ................................................... 19 算法简述: .................................................................... 19 源程序: ..................................................................... 19 结果输出: ....................................................................22
相关文档
最新文档