PAT A1018 Gestión de bicicletas públicas (30 分)

Inserte la descripción de la imagen aquí
Esta pregunta es realmente una buena pregunta, básicamente cubre todos los sitios de prueba en Dijkstral.
Me calmé y escribí durante una hora, y descubrí que los puntos de prueba 5 y 7 no podían pasar. Lo analicé durante un tiempo sin éxito. Consulté los blogs de otras personas y encontré que estos dos puntos son pits, porque las bicicletas detrás del la carretera no puede llenar la falta de bicicletas en el frente, las bicicletas adicionales en el frente pueden llenar las que faltan en la parte trasera.

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

int d[MAXV];
int num[MAXV];
int G[MAXV][MAXV];
int half;
int tb = INF;
int mn = INF;
int out = 0;
int in = 0;
int c, n, p, m;
bool vis[MAXV] = {
    
    false};
vector<int> pre[MAXV], 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 e){
    
    
	if(e == 0){
    
    
		tempPath.push_back(e);
		int need = 0;
		int back = 0;
		for(int i=0; i<tempPath.size()-1; i++){
    
    
			int index = tempPath[i];
			if(num[index] > half){
    
    
				back += (num[index]-half);
				if(need != 0){
    
    
					if(back <= need){
    
    
						need -= back;
						back = 0;
					}else{
    
    
						back -= need;
						need = 0;
					}
				}
			}else if(num[index] < half){
    
    
				need += (half-num[index]);
			}
		}
		if(need < mn){
    
    
			tb = back;
			path = tempPath;
			in = tb;
			mn = need;
			out = need;
		}else if(need == mn){
    
    
			if(back < tb){
    
    
				tb = back;
				path = tempPath;
				in = tb;
				mn = need;
				out = need;
			}
		}
		tempPath.pop_back();
	}
	tempPath.push_back(e);
	for(int i=0; i<pre[e].size(); i++){
    
    
		DFS(pre[e][i]);
	}
	tempPath.pop_back();
}

int main(){
    
    
	fill(G[0], G[0]+MAXV*MAXV, INF);
	int u, v, w;
	scanf("%d %d %d %d", &c, &n, &p, &m);
	for(int i=1; i<=n; i++){
    
    
		scanf("%d", &num[i]);
	}
	half = c/2;
	for(int i=0; i<m; i++){
    
    
		scanf("%d %d %d", &u, &v, &w);
		G[u][v] = w;
		G[v][u] = w;
	}
	Dijkstral(0);
	DFS(p);
	printf("%d ", out);
	for(int i=path.size()-1; i>=0; i--){
    
    
		printf("%d", path[i]);
		if(i != 0) printf("->");
	}
	printf(" %d", in);
	
	return 0;
}

Supongo que te gusta

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