POJ-1062 昂贵的聘礼 解题报告

Question Link

题目分析

建立图模型,物品对应结点,兑换关系,以及优惠价格,对应终点,和边权,而你现在要做的就是找一条链路,就是多个兑换关系形成的一条路,这条路上所有的权值,再加上,最终兑换成的物品本身的价格,使得这个值最小。由于物品本身价格不变,那么可知,我们需要求得这条路权加和最小。对应dijkstra求最短路。
但是上述分析缺少一个限制,就是这条路上的所有点,他们的最大等级减去他们最小的等级,这个等级差,不能超过m,这个给定的值。我们要把这个m值变成,选取点的限制条件,这样我们就能利用dijkstra算法解题。所以我们枚举区间,明确这个区间长度为m,并且左闭右闭,枚举下限,由 lev[ 1 ] - m ,到lev[ 1 ]。

AC code

Dijkstra algorithm

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 110
int m,n; 
bool vis[N],can_chang[N];int dis[N],e[N][N],val[N],lev[N];
int dijkstra(int l,int r){
	int minx,x;
	mset(vis,0);
	rep(i,1,n)	dis[i] = inf; 	dis[1] = 0;  
	for (int aai = 1; aai <= n; aai++){
		minx = inf; 
		for (int i = 1; i <= n; i++ ){
			if ( !vis[i] && dis[i] < minx && l <= lev[i] && lev[i] <= r )
				minx = dis[ x = i ];
		}
		vis[ x ] = 1; 
		for (int i = 1; i <= n; i++ ){
			if ( dis[i] > dis[x] + e[x][i] && e[x][i] != inf && l <= lev[i] && lev[i] <= r )
				dis[i] = dis[ x ] + e[x][i] ; 
		}
	}
	int min_cost = inf; 
	rep(i,1,n){
		dis[ i ] += val[ i ];
		min_cost = min( min_cost , dis[i] ); 
	}
	return min_cost; 
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
 
	m = read(); n = read();
	
	rep(i,1,n)	rep(j,1,n)	e[i][j] = (i==j)? 0:inf;  	
	rep(i,1,n){
		int va_,le_,num_;
		va_ = read();le_ = read(); num_ = read();
		val[i] = va_;	lev[i] = le_ ;  
		rep(j,1,num_){
			int y_; 
			y_ = read(); va_ = read();
			e[ i ][ y_ ] = va_; 
		} 
	}
//	rep(i,1,n){
//		rep(j,1,n){
//			printf("%7d ",e[i][j]);
//		}
//		cout<

Bellman-Ford algorithm

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 110
int m,n; 
bool vis[N],can_chang[N];int dis[N],val[N],lev[N];
struct Ead{
	int u,v,w; 
}e[N*N];int tot; 
int bellman(int l,int r){
	int minx,x;
	mset(vis,0);
	rep(i,1,n)	dis[i] = inf; 	dis[1] = 0;  
	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= tot; j++){
			if ( dis[ e[j].v ] > dis[ e[j].u ] + e[j].w && l <= lev[ e[j].v ] && lev[ e[j].v ] <= r && l <= lev[ e[j].u ] && lev[ e[j].u ] <= r  )
				dis[ e[j].v ] = dis[ e[j].u ] + e[j].w ;
		}
	}
	int min_cost = inf; 
	rep(i,1,n){
		dis[ i ] += val[ i ];
		min_cost = min( min_cost , dis[i] ); 
	}
	return min_cost; 
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	m = read(); n = read();
	rep(i,1,n){
		int va_,le_,num_;
		va_ = read();le_ = read(); num_ = read();
		val[i] = va_;	lev[i] = le_ ;  

		rep(j,1,num_){
			int y_; 
			y_ = read(); va_ = read();
			e[++tot].u = i; e[tot].v = y_ ; e[tot].w = va_; 			
		} 
	}

	int min_cost = inf; 
	for (int i = lev[1]-m; i <= lev[1]; i++){
		min_cost = min ( min_cost , bellman(i,i+m) ) ;
	}
	printf("%d",min_cost);
	return 0;
}

SPFA algorithm

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 110
int m,n; 
bool vis[N],can_chang[N];int dis[N],val[N],lev[N];
struct Ead{
	int u,v,w,nx; 
}e[N*N];int tot,hea[N]; 
int SPFA(int l,int r){
	
	rep(i,1,n)	dis[i] = inf; 	dis[1] = 0;  
	queueq; q.push( 1 );
	while ( !q.empty() ){
		int x = q.front() ; q.pop() ; 
		vis[ x ] = 0; 
		for (int i = hea[x]; i ; i = e[i].nx ){
			if ( dis[ e[i].v ] > dis[ e[i].u ] + e[i].w && l <= lev[e[i].v] && lev[e[i].v] <= r){
				dis[ e[i].v ] = dis[ e[i].u ] + e[i].w ; 
				if ( !vis[ e[i].v ] ){
					vis[ e[i].v ] = 1; 
					q.push( e[i].v );
				}
			}
		}
	}

	int min_cost = inf; 
	rep(i,1,n){
		dis[ i ] += val[ i ];
		min_cost = min( min_cost , dis[i] ); 
	}
	return min_cost; 
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	m = read(); n = read();
	rep(i,1,n){
		int va_,le_,num_;
		va_ = read();le_ = read(); num_ = read();
		val[i] = va_;	lev[i] = le_ ;  

		rep(j,1,num_){
			int y_; 
			y_ = read(); va_ = read();
			e[++tot].u = i; e[tot].v = y_ ; e[tot].w = va_;  e[tot].nx = hea[i];  hea[i] = tot; 			
		} 
	}

	int min_cost = inf; 
	for (int i = lev[1]-m; i <= lev[1]; i++){
		min_cost = min ( min_cost , SPFA(i,i+m) ) ;
	}
	printf("%d",min_cost);
	return 0;
}

dijkstra + priority

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; 
#define rep(i,aa,bb) for(register int i=aa;i<=bb;i++)
#define rrep(i,aa,bb) for(register int i=aa;i>=bb;i--)
#define mset(var,val)	 memset(var,val,sizeof(var))
#define LL long long 
#define eps 0.000001
#define inf 0x7f7f7f7f
#define llinf 1e18
#define exp 0.000001
#define pai 3.141592654
#define random(x)   rand()%(x)
#define lowbit(x)   x&(-x)
inline int read()
{
	int x=0,y=1;char a=getchar();while ( a>'9' || a<'0'){if ( a=='-')y=-1;a=getchar();}
	while ( a>='0' && a<='9' ){	x=(x<<3)+(x<<1)+a-'0'; a=getchar();}return x*y;
}
#define N 110
int m,n; 
bool vis[N],can_chang[N];int dis[N],val[N],lev[N];
struct Ead{
	int u,v,w,nx; 
}e[N*N];int tot,hea[N]; 
struct node {
	int v,w;
	bool operator < (const node& rhs)const {
		return w > rhs.w; 
	} 
};
int dijk(int l,int r){
	
	rep(i,1,n)	dis[i] = inf; 	dis[1] = 0;  
	priority_queue< node >q; 
	node xxx; xxx.v = 1;xxx.w = 0; 
	q.push(	xxx );
	while ( !q.empty() ){
		node x = q.top() ; q.pop() ; 
		if ( dis[ x.v ] != x.w )	continue; 
		for (int i = hea[x.v]; i ; i = e[i].nx ){
			if ( dis[ e[i].v ] > dis[ e[i].u ] + e[i].w && l <= lev[e[i].v] && lev[e[i].v] <= r){
				dis[ e[i].v ] = dis[ e[i].u ] + e[i].w ; 
				xxx.v = e[i].v ; xxx.w = dis[ e[i].v ];
				q.push( xxx ) ;
			}
		}
	}

	int min_cost = inf; 
	rep(i,1,n){
		dis[ i ] += val[ i ];
		min_cost = min( min_cost , dis[i] ); 
	}
	return min_cost; 
}
int main()
{
//	freopen("1.txt","r",stdin);
	srand((int)time(0));
//	std::ios::sync_with_stdio(false);
	m = read(); n = read();
	rep(i,1,n){
		int va_,le_,num_;
		va_ = read();le_ = read(); num_ = read();
		val[i] = va_;	lev[i] = le_ ;  

		rep(j,1,num_){
			int y_; 
			y_ = read(); va_ = read();
			e[++tot].u = i; e[tot].v = y_ ; e[tot].w = va_;  e[tot].nx = hea[i];  hea[i] = tot; 			
		} 
	}

	int min_cost = inf; 
	for (int i = lev[1]-m; i <= lev[1]; i++){
		min_cost = min ( min_cost , dijk(i,i+m) ) ;
	}
	printf("%d",min_cost);
	return 0;
}

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