下列c语言程序改成python,并详细注解。#include<iostream> #include"qx.h" using namespace std; //弗洛伊德算法 void graph::floyd(graph &t, const int n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { t.a[i][j]=t.arcs[i][j]; if((i!=j)&&(a[i][j]<max)) t.path[i][j]=i; else t.path[i][j]=0; } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(t.a[i][k]+t.a[k][j]<t.a[i][j]) { t.a[i][j]=t.a[i][k]+t.a[k][j]; t.path[i][j]=t.path[k][j]; } } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(i!=j) { cout<<i<<"到"<<j<<"的最短路径为"<<t.a[i][j]<<":"; int next=t.path[i][j]; cout<<j; while(next!=i) { cout<<"←"<<next; next=t.path[i][next]; } cout<<"←"<<i<<endl; } } } //计算最短距离之和 void graph::add(graph &t) { int sum[n+1]; for(int i=0;i<n+1;i++) sum[i]=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i!=j) { sum[i]=sum[i]+t.a[i][j]; } } cout<<endl; cout<<i<<"到各顶点的最短路径总和为"<<sum[i]<<endl; } sum[0]=sum[1]; int address=1; for(int i=2;i<n+1;i++) if(sum[0]>sum[i]) { sum[0]=sum[i]; address=i; } cout<<"所以最短路径总和为"<<sum[0]<<" 学院超市的最佳选址为顶点"<<address<<endl; } //主函数 void main() { graph t;int i,j,w; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(i==j) t.arcs[i][j]=0; else t.arcs[i][j]=max; cout<<" 学校超市最佳选址*"<<endl<<endl<<endl; cout<<"请输入请输入存在路径的两个单位以及相通两个单位间的距离(用空格隔开)"; cout<<endl; for(int k=1;k<=e;k++) { cin>>i>>j>>w; t.arcs[i][j]=w; } t.floyd(t,n); t.add(t); system("pause"); }
时间: 2024-01-19 21:03:24 浏览: 101
以下是将C语言程序改写成Python的代码,并进行注释:
```python
class Graph:
def __init__(self, n, e):
self.n = n # 图中顶点个数
self.e = e # 图中边的条数
self.arcs = [[float('inf')] * (n+1) for _ in range(n+1)] # 初始化邻接矩阵,用inf表示两点不直接相连
self.a = [[float('inf')] * (n+1) for _ in range(n+1)] # 存储最短距离
self.path = [[0] * (n+1) for _ in range(n+1)] # 存储最短路径
# 弗洛伊德算法
def floyd(self):
for i in range(1, self.n+1):
for j in range(1, self.n+1):
self.a[i][j] = self.arcs[i][j]
if i != j and self.a[i][j] < float('inf'):
self.path[i][j] = i
else:
self.path[i][j] = 0
for k in range(1, self.n+1):
for i in range(1, self.n+1):
for j in range(1, self.n+1):
if self.a[i][k] + self.a[k][j] < self.a[i][j]:
self.a[i][j] = self.a[i][k] + self.a[k][j]
self.path[i][j] = self.path[k][j]
for i in range(1, self.n+1):
for j in range(1, self.n+1):
if i != j:
print(f'{i}到{j}的最短路径为{self.a[i][j]}:', end='')
next = self.path[i][j]
print(j, end='')
while next != i:
print(f'←{next}', end='')
next = self.path[i][next]
print(f'←{i}')
# 计算最短距离之和
def add(self):
sum = [0] * (self.n+1)
for i in range(1, self.n+1):
for j in range(1, self.n+1):
if i != j:
sum[i] += self.a[i][j]
print(f'{i}到各顶点的最短路径总和为{sum[i]}')
address = 1
for i in range(2, self.n+1):
if sum[0] > sum[i]:
sum[0] = sum[i]
address = i
print(f'所以最短路径总和为{sum[0]},学院超市的最佳选址为顶点{address}')
if __name__ == '__main__':
n = int(input('请输入图中顶点个数:'))
e = int(input('请输入图中边的条数:'))
t = Graph(n, e)
print('学校超市最佳选址*')
print()
print('请输入存在路径的两个单位以及相通两个单位间的距离(用空格隔开)')
print()
for k in range(1, e+1):
i, j, w = map(int, input().split())
t.arcs[i][j] = w
t.floyd()
t.add()
input('按回车键退出')
```
注:由于Python中列表下标从0开始,而题目中的C语言程序列表下标从1开始,因此在Python代码中需要将下标全部减1。
阅读全文