Plan de viaje PAT A1030 (30 分)

Inserte la descripción de la imagen aquí
Preguntas de plantilla estándar de Dijkstral, nada difícil

#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXV = 510;
const int INF = 1e9;

int n, m, s, e;
int G[MAXV][MAXV];
int d[MAXV];
int mincost[MAXV];
int cost[MAXV][MAXV];
bool vis[MAXV] = {
    
    false};
int pre[MAXV];

void Dijkstral(int s){
    
    
	fill(d, d+MAXV, INF);
	fill(mincost, mincost+MAXV, INF);
	d[s] = 0;
	mincost[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 && d[j]<MIN){
    
    
				u = j;
				MIN = d[j];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int v=0; v<n; v++){
    
    
			if(vis[v]==false && G[u][v]!=INF){
    
    
				if(d[v] > d[u]+G[u][v]){
    
    
					d[v] = d[u]+G[u][v];
					mincost[v] = mincost[u]+cost[u][v];
					pre[v] = u;
				}else if(d[v] == d[u]+G[u][v]){
    
    
					if(mincost[v] > mincost[u]+cost[u][v]){
    
    
						mincost[v] = mincost[u]+cost[u][v];
						pre[v] = u;
					}
				}
			}
		}	
	}
}

void print(int t){
    
    
	if(t == s){
    
    
		printf("%d ", t);
		return;
	}
	print(pre[t]);
	printf("%d ", t);
}

int main(){
    
    
	int u, v, w, c;
	fill(G[0], G[0]+MAXV*MAXV, INF);
	scanf("%d %d %d %d", &n, &m, &s, &e);
	for(int i=0; i<m; i++){
    
    
		scanf("%d %d %d %d", &u, &v, &w, &c);
		G[v][u] = G[u][v] = w;
		cost[u][v] = cost[v][u] = c;
	}
	Dijkstral(s);
	print(e);
	printf("%d %d", d[e], mincost[e]);
	
	return 0;
}

Solución Dijkstral + DFS

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXV = 510;
const int INF = 1e9;

int n, m, s, e;
int G[MAXV][MAXV];
int d[MAXV];
int mincost = INF;
int cost[MAXV][MAXV];
bool vis[MAXV] = {
    
    false};
vector<int> pre[MAXV];
vector<int> tempPath, path;

void Dijkstral(int s){
    
    
	fill(d, d+MAXV, INF);
	d[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 && d[j]<MIN){
    
    
				u = j;
				MIN = d[j];
			}
		}
		if(u == -1) return;
		vis[u] = true;
		for(int v=0; v<n; v++){
    
    
			if(vis[v]==false && G[u][v]!=INF){
    
    
				if(d[v] > d[u]+G[u][v]){
    
    
					d[v] = d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}else if(d[v] == d[u]+G[u][v]){
    
    
					pre[v].push_back(u);
				}
			}
		}	
	}
}

void DFS(int v){
    
    
	if(v == s){
    
    
		tempPath.push_back(v);
		int sum = 0;
		for(int i=0; i<tempPath.size()-1; i++){
    
    
			int index = tempPath[i];
			int next = tempPath[i+1];
			sum += cost[index][next];
		}
		if(sum < mincost){
    
    
			mincost = sum;
			path = tempPath;
		}
		tempPath.pop_back();
	}
	tempPath.push_back(v);
	for(int i=0; i<pre[v].size(); i++){
    
    
		DFS(pre[v][i]);
	}
	tempPath.pop_back();
}

int main(){
    
    
	int u, v, w, c;
	fill(G[0], G[0]+MAXV*MAXV, INF);
	scanf("%d %d %d %d", &n, &m, &s, &e);
	for(int i=0; i<m; i++){
    
    
		scanf("%d %d %d %d", &u, &v, &w, &c);
		G[v][u] = G[u][v] = w;
		cost[u][v] = cost[v][u] = c;
	}
	Dijkstral(s);
	DFS(e);
	for(int i=path.size()-1; i>=0; i--){
    
    
		printf("%d ", path[i]);
	}
	printf("%d %d", d[e], mincost);
	
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_45964844/article/details/113774214
Recomendado
Clasificación