码迷,mamicode.com
首页 > 其他好文 > 详细

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

时间:2016-06-08 00:01:57      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:

 

 

//转移为最短路问题,枚举必经每一个不小于酋长等级的人的最短路
//Time:16Ms	Memory:208K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f
#define MAX 105

int lim, n;
int p[MAX][MAX], rk[MAX];
int d[MAX];
bool v[MAX];

void dijkstra(int x)
{
	for (int i = 0; i <= n; i++)
		d[i] = p[x][i];
	for (int i = 0; i < n; i++)
	{
		int Min = INF, k;
		for (int j = 0; j <= n; j++)
			if (!v[j] && d[j] < Min)
				Min = d[k = j];
		v[k] = true;
		if (k == 0)	return;	//到达不可优惠的地方
		for (int j = 0; j <= n; j++)
			if (!v[j] && d[j] > d[k] + p[k][j])
				d[j] = d[k] + p[k][j];
	}
}

int main()
{
	memset(p, INF, sizeof(p));
	scanf("%d%d", &lim, &n);
	for (int i = 1; i <= n; i++)
	{
		int rp, v;
		scanf("%d%d%d", &p[i][0], &rk[i], &rp);	//p[i][0]:原花费
		while (rp--) {
			scanf("%d", &v);
			scanf("%d", &p[i][v]);
		}
	}

	int minp = INF;
	for (int i = 1; i <= n; i++)	//必经过i点时的最短路
	{
		memset(v, 0, sizeof(v));
		if (rk[i] < rk[1] || rk[i] - rk[1] > lim) continue;
		for (int j = 1; j <= n; j++)	//使所有点都满足[rk[j] >= rk[i] -lim]
			v[j] = rk[j] > rk[i] || rk[i] - rk[j] > lim;
		dijkstra(1);	//从1开始
		minp = min(minp, d[0]);
	}
	printf("%d\n", minp);

	return 0;
}

 

ACM/ICPC 之 昂贵的聘礼-最短路解法(POJ1062)

标签:

原文地址:http://www.cnblogs.com/Inkblots/p/5568585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!