POJ - 1062 Expensive Dowry (Shortest Path Deformation)

Daniel's idea: For the path from point u to point w, he will trade with people of many levels, but it must be satisfied that the point level difference in the path does not exceed an M value, so how to solve such a problem Woolen cloth? I was very confused before I read the report!
Suppose that if an additional condition is added to this path, the situation may change, requiring that the ranks of all points in the shortest path be in an interval [a, b], if this interval can be given well , just filter the points in the graph.
The determination of this interval is obviously not random, so it must be based on certain conditions. From the meaning of the question, we know that in the end all the shortest paths will converge at point 1, which means that point 1 is where all the shortest paths exist. Point, well, this condition is very important, so that we can give the interval according to point 1. For example, the level of point 1 is lev, which means that at all the shortest points, these points must be satisfied in [lev- M,lev+M] in this interval. Well, maybe you can't wait to use this interval as the last interval, thinking about it, if the level difference between two points that appear in this interval exceeds the M value (which exists), obviously, no In line with the meaning of the title, so this range continues to narrow. In fact, as long as you use your brain a little, you can find such an interval [lev-M,lev],[lev-M+1,lev+1],... ...,[lev,lev+M], first of all These intervals all meet the conditions of the large interval, and if one of these intervals is used as the screening condition, the level of any two points in this interval will not exceed the M value. This is a very special place, and so do I. Stuck here.
Okay, finished talking, just enumerate the interval, then filter the points, and find the shortest path.
Daniel's code: http://blog.csdn.net/chinaczy/article/details/5422443
1 //Shortest path - Dijkstra's algorithm  
2 //The key to this problem is the processing of level restrictions, the best way is to use Enumeration, that is, assuming that the chief level is 5 and the level limit is 2, then the enumeration level needs to be from 3~5, 4~6, 5~7  
3 //Use Dijkstra to calculate the shortest path from the subgraph composed of nodes that meet the level range.  
4 //Summary, some restrictions between graphs can be eliminated by enumeration 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int n;
int mp[110][110];
int value[110],level[110],can_change[110];
int vis[110],dist[110];
int dijkstra(){
	
	memset(vis,0,sizeof(vis));

	for(int i=1;i<=n;i++)
	dist[i]=INF;
	dist[1]=0;
	for(int i=1;i<n;i++)
	{
		you are from = INF;
		int u = -1;
		for(int j=1;j<=n;j++){
			if(!vis[j]&&minn>=dist[j]&&can_change[j]){
				from = dist [j];
				u=j;
			}
		}
		if(u==-1)
		break;
		vis [u] = 1;
		for(int k=1;k<=n;k++){
			if(can_change[k]&&dist[k]>dist[u]+mp[u][k]){
				dist [k] = dist [u] + mp [u] [k];
			}
		}
	}
	int mine = INF;
	for(int i=1;i<=n;i++){
		dist[i]+=value[i];
		mina = min (mina, dist [i]);
	}
	return mina;
}

int main(){
	int	limit;
	scanf("%d%d",&limit,&n);
	
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	mp [i] [j] = (i == j? 0: INF);
	
	for(int i=1;i<=n;i++){
		int num;
		scanf("%d%d%d",&value[i],&level[i],&num);
		for(int j=0;j<num;j++){
			int id,pri;
			scanf("%d%d",&id,&pri);
			mp[i][id]=pri;
		}
	}
	you are from = INF;
	for(int i=0;i<=limit;i++)
	{
		memset(can_change,0,sizeof(can_change));
		for(int j=1;j<=n;j++){
			if(level[j]>=level[1]-limit+i&&level[j]<=level[1]+i){
				can_change[j]=1;
			}
		}
		from = min (from, right ());
	}
	 printf("%d\n",minn);
	
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325812933&siteId=291194637