最短路模板,Dijkstra,spfa算法,没有Floyd算法的,模板
最小生成树算法只有prim没有克鲁斯卡尔算法:
* 一点点的说明: 代码中引入了二元组:pair(目的辅助算法的实现,存储部分相关信息),以及优先队列priority_queue(实现快速查找,即所谓的堆排序,返回现有里的最小值) *
总代码: 题目: sdutacm 2143 2144 :
所需要的一点东西:
Ps :head[]数组初始化在主函数中
#include <bits/stdc++.h>
using namespace std;
const int maxn =1e6+5; //数组的最大内存
const int INF =0x3f3f3f; //定义的无限大
bool vis[maxn]; //标记数组,prim,spfa算法用
typedef pair <int ,int >qq; //定义的二元组 (dist[],pos),frist是对应的dist[]值,pos表示位置
int head[maxn]; //存储图的边的信息
struct node //存储图的信息
{
int to,next,data;
}e[maxn];
int dist[maxn];//最短距离
int cnt ; //存储图
int n,m;//节点n,边数m
int num[maxn];//判断负环 spfa算法
void add (int x,int y,int z) //添加边
{
e[cnt].to=y;
e[cnt].data=z;
e[cnt].next=head[x];
head[x]=cnt++;
}
Dijkstra算法:
void Dijkstra(int st)
{
fill(dist,dist+n+1,INF);
priority_queue<qq,vector<qq>,greater<qq> >q;
q.push(qq(0,st));
int i,j,k;
dist[st]=0;
while (!q.empty())
{
qq p=q.top();
q.pop();
int u =p.second;
if (dist[u]<p.first)
continue ;
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>ee.data+dist[u])
{
dist[ee.to]=ee.data+dist[u];
q.push(qq(dist[ee.to],ee.to));
}
}
}
}
Spfa算法:
bool yes; //判断负环
void spfa(int st)
{
fill(vis,vis+n+1,0);
fill(dist,dist+n+1,INF);
queue<int >q;
q.push(st);
dist[st]=0;
vis[st]=1;
int i,j,k;
yes =0;
while (!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>dist[u]+ee.data)
{
dist[ee.to]=dist[u]+ee.data;
if (!vis[ee.to])
{
vis[ee.to]=1;
num[ee.to]++;
if (num[ee.to]>n)
{
yes = 1 ;
return ;
}
q.push(ee.to);
}
}
}
}
}
Prim 算法,最小生成树:
void prim(int st)
{
int i,j,k;
fill(vis,vis+n+1,0);
fill(dist,dist+n+1,INF);
int ans =0;
priority_queue<qq,vector<qq>,greater<qq> >q;
q.push(qq(0,st));
dist[st]=0;
while (!q.empty())
{
qq p=q.top();
q.pop();
int u=p.second;
if (vis[u])
continue ;
vis[u]=1;
ans+=dist[u];
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>ee.data)
{
dist[ee.to]=ee.data;
q.push(qq(ee.data,ee.to));
}
}
}
cout<<ans<<endl;
}
全部代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn =1e6+5;
const int INF =0x3f3f3f;
bool vis[maxn];
typedef pair <int ,int >qq;
int head[maxn];
struct node
{
int to,next,data;
}e[maxn];
int dist[maxn];
int cnt ;
int n,m;
int num[maxn];
void add (int x,int y,int z)
{
e[cnt].to=y;
e[cnt].data=z;
e[cnt].next=head[x];
head[x]=cnt++;
}
void Dijkstra(int st)
{
fill(dist,dist+n+1,INF);
priority_queue<qq,vector<qq>,greater<qq> >q;
q.push(qq(0,st));
int i,j,k;
dist[st]=0;
while (!q.empty())
{
qq p=q.top();
q.pop();
int u =p.second;
if (dist[u]<p.first)
continue ;
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>ee.data+dist[u])
{
dist[ee.to]=ee.data+dist[u];
q.push(qq(dist[ee.to],ee.to));
}
}
}
}
bool yes; //判断负环
void spfa(int st)
{
fill(vis,vis+n+1,0);
fill(dist,dist+n+1,INF);
queue<int >q;
q.push(st);
dist[st]=0;
vis[st]=1;
int i,j,k;
yes =0;
while (!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>dist[u]+ee.data)
{
dist[ee.to]=dist[u]+ee.data;
if (!vis[ee.to])
{
vis[ee.to]=1;
num[ee.to]++;
if (num[ee.to]>n)
{
yes = 1 ;
return ;
}
q.push(ee.to);
}
}
}
}
}
void prim(int st)
{
int i,j,k;
fill(vis,vis+n+1,0);
fill(dist,dist+n+1,INF);
int ans =0;
priority_queue<qq,vector<qq>,greater<qq> >q;
q.push(qq(0,st));
dist[st]=0;
while (!q.empty())
{
qq p=q.top();
q.pop();
int u=p.second;
if (vis[u])
continue ;
vis[u]=1;
ans+=dist[u];
for (i=head[u];i!=-1;i=e[i].next)
{
node ee =e[i];
if (dist[ee.to]>ee.data)
{
dist[ee.to]=ee.data;
q.push(qq(ee.data,ee.to));
}
}
}
cout<<ans<<endl;
}
int main()
{
while (cin>>n>>m)
{
int i,j,k;
cnt=0;
fill(head,head+maxn+1,-1);
while(m--)
{
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}
// Dijkstra(1);
spfa(1);
cout<<dist[n]<<endl;
}
return 0;
}