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

[leetcode]5321. 阈值距离内邻居最少的城市_第1张图片
[leetcode]5321. 阈值距离内邻居最少的城市_第2张图片
[leetcode]5321. 阈值距离内邻居最少的城市_第3张图片
[leetcode]5321. 阈值距离内邻居最少的城市_第4张图片

多源最短路径类型的题目

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<
        //     }
        //     cout<
        // }
    }
    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<
        // }
        // cout<
        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;
    }
};

你可能感兴趣的:(leetcode,图论)