1018. Public Bike Management (30)

   DFS找符合条件的路径

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <limits>

using namespace std;

int cmax, n, sp, m, half;

vector<bool> used;
vector<int> bikes;
vector<vector<int>> edges;

int minTime = numeric_limits<int>::max();
int minSend = numeric_limits<int>::max();
int minCollect = numeric_limits<int>::max();
vector<int> minPath;

void dfs(int s, int curTime, int curSend, int curCollect, vector<int>& curPath){
	if(s == sp){
		bool choosed = curTime < minTime 
		|| (curTime == minTime && curSend < minSend)
		|| (curTime == minTime && curSend == minSend && curCollect < minCollect);

		if(choosed){
			minTime = curTime;
			minSend = curSend;
			minCollect = curCollect;
			minPath = curPath;
		}

		return;
	}

	if(curTime > minTime) return;

	for(int i = 1; i <= n; ++i){
		if(!used[i] && edges[s][i] != -1){
			used[i] = true;
			curPath.push_back(i);

			if(curCollect + bikes[i] < half){
				dfs(i, curTime + edges[s][i], curSend + half - bikes[i] - curCollect, 0, curPath);
			}else{
				dfs(i, curTime + edges[s][i], curSend, bikes[i] + curCollect - half, curPath);
			}
			
			used[i] = false;
			curPath.pop_back();
		}
	}
}

int main(){
	scanf("%d%d%d%d", &cmax, &n, &sp, &m);
	half = cmax / 2;

	used.resize(n+1, false);
	bikes.resize(n+1);
	edges.resize(n+1);
	for(auto& edge : edges){
		edge.resize(n+1, -1);
	}

	for(int i = 1; i <= n; ++i){
		scanf("%d", &bikes[i]);
	}

	for(int i = 0; i < m; ++i){
		int si, sj, tij;
		scanf("%d%d%d", &si, &sj, &tij);
		edges[si][sj] = edges[sj][si] = tij;
	}

	vector<int> curPath;
	dfs(0, 0, 0, 0, curPath);

	printf("%d 0", minSend);
	for(auto& n : minPath){
		printf("->%d", n);
	}

	printf(" %d", minCollect);

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值