poj 1062 昂贵的聘礼(最短路 dijk+枚举)
终于A 了,这题做着真麻烦
题目:http://poj.org/problem?id=1062
dijk 一般用于正权有向图
此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等级限制为2,那么需要枚举等级从3~5,4~6,5~7
题意就不用说了,做poj以来的第一道中文题目。
要考虑间接身份差异不可行的情况
如:1 4
10000 3 2
2 1
3 3
1000 2 2
4 1
3 1
1000 3 1
4 2
100 4 0
错误程序出104,答案105。
对于这组数据错误的程序是4->3->2->1的,但4和2不能并存
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std; int lim[];
const int INF=<<;
int n;
struct no
{
int p,l,x,a[],b[];
} thing[]; struct node
{
int u,v,w,next;
} edge[];
int head[],dis[],cnt;
void add(int u,int v,int w)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
} int dijkstra(int s)
{
int vis[],i,j,min;
for(i=; i<=n; i++)
dis[i]=INF;
memset(vis,,sizeof(vis));
dis[s]=;
for(i=; i<=n; i++)
{
int minn=INF;
int u=s;
for(j=; j<=n; j++)
{
if(!vis[j] && minn>dis[j] &&lim[j]) //lim标记是否在限制条件内
{
minn=dis[j];
u=j;
}
}
for(j=head[u]; j!=-; j=edge[j].next)
{
int v=edge[j].v;
int newdis=minn+edge[j].w;
if(!vis[v] && newdis < dis[v] && lim[v])//lim标记是否在限制条件内
dis[v]= newdis;
}
vis[u]=;
}
min=thing[].p;
for(i=; i<=n; i++) //一组完成后 比较
{
if(dis[i]+thing[i].p<min)
min=dis[i]+thing[i].p;
}
return min;
}
int main()
{
int m,i,j,ans;
scanf("%d%d",&m,&n);
cnt=;
memset(head,-,sizeof(head));
for(i=; i<=n; i++)
{
scanf("%d%d%d",&thing[i].p,&thing[i].l,&thing[i].x);
for(j=; j<=thing[i].x; j++)
{
scanf("%d%d",&thing[i].a[j],&thing[i].b[j]);
}
}
for(i = ; i <= n; i++) //其实我这一步没什么必要,当时想少了,下一步就可以搞定
{
for(j=; j<=thing[i].x; j++)
{
if(abs(thing[i].l-thing[thing[i].a[j]].l)<=m)
add(i,thing[i].a[j],thing[i].b[j]);//倒着建的图
}
}
ans=thing[].p;
for(i=m; i>=; i--)
{
memset(lim,,sizeof(lim));
for(j=; j<=n; j++) //枚举
{
if(thing[j].l>=(thing[].l-i)&&thing[j].l<=(thing[].l+m-i))
lim[j]=;
}
if(dijkstra()<ans)
ans=dijkstra();
}
printf("%d\n",ans);
return ;
}
poj 1062 昂贵的聘礼(最短路 dijk+枚举)的更多相关文章
- POJ - 1062 昂贵的聘礼(最短路Dijkstra)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Descr ...
- POJ 1062 昂贵的聘礼 最短路 难度:0
http://poj.org/problem?id=1062 #include <iostream> #include <cstring> #include <queue ...
- POJ 1062 昂贵的聘礼 最短路+超级源点
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...
- poj 1062 昂贵的聘礼 最短路 dijkstra
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...
- 最短路(Dijkstra) POJ 1062 昂贵的聘礼
题目传送门 /* 最短路:Dijkstra算法,首先依照等级差距枚举“删除”某些点,即used,然后分别从该点出发生成最短路 更新每个点的最短路的最小值 注意:国王的等级不一定是最高的:) */ #i ...
- POJ 1062 昂贵的聘礼(图论,最短路径)
POJ 1062 昂贵的聘礼(图论,最短路径) Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女 ...
- poj 1062 昂贵的聘礼 (dijkstra最短路)
题目链接:http://poj.org/problem?id=1062 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- 最短路POJ 1062 昂贵的聘礼
C - 昂贵的聘礼 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 1062 昂贵的聘礼 (最短路)
昂贵的聘礼 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/M Description 年轻的探险家来到了一个印第安部落里.在那里 ...
- poj 1062 昂贵的聘礼 (有限制的最短路)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56594 Accepted: 17083 Descripti ...
随机推荐
- ACE_linux:任务 & 命令(Task and Command)
1.涉及类 ACE_Task//ACE任务ACE_Activation_Queue//ACE命令队列 ACE_Method_Request//ACE请求(命令) 2.简介 ACE主动对象模式 主动对象 ...
- 1078. Hashing (25)
时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of this problem is simp ...
- [ 转载]JAVA Socket超时浅析
JAVA Socket超时浅析 转载自 http://blog.csdn.net/sureyonder/article/details/5633647 套接字或插座(socket)是一种软件形 式的抽 ...
- shutdown彻底关闭tomcat,以及多线程关闭
最近做的一个Web项目,发现shutdown.sh后,无法关掉tomcat进程. ps -ef | grep tomcat 返回tomcat进程仍然存在.经过调查发现是因为在Web应用中启动了线程池, ...
- Mapped Statements collection does not contain value for TaskMapper.selectByPrimaryKey
Mapped Statements collection does not contain value for后面是什么类什么方法之类的: 错误原因有几种: 1.mapper.xml中没有加入name ...
- python学习笔记24(路径与文件 (os.path包, glob包))
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. >>> import os.path >>> path = '/home/ ...
- Angular指令封装jQuery日期时间插件datetimepicker实现双向绑定
一放假就高产似母猪了. 00.混乱的前端界 Angular1.x确实是个学习成本很高的框架,刚开始实习那会儿,前端啥也不懂,工头说用Angular,我们这群小弟也只能硬着头皮学.在这之前,前端的东西大 ...
- jquery bind()方法与live()方法的区别
jquery bind() 方法和 live() 方法都可以绑定元素事件. <!DOCTYPE html> <html> <head> <meta chars ...
- POJ - 1741 Tree
DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...
- 图片流滚动效果html代码(复制)
<!doctype html> <html> <head> <meta charset="utf-8" /> < ...