(最短路 dijkstra)昂贵的聘礼 -- poj -- 1062
链接:
http://poj.org/problem?id=1062
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 41359 | Accepted: 12076 |
Description
为了方便起见,我们把所有的物品从1开始进行编号,酋长的允诺也看作一个物品,并且编号总是1。每个物品都有对应的价格P,主人的地位等级L,以及一系列的替代品Ti和该替代品所对应的"优惠"Vi。如果两人地位等级差距超过了M,就不能"间接交易"。你必须根据这些数据来计算出探险家最少需要多少金币才能娶到酋长的女儿。
Input
Output
Sample Input
1 4
10000 3 2
2 8000
3 5000
1000 2 1
4 200
3000 2 1
4 200
50 2 0
Sample Output
5250
dijkstra
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int INF = (<<)-;
#define maxn 205
#define MAXN 150005
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b) int M, N;
int dist[maxn], G[maxn][maxn], lv[maxn];
bool vis[maxn]; int Dij()
{
int i, j, k, ans = G[][]; for(k=lv[]-M; k<=lv[]; k++)
{
memset(vis, false, sizeof(vis));
for(i=; i<=N; i++)
{
dist[i] = G[][i]; if(lv[i]<k || lv[i]>k+M)
vis[i] = true;
} for(i=; i<=N; i++)
{
int index, Min=INF;
for(j=; j<=N; j++)
{
if(!vis[j] && dist[j]<Min)
{
Min = dist[j];
index = j;
}
}
vis[index] = true; for(j=; j<=N; j++)
{
if(!vis[j] && dist[j] > G[index][j] + dist[index])
dist[j] = G[index][j] + dist[index];
}
} ans = min(ans, dist[]);
}
return ans;
} int main()
{
while(scanf("%d%d", &M, &N)!=EOF)
{
int i, j, X, T, V; for(i=; i<=N; i++)
for(j=; j<=i; j++)
G[i][j] = G[j][i] = INF;
G[i][j] = ; for(i=; i<=N; i++)
{
scanf("%d%d%d", &G[][i], &lv[i], &X);
while(X--)
{
scanf("%d%d", &T, &V);
G[T][i] = V;
}
} printf("%d\n", Dij());
}
return ;
}
#include<stdio.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std; const int maxn = ;
const int maxm = ;
const int oo = 0xfffffff; struct node
{
int u, v, price, next;
}e[maxn];
int head[maxm], lv[maxm], p[maxm];
bool use[maxm]; void Add(int u, int v, int c, int k)
{
e[k].u = u;
e[k].v = v;
e[k].price = c;
e[k].next = head[u];
head[u] = k;
} int Dfs(int MaxLv, int MinLv, int k, int M)
{
int MinP = oo; for(int i=head[k]; i!=; i=e[i].next)
{
int v = e[i].v;
int c = e[i].price; if(lv[v] >= MinLv && lv[v] <= MaxLv && use[v] == false)
{
use[v] = true; int L = min(MaxLv, lv[v]+M);
int R = max(MinLv, lv[v]-M); MinP = min(MinP, Dfs(L, R, v, M)+c); use[v] = false;
}
} return min(p[k], MinP);
} int main()
{
int N, M; while(scanf("%d%d", &M, &N) != EOF)
{
int i, k=, v, price, T; memset(head, , sizeof(head)); for(i=; i<=N; i++)
{
scanf("%d%d%d", &p[i], &lv[i], &T); while(T--)
{
scanf("%d%d", &v, &price);
Add(i, v, price, k++);
}
} use[] = true;
int ans = Dfs(lv[]+M, lv[]-M, , M); printf("%d\n", ans);
} return ;
}
(最短路 dijkstra)昂贵的聘礼 -- poj -- 1062的更多相关文章
- 昂贵的聘礼 POJ - 1062(最短路)
年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:" ...
- 昂贵的聘礼 - poj 1062 (Dijkstra+枚举)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39976 Accepted: 11596 Description 年 ...
- 昂贵的聘礼 POJ - 1062
题目链接:https://vjudge.net/problem/POJ-1062 如图,我们可以把交换的情况,抽象为一个有向图, 先抛去等级限制,那么就是一个最短路,从①出发,到达其他点的最短路中 最 ...
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- POJ - 1062 昂贵的聘礼(最短路Dijkstra)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...
- 最短路POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 1062 昂贵的聘礼(带限制条件的dijkstra)
题目网址:http://poj.org/problem?id=1062 题目: 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- POJ 1062 昂贵的聘礼 (最短路)
昂贵的聘礼 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/M Description 年轻的探险家来到了一个印第安部落里.在那里 ...
随机推荐
- UEFI BIOS和普通BIOS的区别
作为传统BIOS(Basic Input/Output System)的继任者,UEFI拥有前辈所不具备的诸多功能,比如图形化界面.多种多样的操作方式.允许植入硬件驱动等等.这些特性让UEFI相比于传 ...
- InitComponent的使用
网页中的数据,有些是不在网页上改变的,像一些个人信息,比如:头像,当前用户名,友情链接等等,每次请求该页面都要重新加载,这样很消耗服务器资源,会降低服务器的性能,这个时候我们可以把这些不变的信息,统一 ...
- dapper.net 转载
Dapper.NET——轻量ORM Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2 ...
- JavaScript eval_r() 函数
定义和用法 eval_r() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval_r(string) 参数 描述 string 必需.要计算的字符串,其中含有要计算的 ...
- go递归函数如何传递数组切片slice
数组切片slice这个东西看起来很美好,真正用起来会发现有诸多的不爽. 第一,数组.数组切片混淆不清,使用方式完全一样,有时候一些特性又完全不一样,搞不清原理很容易误使用. 第二,数组切片的appen ...
- Bioconductor的历史
---------------------------------------------------------------Bioconductor------------------------- ...
- 获取Activity中得到焦点的EditText
Android Activity中获取当前焦点的控件,自动化输入EditText 获取焦点的view对象 View view=getWindow().getDecorView().findFocus( ...
- hdoj2612 Find a way (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 思路: 这个题我wa了十多发QAQ. 刚开始的思路是搜索每个‘@’,然后广搜该点到Y和M的最小距 ...
- 第五章 二叉树(d)二叉树实现
- cdoj第13th校赛初赛F - Fabricate equation
http://acm.uestc.edu.cn/#/contest/show/54 F - Fabricate equation Time Limit: 3000/1000MS (Java/Other ...