[leetcode]5321. 阈值距离内邻居最少的城市

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

多源最短路径类型的题目

1、floydWarshall
class Solution {
public:
    void floydWarshall(int n,vector<vector<int>>& graph,vector<vector<int>>& dist)
    {
        for(int k = 0; k < n; k++)
        {//k表示用0~k之间的节点去松弛dist[i][j]
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    if(dist[i][k] + dist[k][j] < dist[i][j])
                    {
                        dist[i][j] = dist[i][k] + dist[k][j];
                    }
                }
            }
        }
        // for(int i = 0; i < n; i++)
        // {
        //     for(int j = 0; j < n; j++)
        //     {
        //         cout<<dist[i][j]<<"  ";
        //     }
        //     cout<<endl;
        // }
    }
    int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
        int INF = 99999999;
        vector<vector<int>>graph(n,vector<int>(n,0));
        vector<vector<int>>dist(n, vector<int>(n, INF));
        for(int i = 0; i < edges.size(); i++)
        {
            graph[edges[i][0]][edges[i][1]] = edges[i][2];
            graph[edges[i][1]][edges[i][0]] = edges[i][2];
            dist[edges[i][0]][edges[i][1]] = edges[i][2];
            dist[edges[i][1]][edges[i][0]] = edges[i][2];
        }

        floydWarshall(n, graph,dist);

        vector<int>counts(n, 0);//阀值内邻居城市的counts
        int res = 0, minCount = INF;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(dist[i][j] <= distanceThreshold && i != j)//i != j
                {
                    counts[i]++;
                }
            }
            if(counts[i] <= minCount)
            {
                res = i;
                minCount = counts[i];
            }
        }
        // for(int i = 0; i < n; i++)
        // {
        //     cout<<counts[i]<<"--";
        // }
        // cout<<endl;
        return res;
    }
};

2、权值非负可以使用dijkstra算法

class Graph
{
	int n;
	list<pair<int,int> > *adj;//邻接表 
public:
	Graph(int _n)
	{
		n = _n;
		adj = new list<pair<int, int> >[_n];
	}
	void addEdge(int u, int v, int weight)
	{
		adj[u].push_back(make_pair(v,weight));
        adj[v].push_back(make_pair(u,weight));
	}
    vector<vector<int>> dijkstra()
    {
        int INF = 99999999;
        vector<vector<int>>dist(n, vector<int>(n, INF));
        
        for(int i = 0; i < n; i++)
        {
            priority_queue< pair<int, int>,
                        vector<pair<int,int>>,
                        greater<pair<int,int>> > pq;  //小根堆
            dist[i][i] = 0;
            pq.push( make_pair(0,i));
            while(!pq.empty())
            {
                int u = pq.top().second;
                pq.pop();

                list<pair<int,int>>::iterator it;
                for(it = adj[u].begin(); it != adj[u].end(); it++)
                {
                    int v = it->first;
                    int weight = it->second;
                    if(dist[i][v] > dist[i][u] + weight)
                    {
                        dist[i][v] = dist[i][u] + weight;
                        pq.push(make_pair(dist[i][v],v));
                    }
                }
            }
        }
        return dist;
    }
}; 
class Solution {
public:
    int findTheCity(int n, vector<vector<int>>& edges, int distanceThreshold) {
        Graph g(n);
        for(int i = 0; i < edges.size(); i++)
        {
            g.addEdge(edges[i][0], edges[i][1], edges[i][2]);
        }
        vector<vector<int>>dist = g.dijkstra();
        vector<int>counts(n, 0);//阀值内邻居城市的counts
        int INF = 99999999;
        int res = 0, minCount = INF;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(dist[i][j] <= distanceThreshold && i != j)//i != j
                {
                    counts[i]++;
                }
            }
            if(counts[i] <= minCount)
            {
                res = i;
                minCount = counts[i];
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值