两道题,感觉都不难,一道简单计算,一道floyd,都能快速AC
- 每人可以投K票,求保证获胜最小的K
n = int(input()) a = list(map(int, input().split())) sum = 0 ans = 0 for i in a: if i>ans: ans = i sum+=i while((ans*n-sum)<=sum): ans+=1 print(ans)
- 找能在d距离内到达其他城市数量最少的城市
def floyd(n, adj): ans = adj for k in range(n): for i in range(n): for j in range(n): ans[i][j] = min(ans[i][j], ans[i][k]+ans[k][j]) return ans T = int(input()) for i in range(T): n,m,d = list(map(int, input().split())) adj_matrix = [[float('inf')]*n for i in range(n)] for j in range(n): adj_matrix[j][j] = 0 for j in range(m): l,r,w = list(map(int, input().split())) adj_matrix[l][r] = w adj_matrix[r][l] = w dis = floyd(n, adj_matrix) ans = 0 fewest = n for j in range(n): count = 0 for to in dis[j]: if to<=d:count+=1 if count<=fewest: fewest = count ans = j print(ans)
全部评论
(10) 回帖