网站的流程,wordpress 摘要 空格,提取卡密网站怎么做,宁波网站制作计划1334. 阈值距离内邻居最少的城市
有 n 个城市#xff0c;按从 0 到 n-1 编号。给你一个边数组 edges#xff0c;其中 edges[i] [fromi, toi, weighti] 代表 fromi 和 toi 两个城市之间的双向加权边#xff0c;距离阈值是一个整数 distanceThreshold。
返回能通过某些路径…1334. 阈值距离内邻居最少的城市
有 n 个城市按从 0 到 n-1 编号。给你一个边数组 edges其中 edges[i] [fromi, toi, weighti] 代表 fromi 和 toi 两个城市之间的双向加权边距离阈值是一个整数 distanceThreshold。
返回能通过某些路径到达其他城市数目最少、且路径距离 最大 为 distanceThreshold 的城市。如果有多个这样的城市则返回编号最大的城市。
注意连接城市 i 和 j 的路径的距离等于沿该路径的所有边的权重之和。
示例 1 输入n 4, edges [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold 4 输出3 解释城市分布图如上。 每个城市阈值距离 distanceThreshold 4 内的邻居
城市分别是 城市 0 - [城市 1, 城市 2] 城市 1 - [城市 0, 城市 2, 城市 3] 城市 2 - [城市 0, 城市 1, 城市 3] 城市 3 - [城市 1, 城市 2] 城市 0 和 3 在阈值距离 4 以内都有 2 个邻居城市但是我们必须返回城市 3因为它的编号最大。
示例 2 输入n 5, edges [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold 2 输出0 解释城市分布图如上。 每个城市阈值距离 distanceThreshold 2 内的邻居
城市分别是 城市 0 - [城市 1] 城市 1 - [城市 0, 城市 4] 城市 2 - [城市 3, 城市 4] 城市 3 - [城市 2, 城市 4] 城市 4 - [城市 1, 城市 2, 城市 3] 城市 0 在阈值距离 2 以内只有 1 个邻居城市。
提示
2 n 1001 edges.length n * (n - 1) / 2edges[i].length 30 fromi toi n1 weighti, distanceThreshold 10^4所有 (fromi, toi) 都是不同的。
Floyd
public:int findTheCity(int n, vectorvectorint edges, int distanceThreshold) {int a[105][105];memset(a,10005,sizeof(a));//初始化不能int a[105][105]{10005}这样只会让第一个元素为10005其他为0只有初始化为0可以用这种方式for(int i 0; i n; i ) a[i][i] 0;for(int i 0; i edges.size(); i ){int from edges[i][0];int to edges[i][1];int weight edges[i][2];a[from][to] weight;a[to][from] weight;}//Floyd算法for(int i 0; i n; i ){for(int k 0; k n; k ){for(int p 0; p n; p){if(a[k][i] 10005 a[i][p] 10005 a[k][p] a[k][i] a[i][p]){a[k][p] a[k][i] a[i][p];}}}}int num[105] {0};for(int i 0; i n; i ){for(int j 0; j n; j){if(i!j a[i][j] distanceThreshold){num[i] ;}}}int res 10004;int ans 0;for(int i 0; i n; i ){if(num[i] res){ans i;res num[i];}}return ans;}
};