PAT A1030 Travel Plan (30分)【Dijkstra模板题】

今天也是为了cc,努力奋斗的一天ヾ(≧▽≦*)o

疑问

暂无

代码

#include<stdio.h>
#include<vector>
#include<algorithm>

using namespace std;

//定义常量
const int MAXV = 510;
const int INF = 1000000000;

//顶点数
int n; 
//起点
int s;
//终点
int t;
//路径数组
int G[MAXV][MAXV];
//最短路径数组
int d[MAXV];
//边权cost数组 
int cost[MAXV][MAXV];
//s是否已访问数组
bool vis[MAXV] = {false};
//最小花费数组
int c[MAXV];
//前驱节点数组
int pre[MAXV]; 

void DFS(int v){
	if(v == s){
		printf("%d ",s);
		return;
	}
	else{
		DFS(pre[v]);
		printf("%d ",v);
		return;
	}
}

void Dijkstra(){
	//一些初始化操作
	fill(d,d+MAXV,INF);
	fill(c,c+MAXV,INF);
	fill(pre,pre+MAXV,-1);
	
	d[s] = 0;
	c[s] = 0;
	
	for(int i=0;i<n;i++){
		int u=-1;
		int MIN=INF;
		
		for(int j=0;j<n;j++){
			if(vis[j] == false && d[j] < MIN){
				u = j;
				MIN = d[j];
			}
		}
		
		vis[u] = true;
		
		for(int v=0;v<n;v++){
			if(vis[v] == false && G[u][v] != INF){
				if(d[u] + G[u][v] < d[v]){
					d[v] = d[u] + G[u][v];
					c[v] = c[u] + cost[u][v];
					pre[v] = u;
				}else if(d[u] + G[u][v] == d[v]){
					if(c[u] + cost[u][v] < c[v]){
						c[v] = c[u] + cost[u][v];
						pre[v] = u;
					}
				}
 			}
		}
	}
	
	DFS(t);
	
	printf("%d %d",d[t],c[t]);
	
}

int main(){
	
	fill(G[0],G[0]+MAXV*MAXV,INF);
	
	//路径数
	int m;
	
	scanf("%d %d %d %d",&n,&m,&s,&t);
	
	for(int i=0;i<m;i++){
		int a,b,c,d;
		scanf("%d %d %d %d",&a,&b,&c,&d);
		
		G[a][b] = c;
		G[b][a] = c;
		
		cost[a][b] = d;
		cost[b][a] = d;
	}	
	
	Dijkstra();
	return 0;
} 

反思

  1. 感觉还行,希望机试遇到的话可以快速写出来。可是,我这记忆力,可能还要再回来看一遍的。

二刷代码

//Dikstra模板题 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 550;
const int inf = 0x3fffffff;

struct node{
	int v;
	int cost;
	int dis;
	node(){}
	node(int _v,int _dis,int _cost){
		v=_v;
		cost=_cost;
		dis=_dis; 
	}
};

int n,m,s,t;
int d[maxn];
int c[maxn];
bool vis[maxn];
vector<node> adj[maxn];
int pre[maxn];
vector<int> path;

void dijkstra(int s,int t){
	fill(vis,vis+maxn,false);
	fill(c,c+maxn,inf);
	fill(d,d+maxn,inf);
	fill(pre,pre+maxn,-1);
	d[s]=0;
	c[s]=0;
	
	for(int i=0;i<n;i++){
		int u=-1,MIN=inf;
		
		for(int j=0;j<n;j++){
			if(vis[j]==false){
				if(d[j]<MIN){
					u=j;
					MIN=d[j];	
				}
			}
		}
		
		if(u == t){
			return;
		}
		
		vis[u]=true;
		
		for(int j=0;j<adj[u].size();j++){
			node cur = adj[u][j];
			int v = cur.v;
			int cost = cur.cost;
			int dis = cur.dis;
			
			if(vis[v]==false){
				if(d[u]+dis<d[v]){
					d[v]=d[u]+dis; 
					c[v]=c[u]+cost;
					pre[v]=u;
				}else if(d[u]+dis==d[v] && c[u]+cost<c[v]){
					c[v]=c[u]+cost;
					pre[v]=u;
				} 
			}
		}
	}
}

void dfs(int x){
	if(x==s){
		path.push_back(x);
		return;
	}else{
		path.push_back(x);
		dfs(pre[x]);
	}
}

int main(){
	scanf("%d %d %d %d",&n,&m,&s,&t);
	for(int i=0;i<m;i++){
		int c1,c2,dis,cost;
		scanf("%d %d %d %d",&c1,&c2,&dis,&cost);
		adj[c1].push_back(node(c2,dis,cost));
		adj[c2].push_back(node(c1,dis,cost));
	}
	
	dijkstra(s,t);
	
	dfs(t);
	
	for(int i=path.size()-1;i>=0;i--){
		printf("%d ",path[i]);
	}
	
	printf("%d %d",d[t],c[t]);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值