POJ1062 昂贵的聘礼(最短路径)

题意:

中文题

要点:

等级比较难处理,注意题目中是间接接触也不行,所以假如酋长等级是5,差距为2,可以交换的等级有(3,4,5),(4,5,6),(5,6,7),也即是区间长度是m,交换中最大的与最小的差为m。所以枚举等级区间,所有可行的区间dijkstra一遍,求总的最小值。还要注意这题每个点最短路径求出后要加上当前点物品价值,最后求最小。


15386959 Seasonal 1062 Accepted 208K 0MS C++ 1297B 2016-04-13 16:28:25
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 0x3f3f3f3f
#define min(a,b) a>b?b:a
int n, m;
int map[105][105],rate[105],dis[105],cost[105];
bool vis[105],limit[105];

int dijkstra()
{
	memset(vis, true, sizeof(vis));
	memset(dis, INF, sizeof(dis));
	dis[1] = 0;
	int min, temp=1;
	for (int i = 1; i <= n; i++)
	{
		min = INF;
		for (int j = 1; j <= n;j++)
			if (vis[j] && min > dis[j]&&limit[j])//注意要比较等级
			{
				temp = j;
				min = dis[j];
			}
		vis[temp] = false;
		for (int j = 1; j <= n; j++)
			if (vis[j] && dis[temp] + map[temp][j] < dis[j] && limit[j])
				dis[j] = dis[temp] + map[temp][j];
	}
	min = INF;
	for (int i = 1; i <= n; i++)//路径求出最小值还要加上当前物品价值
	{
		dis[i] += cost[i];
		if (min > dis[i])
			min = dis[i];
	}		
	return min;
}

int main()
{
	int p, l, x,t,v;
	memset(map, INF, sizeof(map));
	scanf("%d%d", &m, &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d%d%d", &p, &l, &x);
		map[i][i] = 0;
		cost[i] = p;
		rate[i] = l;
		while (x--)
		{
			scanf("%d%d", &t, &v);
			map[i][t] =v;
		}
	}
	int minx = INF;
	for (int i = 0; i <= m; i++)//注意不是比较相邻的等级,要与最大的那个比较,就是区间长度要是m
	{
		memset(limit, 0, sizeof(limit));
		for (int j = 1; j <= n; j++)
			if (rate[j] >= rate[1] - m + i && rate[j] <= rate[1] + i)
			{
				limit[j] = 1;
			}
		minx = min(minx, dijkstra());
	}
	printf("%d\n", minx);
	return 0;
}



posted @ 2016-04-13 16:41  seasonal  阅读(82)  评论(0编辑  收藏  举报