1. 程式人生 > >1030 Travel Plan [Dijkstra]

1030 Travel Plan [Dijkstra]

none esp click img play view oid true namespace

最短路板題~想看dijkstra可以看一下另一篇。傳送門https://www.cnblogs.com/FTA-Macro/p/10499725.html

技術分享圖片
#include <bits/stdc++.h>
#define maxn 505
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int n,m,u,v,w,c,st,en;
int vis[maxn],dis[maxn],co[maxn],path[maxn];
int ma[maxn][maxn],cost[maxn][maxn];
void dijkstra(int s) { memset(vis,0,sizeof(vis)); memset(dis,INF,sizeof(dis)); memset(co,INF,sizeof(co)); dis[s]=0; co[s]=0; path[s]=-1; while(true) { int u=-1,minn=INF; for(int i=0;i<n;i++) { if(!vis[i]&&dis[i]<minn) { u
=i; minn=dis[i]; } } if(u==-1) break; vis[u]=1; for(int i=0;i<n;i++) { if(dis[i]>dis[u]+ma[u][i]) { dis[i]=dis[u]+ma[u][i]; co[i]=co[u]+cost[u][i]; path[i]
=u; } else if(dis[i]==dis[u]+ma[u][i]) { if(co[i]>co[u]+cost[u][i]) { co[i]=co[u]+cost[u][i]; path[i]=u; } } } } } void display(int x) { if(path[x]==-1) { printf("%d ",x); return; } display(path[x]); printf("%d ",x); return; } int main() { memset(ma,INF,sizeof(ma)); memset(cost,INF,sizeof(cost)); scanf("%d %d %d %d",&n,&m,&st,&en); for(int i=0;i<m;i++) { scanf("%d%d%d%d",&u,&v,&w,&c); ma[u][v]=ma[v][u]=w; cost[u][v]=cost[v][u]=c; } dijkstra(st); display(en); printf("%d %d\n",dis[en],co[en]); return 0; }
View Code

1030 Travel Plan [Dijkstra]