poj-3159【差分约束+Dijkstra算法的优化】
来源:互联网 发布:淘宝产品图片尺寸留白 编辑:程序博客网 时间:2024/06/05 01:53
昨天看图论的时候找到了这么个题目,刚开始不知道查分约束是什么东西,后来在网上学了一下,知道就是求图的最短路径额。。。。。这东西需要好好看看了。
再看这个题目,题意就是给出两点的最大差值,求出1到n的最大差值,刚开始用的一般的方法这个问题超时了,所以用的优先队列,最后ac了。。。(简简单单两行字,这里面全是泪额。。。。。)
上代码。。。。。
#include <iostream>#include <cstdio>#include <queue>#include <vector>#include <cstring>#define Max 0x3f3f3fusing namespace std;typedef pair<int,int> P;int d[30005];int head[30005];int n,m;int num1,num2,num3;int top;struct node{ int value; int to; int next;}x[150010];void add(int a,int b,int c){ x[top].to=b; x[top].value=c; x[top].next=head[a]; head[a]=top++;}void Init(){ top=0; for (int i=1;i<=n;i++) { head[i]=-1; d[i]=Max; }}void Diskj(){ d[1]=0; priority_queue<P,vector<P>,greater<P> >que; que.push(P(0,1)); int first,last; while(que.size()) { P p=que.top(); que.pop(); first=p.first; last=p.second; if (d[last]<first)continue; for (int i=head[last];i!=-1;i=x[i].next) { if (d[x[i].to]>x[i].value+d[last]) { d[x[i].to]=x[i].value+d[last]; que.push(P(d[x[i].to],x[i].to)); } } } int z=d[n]; printf("%d\n",z);}int main(){ scanf("%d %d",&n,&m); { Init(); while(m--) { scanf("%d %d %d",&num1,&num2,&num3); add(num1,num2,num3); } Diskj(); }}
0 0
- poj-3159【差分约束+Dijkstra算法的优化】
- poj 3159 Candies (差分约束 先队列优化Dijkstra 链式前向星存储)
- POJ 3159 Candies(差分约束+dijkstra+heap)
- poj 3159 差分约束
- POJ 3159 差分约束
- poj 3159 差分约束
- POJ 3159 Candies (差分约束 Dijkstra+优先队列 SPFA+栈)
- POJ - 3159 不卡建图,几乎赤果果的差分约束+SPFA..
- POJ 3159 Candies(差分约束系统)
- 【POJ 3159】 Candies 差分约束系统
- POJ 3159 Candies 差分约束+spfa
- POJ--3159[Candies] 差分约束
- poj 3159 Candies 差分约束 + spfa
- poj-3159-Candies-差分约束+stack
- POJ 3159 Candies 差分约束系统
- poj 3159 Candies 差分约束
- poj 3159(差分约束经典题)
- POJ 3159 Candies 差分约束
- android studio 记录
- 基本注意事项2
- C#学习之方法
- 剑指offer-算法题练习:part19 树的子结构
- Python 抽象
- poj-3159【差分约束+Dijkstra算法的优化】
- 排序总结系列二:直接插入排序
- 双向链表的Java实现,以及相关函数的实现
- 【工控安全产品】工业控制系统信息安全检查工具箱
- [Android]XML和JSON的区别
- Zhu and 772002
- 某个个人公众号阅读量和点赞数分析
- Http协议详解
- 在ubuntu中添加widows启动项的简单方法