首页 技术 正文
技术 2022年11月14日
0 收藏 401 点赞 4,527 浏览 1969 个字

最短路模板题 但是其实很费时间 因为要看明白dij floyd 以及 dij优化 spfa优化 交了三次 大概是理解了

不过涉及到priority_queue的重载运算符问题 以后要在C++里面好好看看 现在不理解

Dijkstra ver:

 #include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;
typedef pair<int, int> pii; int size, head[], point[], next[], val[];
int dis[], t, n; void add(int from, int to, int value)
{
point[size] = to;
next[size] = head[from];
val[size] = value;
head[from] = size++;
} struct cmp{
bool operator () (pii a, pii b){
return a.first > b.first;
}
}; void dijkstra(int s)
{
memset(dis, 0x3F, sizeof dis);
priority_queue<pii, vector<pii>, cmp> q;
q.push(make_pair(, s));
dis[s] = ;
while(!q.empty()){
pii u = q.top();
q.pop();
if(u.first > dis[u.second]) continue;
for(int i = head[u.second]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > u.first + val[i]){
dis[j] = u.first + val[i];
q.push(make_pair(dis[j], j));
}
}
}
} int main()
{
while(~scanf("%d%d", &t, &n)){
size = ;
memset(head, -, sizeof head);
for(int i = ; i <= t; i++){
int from, to, value;
scanf("%d%d%d", &from, &to, &value);
add(from, to, value);
add(to, from, value);
}
dijkstra();
printf("%d\n", dis[n]);
}
return ;
}

Spfa ver:

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#define INF 0x3F3F3F3F
using namespace std;int head[], point[], next[], val[], size;void add(int from, int to, int value)
{
point[size] = to;
val[size] = value;
next[size] = head[from];
head[from] = size++; point[size] = from;
val[size] = value;
next[size] = head[to];
head[to] = size++;
}void spfa(int s, int t)
{
int dis[];
bool vis[];
memset(dis, 0x3f, sizeof dis);
memset(vis, false, sizeof vis);
queue<int> q;
dis[s] = ;vis[s] = true;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; ~i; i = next[i]){
int j = point[i];
if(dis[j] > dis[u] + val[i]){
dis[j] = dis[u] + val[i];
if(!vis[j]){
q.push(j);
vis[j] = true;
}
}
}
}
printf("%d\n", dis[t]);
}int main()
{
int t, n;
memset(head, -, sizeof head);
scanf("%d%d", &t, &n);
while(t--){
int a, b, value;
scanf("%d%d%d", &a, &b, &value);
add(a, b, value);
}
spfa(, n);
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862