POJ Telephone Lines (two points + dijkstra)

l i n k link link

Question: Give you n sites and m undirected edges. Find a path from site 1 to site n. What is the minimum and maximum free path for k paths?

Solution: Seeing that the largest path is the smallest, one should think of dichotomy, but because I saw that k paths are free, I went to the hierarchical graph. Two-point answer, and then dijkstra decides that if that edge is greater than mid, then that edge is assigned a value of 1, otherwise, it is assigned a value of 0. If the shortest path is less than K, it means that the answer is ok, just find it in two.

#include <cstdio>
#include <queue>
#include <cstring>
#define ll long long
#define pi pair<int,int>
#define mk make_pair
#define pb push_back
using namespace std;
const int maxn = 1e3+10;
vector<pi>G[maxn];
int n,m,K;
int vis[maxn],d[maxn];
bool ok(int ans)
{
    
    
	priority_queue<pi>q;
	memset(d,0x3f,sizeof(d));
	memset(vis,0,sizeof(vis));
	q.push(mk(0,1));
	d[1] = 0;
	while(!q.empty())
	{
    
    
		int u = q.top().second;
		q.pop();
		if(vis[u])continue;
		vis[u] = 1;
		for(int i=0;i<G[u].size();i++)
		{
    
    
			int v = G[u][i].first;
			int w = G[u][i].second;
			
			if(w <= ans)w = 0;
			else w = 1;
			
			if(d[v] > d[u] + w)
			{
    
    
				d[v] = d[u] + w;
				q.push(mk(-d[v],v));
			}
		}
	}
	if(d[n] <= K)return true;
	else return false;
}
int main()
{
    
    
	int mx=0;
	scanf("%d%d%d",&n,&m,&K);
	for(int i=1;i<=m;i++)
	{
    
    
		int u,v,w;
		scanf("%d%d%d",&u,&v,&w);
		G[u].pb(mk(v,w));
		G[v].pb(mk(u,w));
		mx = max(mx,w);
	}
	int l = 0,r = mx,ans = -1;
	while(l <= r)
	{
    
    
		int mid = (l+r)/2;
		if(ok(mid))r = mid - 1,ans = mid;
		else l = mid + 1;
	}
	printf("%d\n",ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44499508/article/details/106783392