V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wcavell
V2EX  ›  Python

有没有老哥帮我看一看代码啊,dijkstra 实现最短路径的问题.

  •  
  •   wcavell · 2019-06-07 07:01:30 +08:00 · 2221 次点击
    这是一个创建于 2027 天前的主题,其中的信息可能已经有所发展或是发生改变。
    100,0,0,0,100,100,100
    1000,20.892,986,602,138.97,206.2,10.44
    200,25,25,10,1,50,25,140,30
    输入格式是这样,每一行第一个数是地图限制,100 就说明这张图是 100x100,后面的点两两组合,第一个点(0,0)就是起点,最后一个点(100,100)就是终点,中间的点是可以去的城镇.
    程序是模拟一个无人机从起点出发去往终点,最大移动距离 100,点与点之间移动举例按 math.sqrt(x_dis * x_dis + y_dis * y_dis)也就是欧几里得距离算.
    如果点超出移动距离或者不在地图范围内是不算的.
    案例输入的输出是
    200.00
    -1
    115.14
    import heapq
    import sys
    import math
    sys.setrecursionlimit(10000)
    INF = 999999999
    class Point:
    def __init__(self, x, y):
    self.x = float(x)
    self.y = float(y)
    def getCost(p,c):
    x_dis = abs(p.x - c.x)
    y_dis = abs(p.y - c.y)
    cost = math.sqrt(x_dis * x_dis + y_dis * y_dis)
    return cost
    def dijkstra(G, start, endp):
    dis = dict((point, INF) for point in G)
    dis[start] = 0.00
    vis = dict((point, False) for point in G)
    # heap
    pq = []
    heapq.heappush(pq, [dis[start], start])
    path = dict((point, [start]) for point in G)
    while len(pq) > 0:
    v_dis, v = heapq.heappop(pq)
    if vis[v] == True:
    continue
    vis[v] = True
    p = path[v].copy()
    for point in G:
    distance = getCost(v,point)
    if distance > 100.00:
    continue
    new_dis = dis[v] + distance

    if new_dis < dis[point] and (not vis[point]):
    dis[point] = new_dis

    heapq.heappush(pq, [dis[point], point])
    temp = p.copy()
    temp.append(point)
    path[point] = temp
    distance = dis[endp]
    if distance == INF:
    print("-1")
    else:
    print("{:.2f}".format(distance))

    while True:
    try:
    numbers = input().strip().split(",")
    limit = int(numbers[0])
    point_list = []
    numbers = list(map(float, numbers))
    stap = Point(numbers[1],numbers[2])
    endp = Point(numbers[-2],numbers[-1])
    if stap.x>limit or stap.y>limit or stap.x<0 or stap.y<0 :
    print("-1")
    elif endp.x>limit or endp.y>limit or endp.x<0 or endp.y<0 :
    print("-1")
    else:
    for i in range(1, len(numbers), 2):
    if numbers[i]<=limit and numbers[i+1]<=limit:
    point_list.append(Point(numbers[i], numbers[i+1]))
    dijkstra(point_list, point_list[0], point_list[-1])
    except EOFError:
    break

    在一个黑箱运行测试输入显示部分答案错误,一共五个案例,我只有 1,2 是全对,3,4,5 正确率越来越低.
    现在我的代码在本地能很好的输出答案,只好猜测是不是逻辑出了问题,有老哥帮忙看一下的话感激不尽!
    polebug
        1
    polebug  
       2019-06-07 08:56:23 +08:00 via Android
    能不能给个有高亮有缩进的代码 这种没缩进的看都不想看...
    enchilada2020
        2
    enchilada2020  
       2019-06-07 09:41:37 +08:00 via Android
    你这样的代码和提问都是没有灵魂的。。。
    zhangpeter
        3
    zhangpeter  
       2019-06-07 10:51:18 +08:00
    1 哪里的题目
    2 代码高亮
    wcavell
        4
    wcavell  
    OP
       2019-06-07 13:35:53 +08:00
    第一次发帖,不知道怎么设置.
    那我发图好了,这是代码的图
    hduwillsky
        5
    hduwillsky  
       2019-06-07 13:41:08 +08:00 via iPhone
    @wcavell 图呢?
    wcavell
        6
    wcavell  
    OP
       2019-06-07 13:43:49 +08:00
    huiyifyj
        7
    huiyifyj  
       2019-06-07 13:44:22 +08:00
    这代码不能格式化一下再贴过来?
    用 Markdown 的
    ```python
    ```
    格式下啊🙃
    wcavell
        8
    wcavell  
    OP
       2019-06-07 13:48:18 +08:00
    @huiyifyj 我已经上传了 gist 的版本,我试试看吧
    ```python
    import heapq
    import sys
    import math
    sys.setrecursionlimit(10000)
    INF = 999999999
    class Point:
    def __init__(self, x, y):
    self.x = float(x)
    self.y = float(y)
    def getCost(p,c):
    x_dis = abs(p.x - c.x)
    y_dis = abs(p.y - c.y)
    cost = math.sqrt(x_dis * x_dis + y_dis * y_dis)
    return cost
    def dijkstra(G, start, endp):
    dis = dict((point, INF) for point in G)
    dis[start] = 0.00
    vis = dict((point, False) for point in G)
    # heap
    pq = []
    heapq.heappush(pq, [dis[start], start])
    path = dict((point, [start]) for point in G)
    while len(pq) > 0:
    v_dis, v = heapq.heappop(pq)
    if vis[v] == True:
    continue
    vis[v] = True
    p = path[v].copy()
    for point in G:
    distance = getCost(v,point)
    if distance > 100.00:
    continue
    new_dis = dis[v] + distance

    if new_dis < dis[point] and (not vis[point]):
    dis[point] = new_dis

    heapq.heappush(pq, [dis[point], point])
    temp = p.copy()
    temp.append(point)
    path[point] = temp
    distance = dis[endp]
    if distance == INF:
    print("-1")
    else:
    print("{:.2f}".format(distance))

    while True:
    try:
    numbers = input().strip().split(",")
    limit = int(numbers[0])
    point_list = []
    numbers = list(map(float, numbers))
    stap = Point(numbers[1],numbers[2])
    endp = Point(numbers[-2],numbers[-1])
    if stap.x>limit or stap.y>limit or stap.x<0 or stap.y<0 :
    print("-1")
    elif endp.x>limit or endp.y>limit or endp.x<0 or endp.y<0 :
    print("-1")
    else:
    for i in range(1, len(numbers), 2):
    if numbers[i]<=limit and numbers[i+1]<=limit:
    point_list.append(Point(numbers[i], numbers[i+1]))
    dijkstra(point_list, point_list[0], point_list[-1])
    except EOFError:
    break
    ```
    huiyifyj
        9
    huiyifyj  
       2019-06-07 13:52:46 +08:00
    @wcavell #8
    飞机因为最近原因坏了,gist 估计是没办法。
    评论区不支持 md 语法。
    wcavell
        10
    wcavell  
    OP
       2019-06-07 13:56:46 +08:00
    wcavell
        11
    wcavell  
    OP
       2019-06-07 16:15:08 +08:00
    已经解决了,最后发现是一个返回值的问题,代码逻辑没有问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3181 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:31 · PVG 20:31 · LAX 04:31 · JFK 07:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.