您的位置:首页 > 其它

最短路模板

2017-08-24 10:04 387 查看

1.bellman--ford

#include<cstdio>
#include<cstring>
#include<algorithm>
#define M 0x3f3f3f3f
using namespace std;
struct edge
{
int from,to,cost;
}es[2010];
int d[1010];
int t,n;
void shortpath(int s)
{
for(int i=1;i<=n;i++)
d[i]=M;
d[s]=0;
while(true)
{
bool up=false;
for(int i=0;i<t;i++)
{
edge e=es[i];
if(d[e.from]!=M&&d[e.to]>d[e.from]+e.cost)
{
d[e.to]=d[e.from]+e.cost;
up=true;
}
if(d[e.to]!=M&&d[e.from]>d[e.to]+e.cost)
{
d[e.from]=d[e.to]+e.cost;
up=true;
}
}
if(!up)
break;
}
}


2.dijkstra 算法

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M =2010;
const int inf =0x3f3f3f3f;
pair<int,int>p[M];
int cost[M][M];
bool used[M];
int d[M];
int t,n;
void dijkstra()
{
int k,mini;
for(int i=1;i<=n;i++)
d[i]=cost[1][i];
for(int i=2;i<=n;i++)
{
mini=inf;
for(int j=1;j<=n;j++)
{

if(!used[j]&&d[j]<mini)
{
mini=d[j];
k=j;
}
}
used[k]=true;
for(int u=1;u<=n;u++)
d[u]=min(d[u],d[k]+cost[k][u]);
}
}



3.spfa

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int M=4000;
const int inf=0x3f3f3f3f;
struct node
{
int now,to,next;
int v;
}e[M];
int head[M];
int vis[M];
int dis[M];
int cmp;
int t,n;
void init(int a,int b,int c)
{
e[cmp].to=b;
e[cmp].now=a;
e[cmp].next=head[a];
e[cmp].v=c;
head[a]=cmp++;
}
void spfa(int st)
{
memset(vis,0,sizeof(vis));
for(int i=i;i<=n;i++)
dis[i]=inf;
dis[st]=0;
vis[st]=1;
queue<int>q;
q.push(st);
while(q.size())
{
int x=q.front();
q.pop();
vis[x]=0;
for(int i=head[x];i!=-1;i=e[i].next)
{
int y=e[i].to;
if(dis[y]>dis[x]+e[i].v)
{
dis[y]=dis[x]+e[i].v;
if(!vis[y])
{
q.push(y);
vis[y]=1;
}
}
}
}
}


4.dijkstra+堆优化

typedef pair<int,int>P;
int dijkstra(int st,int e)
{
for(int i=1;i<=n;i++)
dis[i]=2100000000;
memset(vis,0,sizeof(vis));
dis[st]=0;
priority_queue<P,vector<P>,greater<P> >q;
q.push(P(dis[st],st));//
while(q.size())
{
P top=q.top();
q.pop();
int now=top.second;
if(vis[now])
continue;
vis[now]=1;
for(int j=1;j<=n;j++)
{
if((!vis[j])&&(a[now][j]+dis[now]<dis[j]))
{
dis[j]=a[now][j]+dis[now];
q.push(P(dis[j],j));
}
}
}
return dis[e];
}

5.dijkstra+堆优化+链式前向星+路径还原

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int M=1e5+100;
struct node
{
int to,next;
ll v;
}e[2*M];
int pre[M];
int head[M];
int res[M];
ll dis[M];
int n,m;
int cmp;
typedef pair<ll,int>P;
void init(int u,int v, int w)
{
e[cmp].to=v;
e[cmp].v=w;
e[cmp].next=head[u];
head[u]=cmp++;
}
void dijkstra(int st)
{
memset(dis,-1,sizeof(dis));
dis[st]=0;
priority_queue<P,vector<P>,greater<P> >q;
q.push(P(dis[st],st));
while(q.size())
{
P a;
while(q.size()&&q.top().first>dis[q.top().second])
q.pop();
if(q.empty())
break;
a=q.top();
b3d8
q.pop();
int aa=a.second;
for(int j=head[aa];j!=-1;j=e[j].next)
{
int y=e[j].to;
if(dis[y]==-1||dis[y]>dis[aa]+e[j].v)
{
dis[y]=dis[aa]+e[j].v;
q.push(P(dis[y],y));
pre[y]=aa;//记录路径
}
}
}

}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cmp=0;
memset(head,-1,sizeof(head));
memset(pre,0,sizeof(pre));
memset(res,0,sizeof(res));
int x,y,z;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
init(x,y,z);
init(y,x,z);
}
dijkstra(1);
if(dis
==-1)
printf("-1\n");
else
{
printf("%lld\n",dis
);
int now=n,cnt=0;
while(now!=1)
{
res[cnt++]=now;
now=pre[now];
}
res[cnt++]=1;
for(int i=cnt-1;i>0;i--)
printf("%d ",res[i]);
printf("%d\n",res[0]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: