关于C++和Python的代沟
  • 板块灌水区
  • 楼主_LiHX
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/13 21:36
  • 上次更新2023/10/28 06:38:31
查看原帖
关于C++和Python的代沟
340137
_LiHX楼主2022/3/13 21:36

有没有哪位写Python的大佬帮忙修改一下这份C++式Python最短路代码的语法,感激不尽啊!!!

fr=[0]*50010 ;
nxt=[0]*50010 ;
to=[0]*50010 ; 
w=[0]*50010 ; 
now=[0]*50010 ;
book=[0]*50010;
dis=[0]*50010;
cnt=0;
inf=1e9;

for i in range(0,50010):
    nxt[i]=0;
    w[i]=0;
    fr[i]=0;
    book[i]=0;
    dis[i]=inf;

dis[1]=0;
book[1]=1;
cnt=0;

from heapq import *
q=[];
heapify(q);

def add(u , v , data):
    to[cnt]=v;
    w[cnt]=data;
    nxt[cnt]=fr[u];
    fr[u]=cnt;

n=int(input());
m=int(input());

for i in range(1,m):
    u=int(input());
    v=int(input());
    w=int(input());
    cnt=cnt+1;
    add(u,v,w);
    cnt=cnt+1;
    add(v,u,w);

heappush(q,1);
while q.isEmpty()==0:
    x=q.pop();
    heappop(q);
    book[x]=0;
    i=fr[x];
    while i!=0:
        i=nxt[i];
        if dis[to[i]]>dis[now[i]]+w[i]:
            dis[to[i]]=dis[now[i]]+w[i];
            if book[to[i]]==0:
                heappush(q,to[i]);
                book[to[i]]=1;

for i in range(1,cnt):
    if dis[to[i]]>dis[now[i]]+w[i]:
        ans=1;
        break;

if ans==1:
    print("Yes");
else:
    print("No");
2022/3/13 21:36
加载中...