poj 1062 昂贵的聘礼
http://poj.org/problem?id=1062
题意:一个人想娶酋长的女儿,酋长有个条件,就是用金币或者是用物品和少量的金币来交换,其他拥有物品的人亦可以用同样方法来交换;
思路:dijkstra+枚举,思路是从网上看的,题目中的“地位较高的的人不会再和他交易”其实挺干扰人的,没有用到这个;
代码:
View Code
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
int leval[110] = {0};
int value[110] = {0};
int map[110][110] = {0};
int ok[110] = {0};
int n = 0;
int len = 0;
int p = 0;
int val = 0;
int vis[110] = {0};
int d[110] = {0};
int dijkstra()
{
memset(vis,0,sizeof(vis));
d[1] = 0;
for(int i = 2;i <= n; ++i)
d[i] = 99999999;
for(int i = 1; i <= n; ++i)
{
int temp = 0;
int max = 99999999;
for(int j = 1;j <= n; ++j)
if(ok[j] && !vis[j] && d[j] < max)
{
max = d[j];
temp = j;
}
vis[temp] = 1;
for(int j = 1;j <= n; ++j)
if(ok[j]&&!vis[j])
d[j] = min(d[j],map[temp][j] + d[temp]);
}
int mm = d[1] + value[1];
for(int i = 2;i <= n; ++i)
{
d[i] = d[i] + value[i];
if(d[i] < mm)
mm = d[i];
}
return mm;
}
int main()
{
int num = 0;
scanf("%d%d",&len,&n);
for(int i = 0 ;i <= n; ++i)
for(int j = 0; j <= n; ++j)
map[i][j] = 99999999;
for(int i = 0;i <= n; ++i)
map[i][i] = 0;
for(int i = 1;i <= n; ++i)
{
scanf("%d%d%d",&value[i],&leval[i],&num);
for(int j = 1;j <= num; ++j)
{
scanf("%d%d",&p,&val);
map[i][p] = val;
}
}
int tt = leval[1];
int mincost = 999999999;
for(int i = 0;i <= len; ++i)
{
memset(ok,0,sizeof(ok));
for(int j = 1;j <= n; ++j)
if(leval[j] >= tt-len+i&&leval[j] <= tt+i)
ok[j] = 1;
int cost= dijkstra();
if(cost < mincost)
mincost = cost;
}
printf("%d\n",mincost);
return 0;
}