HDU-2544 最短路(dijkstra 邻接矩阵存图、链式前向星存图、链式前向星+优先队列)
作者:互联网
关于链式前向星的博客:https://blog.csdn.net/acdreamers/article/details/16902023
链式前向星+优先队列
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 1e4+5;
const int Inf = 0x3f3f3f3f;
int vis[N],dis[N];
int Head[N];
int n,m,cnt;
struct node{
int to,w;
int Next;
bool operator < (const node &a) const{
return a.w < w;
}
} Edge[N],Now,Next;
void init() {
memset(Head,-1,sizeof(Head));
cnt = 0;
}
void Add(int u,int v,int w){
Edge[cnt].to = v;
Edge[cnt].w = w;
Edge[cnt].Next = Head[u];
Head[u] = cnt ++;
}
void dijkstra(int s){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s] = 0;
// vis[s] = 1;
priority_queue<node> q;
Now.to = s;
q.push(Now);
while(!q.empty()){
Now = q.top();
q.pop();
s = Now.to;
if(vis[s]) continue;
vis[s] = 1;
for(int i=Head[s]; ~i; i = Edge[i].Next){
int v = Edge[i].to;
if(!vis[v] && dis[v] > dis[s] + Edge[i].w){
dis[v] = dis[s] + Edge[i].w;
Next.to = v;
Next.w = dis[v];
q.push(Next);
}
}
}
printf("%d\n",dis[n]);
}
int main(){
while(~scanf("%d%d",&n,&m),(n && m)){
init();
for(int i=0;i<m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
dijkstra(1);
}
return 0;
}
链式前向星存图
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int N = 1e4+10;
const int Inf = 0x3f3f3f3f;
int Head[N];
int vis[N],dis[N];
int n,m,cnt;
struct node{
int to;
int w;
int Next;
} Edge[N];
void init(){
memset(Head,-1,sizeof(Head));
cnt = 0;
}
void Add(int u,int v,int w){
Edge[cnt].to = v;
Edge[cnt].w = w;
Edge[cnt].Next = Head[u];
Head[u] = cnt++;
}
void dijkstra(int s){
memset(dis,Inf,sizeof(dis));
memset(vis,0,sizeof(vis));
vis[s] = 1;
dis[s] = 0;
for(int i = Head[s]; ~i; i = Edge[i].Next){
int v = Edge[i].to, w = Edge[i].w;
dis[v] = w;
}
for(int i=1;i<n;i++){
int Min = Inf;
for(int j=1;j<=n;j++){
if(!vis[j] && Min > dis[j]){
Min = dis[j];
s = j;
}
}
vis[s] = 1;
for(int j=Head[s]; ~j ;j = Edge[j].Next){
int v = Edge[j].to, w = Edge[j].w;
if(!vis[v] && dis[v] > dis[s] + w){
dis[v] = dis[s] + w;
}
}
}
printf("%d\n", dis[n]);
}
int main(){
while(~scanf("%d%d",&n,&m),(n && m)){
init();
int u,v,w;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
Add(u,v,w);
Add(v,u,w);
}
dijkstra(1);
}
return 0;
}
邻接矩阵存图:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 1e3+10;
const int Inf = 0x3f3f3f3f;
int Map[N][N],dis[N],vis[N];
int n,m;
int u,v,w;
void init(){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i == j) Map[i][j] = 0;
else Map[i][j] = Inf;
}
}
}
void dijkstra(){
int s,Min;
for(int i=1;i<=n;i++){
dis[i] = Map[1][i];
}
memset(vis,0,sizeof(vis));
vis[1] = 1;
for(int i=1;i<n;i++){
Min = Inf;
for(int j=1;j<=n;j++){
if(!vis[j] && Min > dis[j]){
Min = dis[j];
s = j;
}
}
vis[s] = 1;
for(int j=1;j<=n;j++){
if(!vis[j] && dis[j] > dis[s] + Map[s][j]){
dis[j] = dis[s] + Map[s][j];
}
}
}
printf("%d\n",dis[n]);
}
int main(){
while(~scanf("%d%d",&n,&m),(n && m)){
init();
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
if(Map[u][v] > w){
Map[u][v] = Map[v][u] = w;
}
}
dijkstra();
}
return 0;
}
标签:Head,int,cnt,vis,Edge,存图,链式,前向星,dis 来源: https://blog.csdn.net/qq_16554583/article/details/88736690