最短路 dijkstra 优先队列
1.裸题 hdu2544
http://acm.hdu.edu.cn/showproblem.php?pid=2544
Way1:
好像不对
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stdbool.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 100+5
#define inf 1e9 long dist[maxn],road[maxn][maxn];
bool vis[maxn];
struct cmp
{
bool operator() (long a,long b)
{
return dist[a]>dist[b];
}
};
priority_queue<long,vector<long>,cmp> f; int main()
{
long n,m,i,j,x,y,z,d;
while ()
{
scanf("%ld%ld",&n,&m);
if (n== && m==)
break;
for (i=;i<=n;i++)
for (j=;j<=n;j++)
road[i][j]=inf;
for (i=;i<=m;i++)
{
scanf("%ld%ld%ld",&x,&y,&z);
road[x][y]=min(road[x][y],z);
road[y][x]=road[x][y];
}
for (i=;i<=n;i++)
{
vis[i]=false;
dist[i]=inf;
}
dist[]=;
//pay attention!
while (!f.empty())
f.pop();
f.push();
for (j=;j<n;j++) //use 'i' wrong!
{
while (vis[f.top()])
f.pop();
d=f.top();
if (d==n)
break;
vis[d]=true;
f.pop(); for (i=;i<=n;i++)
if (!vis[i] && dist[i]>dist[d]+road[i][d])
{
dist[i]=dist[d]+road[i][d];;
f.push(i);
}
}
printf("%ld\n",dist[n]);
}
return ;
}
/*
6 7
1 2 10
1 2 3
1 3 1
2 4 3
4 5 1
5 6 10
2 5 1
*/
Way2:
自行写堆,让堆中的值减小(路径长度减小),在堆中上升。
只要掌握了合理的写法,其实也不是特别不好理解和难写
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <stdbool.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 10000+5
#define inf 1e9 //pos[i]:编号为i的城市到起点的距离 在 树中的位置
long dist[maxn],road[maxn][maxn],tree[maxn],pos[maxn],g=;
bool vis[maxn]; //minimum-heap
void up(long i)
{
long j,temp;
while (i>)
{
j=i>>;
//i>j
if (dist[tree[i]]<dist[tree[j]])
{
temp=tree[i];
tree[i]=tree[j];
tree[j]=temp;
pos[tree[i]]=i;
pos[tree[j]]=j;
}
else
break;
i=j;
}
} void down(long i)
{
long j,temp;
while ((i<<)<=g)
{
j=i<<;
if (dist[tree[j+]]<dist[tree[j]])
j++;
//i<j
if (dist[tree[i]]>dist[tree[j]])
{
temp=tree[i];
tree[i]=tree[j];
tree[j]=temp;
pos[tree[i]]=i;
pos[tree[j]]=j;
}
else
break;
i=j;
}
} int main()
{
long n,m,i,j,x,y,z,d;
while ()
{
scanf("%ld%ld",&n,&m);
if (n== && m==)
break;
for (i=;i<=n;i++)
for (j=;j<=n;j++)
road[i][j]=inf;
for (i=;i<=m;i++)
{
scanf("%ld%ld%ld",&x,&y,&z);
road[x][y]=min(road[x][y],z);
road[y][x]=road[x][y];
}
for (i=;i<=n;i++)
{
vis[i]=false;
dist[i]=-;
}
dist[]=;
g=;
tree[]=;
pos[]=;
for (j=;j<n;j++) //use 'i' wrong!
{
d=tree[];
if (d==n)
break;
tree[]=tree[g];
pos[tree[]]=;
g--;
down(); vis[d]=true;
for (i=;i<=n;i++)
if (!vis[i])
{
if (dist[i]==-)
{
dist[i]=dist[d]+road[i][d];
g++;
tree[g]=i;
pos[i]=g;
up(g);
}
else if (dist[i]>dist[d]+road[i][d])
{
dist[i]=dist[d]+road[i][d];
up(pos[i]);
}
}
}
printf("%ld\n",dist[n]);
}
return ;
}
/*
6 7
1 2 10
1 2 3
1 3 1
2 4 3
4 5 1
5 6 10
2 5 1
*/
2.多关键字
L3-011. 直捣黄龙
https://www.patest.cn/contests/gplt/L3-011
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stdbool.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 200+5
#define inf 1e9 struct node
{
long kill,dist,free,count;
}point[maxn]; struct cmp
{
bool operator() (long a,long b)
{
return point[a].dist>point[b].dist;
}
};
priority_queue<long,vector<long>,cmp> f; long road[maxn][maxn],kill[maxn],pre[maxn],n;
char s[maxn][];
bool vis[maxn]; long find(char ss[])
{
long i;
for (i=;i<=n;i++)
if (strcmp(s[i],ss)==)
return i;
} void print(long d)
{
if (pre[d]!=-)
{
print(pre[d]);
printf("->%s",s[d]);
}
else
printf("%s",s[d]);
} int main()
{
char s1[],s2[];
long m,i,j,d,x,y,e;
scanf("%ld%ld%s%s",&n,&m,s1,s2);
for (i=;i<=n;i++)
{
point[i].dist=inf;
vis[i]=false;
}
for (i=;i<=n;i++)
for (j=;j<=n;j++)
road[i][j]=inf+;
point[n].kill=;
strcpy(s[n],s1);
pre[n]=-;
point[n].dist=;
point[n].free=;
point[n].count=;
f.push(n);
for (i=;i<n;i++)
{
scanf("%s%ld",s[i],&kill[i]);
if (strcmp(s2,s[i])==)
e=i;
}
for (i=;i<=m;i++)
{
scanf("%s%s%ld",s1,s2,&d);
x=find(s1);
y=find(s2);
road[x][y]=min(road[x][y],d);
road[y][x]=road[x][y];
}
for (j=;j<n;j++)
{
while (vis[f.top()])
f.pop();
d=f.top();
if (d==e)
break;
vis[d]=true;
for (i=;i<=n;i++)
if (!vis[i])
{
if (point[i].dist>point[d].dist+road[i][d])
{
pre[i]=d;
point[i].count=point[d].count;
point[i].dist=point[d].dist+road[i][d];
point[i].free=point[d].free+;
point[i].kill=point[d].kill+kill[i];
f.push(i);
}
else if (point[i].dist==point[d].dist+road[i][d])
{
point[i].count+=point[d].count;
if ((point[i].free<point[d].free+) ||
(point[i].free==point[d].free+ && point[i].kill<point[d].kill+kill[i]))
{
pre[i]=d;
point[i].free=point[d].free+;
point[i].kill=point[d].kill+kill[i];
f.push(i);
}
}
}
}
print(e);
printf("\n%ld %ld %ld\n",point[e].count,point[e].dist,point[e].kill);
return ;
}
最短路 dijkstra 优先队列的更多相关文章
- Codeforces Gym101502 I.Move Between Numbers-最短路(Dijkstra优先队列版和数组版)
I. Move Between Numbers time limit per test 2.0 s memory limit per test 256 MB input standard inpu ...
- HDU 1874-畅通project续(最短路Dijkstra+优先队列)
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 最短路--dijkstra+优先队列优化模板
不写普通模板了,还是需要优先队列优化的昂 #include<stdio.h> //基本需要的头文件 #include<string.h> #include<queue&g ...
- POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...
- 最短路 dijkstra+优先队列+邻接表
http://acm.hdu.edu.cn/showproblem.php?pid=2544 #include<iostream> #include<queue> #inclu ...
- 【poj 1724】 ROADS 最短路(dijkstra+优先队列)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- 单源最短路dijkstra算法&&优化史
一下午都在学最短路dijkstra算法,总算是优化到了我能达到的水平的最快水准,然后列举一下我的优化历史,顺便总结总结 最朴素算法: 邻接矩阵存边+贪心||dp思想,几乎纯暴力,luoguTLE+ML ...
- 【bzo1579】拆点+dijkstra优先队列优化+其他优化
题意: n个点,m条边,问从1走到n的最短路,其中有K次机会可以让一条路的权值变成0.1≤N≤10000;1≤M≤500000;1≤K≤20 题解: 拆点,一个点拆成K个,分别表示到了这个点时还有多少 ...
随机推荐
- TensorFlow中的卷积函数
前言 最近尝试看TensorFlow中Slim模块的代码,看的比较郁闷,所以试着写点小的代码,动手验证相关的操作,以增加直观性. 卷积函数 slim模块的conv2d函数,是二维卷积接口,顺着源代码可 ...
- FileZilla-FTP连接失败
状态: 已登录状态: 读取“/”的目录列表...命令: CWD /响应: 250 CWD successful. "/" is current directory.命令: TYPE ...
- Homebrew1.5之后安装PHP和扩展
Homebrew 1.5 宣布放弃 homebrew/php, 转而使用homebrew/core维护, 详见https://brew.sh/2018/01/19/homebrew-1.5.0/ 于是 ...
- Python字典 (dict)
作者博文地址:http://www.cnblogs.com/spiritman/ 字典是Python语言中唯一的映射类型.字典对象是可变的,它是一个容器类型,支持异构.任意嵌套. 创建字典 语法:{k ...
- [ c++] cmake 编译时 undefined reference to `std::cout' 错误的解决方案
cmake .. 和 make 之后,出现如下错误 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/ModuleTest.dir/ ...
- 猫咪记单词——NABCD模型分析
N ——Need 需求:学习英语是一件非常重要的事.面对各种各样的考试,学习英语,最重要的就是词汇量,背单词是提高词汇量的最直接的方法,但是单纯的背单词太单调.寻找一些合适的,更易于接受的背单词学习英 ...
- struts2 Action生命周期
Struts2.0中的对象既然都是线程安全的,都不是单例模式,那么它究竟何时创建,何时销毁呢? 这个和struts2.0中的配置有关,我们来看struts.properties ### if spec ...
- 【Python】LeetCode-155
一.题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- quartusII13.0使用教程
1.新建工程项目,填写项目存储路径和工程名,不要出现中文路径 2.添加已存在文件(可选),在[File name]下选择已经存在的工程项目,利用[Add]或[Add all]命令添加文件到新工程,点击 ...
- 项目冲刺Beta第二篇博客
Beta版本冲刺计划安排 1.当天站立式会议照片: 2.工作分工: 团队成员 分工 张洪滨060 排行榜界面美化 陈敬轩059 注册成功界面美化 黄兴067 登录界面美化 林国梽068 答题界 ...