HDU 2224 The shortest path

Problem Description

There are n pointson the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You beginat P1 and visit all points then back to P1. But there is a constraint: 
Before you reach the rightmost point Pn, you can only visit the points thosehave the bigger x-coordinate value. For example, you are at Pi now, then youcan only visit Pj(j > i). When you reach Pn, the rule is changed, from nowon you can only visit the points those have the smaller x-coordinate value thanthe point you are in now, for example, you are at Pi now, then you can onlyvisit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point onlyonce.

 

 

Input

The input consistsof multiple test cases. Each case begins with a line containing a positiveinteger n(2 <= n <= 200), means the number of points. Then following nlines each containing two positive integers Pi(xi, yi), indicating thecoordinate of the i-th point in the plane.

 

 

Output

For each testcase, output one line containing the shortest path to visit all the points withthe rule mentioned above.The answer should accurate up to 2 decimal places.

 

 

Sample Input

3

1 1

2 3

3 1

 

 

Sample Output

6.47

 

题目大意:有n个点,告诉每个点的坐标。先从p1pnpipj必须保证i<j。再从pnp1pipj必须保证i>j,同时,来回之后必须保证每个点到且仅到一次。求最短路程。

 

对于题目来说,可以看做是两条从1出发到n的路。每当一条路选定了走法,另一条路的走法就是唯一的了。

两条路径分别为A和B,且起点都是点1,方向严格向右

1、A[i]表示路径A的一种状态,起点为点1,终点为点i,方向严格向右,1<=i<=n

2、B[j]表示路径B的一种状态,起点为点1,终点为点i,方向严格向右,1<=j<=n

3、dist[i][j]为点i与点j之间的距离

4、dp[i][j]为满足以下条件的A[i]和B[j]的路径和的最小长度:

a)A[i]和B[j]包含了所有的1-max(i,j)中的点;

b)A[i]和B[j]中没有重复了点(除了点1,或i=j时的点i)

经过以上定义,题目可以化简为求dp[n][n]的值。

dp[i][j]的意义即是A走到i点,B走到j点时的最短路程

                min(dp[i][k]+dist[k][j])(1<=k<i)   j=i+1;

dp[i][j]  =  

                dp[i][j-1]+dist[j][j-1]            j>i+1;

dp[i][i]  =  dp[i][i-1]+dist[i][i-1];

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

typedef struct NODE
{
    int x ,y;
};

double dist[210][210] ,dp[210][210];
NODE nodes[210];
int n;

double dis(int a ,int b)
{
    return sqrt((double)(nodes[a].y - nodes[b].y) * (nodes[a].y - nodes[b].y) + (nodes[a].x - nodes[b].x) * (nodes[a].x - nodes[b].x));
}

int main()
{
    while(~scanf("%d",&n))
    {
        //计算两点间距离
        for(int i = 1;i<=n;i++)
        {
            scanf("%d%d",&nodes[i].x,&nodes[i].y);
            for(int j = 1;j<i;j++)
            {
                dist[i][j] = dist[j][i] = dis(i,j);
            }
        }
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=i;j++)
            {
                dp[i][j] = dp[j][i] = 999999999;
            }
        }
        
        dp[2][1] = dp[1][2] = dist[1][2];
        for(int j = 3;j<=n;j++)
        {
            dp[j][1] = dp[1][j] = dp[1][j-1] + dist[j-1][j];
        }
        for(int i = 2;i<=n;i++)
        {
            double ans = 99999999;
            for(int k = 1;k<i;k++)
            {
                if(ans > dp[i][k] + dist[k][i+1])
                {
                    ans = dp[i][k] + dist[k][i+1];
                }
            }
            dp[i+1][i] = dp[i][i+1] = ans;
            for(int j = i + 2;j<=n;j++)
            {
                dp[j][i] = dp[i][j] = dp[i][j-1] + dist[j-1][j];
            }
        }
        dp[n][n] = dp[n][n-1] + dist[n-1][n];
        printf("%.2lf\n",dp[n][n]);
    }
    return 0;
}


 

关于 HDU 6852 的 Python 解法或方法,虽然具体题目未提供,但从一般竞赛编程的角度出发,可以推测该问题可能涉及算法设计、数据结构应用或其他计算挑战。以下是基于常见 ACM/ICPC 题目类型的解答框架。 ### 可能的解决方案 #### 方法一:动态规划 (Dynamic Programming) 如果问题是求解最优子结构性质的任务,则可以通过动态规划解决。假设目标是最优路径或者最小代价等问题: ```python def solve_hdu_6852_dp(n, data): dp = [0] * (n + 1) # 初始化 DP 数组 dp[0] = 0 # 基础情况 for i in range(1, n + 1): # 迭代状态转移方程 dp[i] = min(dp[j] + cost(j, i) for j in range(i)) # 转移逻辑需调整[^1] return dp[n] # 示例调用 print(solve_hdu_6852_dp(10, some_data)) ``` 上述代码中的 `cost` 函数应根据实际问题定义具体的开销函数[^1]。 --- #### 方法二:图论最短路 (Graph Shortest Path) 如果是涉及到节点间距离或连通性的题目,Dijkstra 或 Floyd-Warshall 算法可能是适用的选择。 ```python import heapq def dijkstra(graph, start, end): pq = [(0, start)] # 初始优先队列 dist = {node: float('inf') for node in graph} # 距离字典初始化 dist[start] = 0 while pq: current_dist, u = heapq.heappop(pq) if u == end: # 提前终止条件 break for v, weight in graph[u]: # 遍历邻接边 distance = current_dist + weight if distance < dist[v]: dist[v] = distance heapq.heappush(pq, (distance, v)) return dist[end] # 图表示示例 graph_example = { 'A': [('B', 1), ('C', 4)], 'B': [('C', 1), ('D', 2)], 'C': [('D', 1)] } result = dijkstra(graph_example, 'A', 'D') print(result) ``` 此实现适用于加权无向图上的单源最短路径问题[^2]。 --- #### 方法三:贪心策略 (Greedy Algorithm) 对于某些局部优化即可得到全局最优的情况,可尝试采用贪心思路解决问题。 ```python def greedy_solution(data): result = [] total_cost = 0 for item in sorted(data, key=lambda x: x[1]): # 排序依据特定规则 if check_condition(item): # 条件判断函数待补充 result.append(item) total_cost += calculate_cost(item) # 成本计算函数待补充 return result, total_cost data_sample = [(value, priority) for value, priority in zip(range(10), reversed(range(10)))] solution, cost = greedy_solution(data_sample) print(solution, cost) ``` 此处的关键在于如何合理地定义排序准则以及成本评估机制[^3]。 --- ### 总结 以上三种方式涵盖了大部分程序设计竞赛中可能出现的核心思想和技术手段。针对 HDU 6852 的具体情况,建议仔细阅读原题描述并匹配相应的模型与工具来构建最终方案。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值