【二分答案】SSL_1155 遨游

本文探讨了一种结合图论和二分搜索算法解决路径费用优化问题的方法。在一个包含多个省份及其内部城市连接的图中,通过二分搜索确定起点到终点路径的最大折扣率LLL和最小实际费用RRR,实现路径费用的最优化。

题意

给出一个图,上面有NNN个省,每个省里面有一些城市,它们之间可能相连,其中每条路有个路费,每省里有一些优惠措施,可以让这个省的道路打一些折,不同的省之间的道路也有一些优惠措施。现在我们要求出LLLRRR,让起点到终点的路的路费都在这个范围内,求出最大的LLL,在这个基础上,求出最小的RRR

思路

根据题目我们很容易看出是一道二分题。
我们先二分LLL,找到一个最大的,再在这个基础上二分到一个最小的RRR,优惠预处理一下就好了。

代码

#include<queue>
#include<cstdio>
#include<cstring>
#define clean for (int i = 1; i <= DN; i++) v[i] = 0
using namespace std;

struct Edge{
	int x, y, next;
	double w;
}e[200001];
int N, M, S, T, DN, DM;
int L = 2147483647, R, l, r, ansl, ansr;
int c[50001], head[50001];
bool v[50001];

void init() {
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++) {
		DM++;
		scanf("%d %d %lf", &e[DM].x, &e[DM].y, &e[DM].w);
		e[DM].next = head[e[DM].x];
		head[e[DM].x] = DM;
		e[++DM].x = e[DM - 1].y;
		e[DM].y = e[DM - 1].x;
		e[DM].next = head[e[DM].x];
		head[e[DM].x] = DM;
	}
	int x, y;
	for (int i = 1; i <= N; i++) {
		scanf("%d", &x);
		for (int j = 1; j <= x; j++) {
			scanf("%d", &y);
			c[y] = i;
			DN++;
		}
	}
	int z[5001];
	for (int i = 1; i <= N; i++) scanf("%d", &z[i]);
	for (int i = 1; i <= DM; i+=2) {
		int x = e[i].x, y = e[i].y;
		if (c[x] == c[y]) e[i].w = e[i].w * z[c[x]] / 100.0;
		else e[i].w = e[i].w * (z[c[x]] + z[c[y]]) / 200.0;
		e[i + 1].w = e[i].w;
		if (L > e[i].w) L = e[i].w;
		if (R < e[i].w) R = e[i].w;
	}
	R++;
	scanf("%d %d", &S, &T);
}

bool dfs(int p, int l, int r) {
	if (p == T) return 1;
	v[p] = 1;
	for (int i = head[p]; i; i = e[i].next) {
		if (!v[e[i].y] && e[i].w >= l && e[i].w <= r)
			if (dfs(e[i].y, l, r)) return 1; 
	}
	return 0;
}

void work() {
	l = L;
	r = R;
	while (l < r) {//L
		int mid = (l + r) >> 1;
		clean;
		if (dfs(S, mid, R)) {//判断当前的答案能否满足起点能到达终点
			l = mid + 1;
			ansl = mid;
		}
		else r = mid;
	}
	l = ansl;
	r = R;
	while (l < r) {//R
		int mid = (l + r + 1) >> 1;
		clean;
		if (dfs(S, ansl, mid)) {
			r = mid - 1;
			ansr = mid;
		}
		else l = mid;
	}
	printf("%d %d", ansl, ansr);
}
int main() {
	init();
	work();
}
int ssl_init(struct tp_ssl *ssl, const struct tls_root_ctx *ssl_ctx, int is_server, int is_cwmp) #else int ssl_init(struct tp_ssl *ssl, const struct tls_root_ctx *ssl_ctx, int is_server) #endif /* INCLUDE_HTTP_SSL_MIN_TLS1_2 */ { int ret = -1; const char *pers = "tp_ssl"; ssl_log(SSL_DEBUG, "enter %s", __FUNCTION__); if (NULL == ssl || NULL == ssl_ctx) return -1; memset(ssl, 0, sizeof(struct tp_ssl)); ssl_init_debug_level(); ssl_init_timeout(); mbedtls_ctr_drbg_init(&ssl->ctr_drbg); mbedtls_entropy_init(&ssl->entropy); if ((ret = mbedtls_ctr_drbg_seed(&ssl->ctr_drbg, mbedtls_entropy_func, &ssl->entropy, (const unsigned char *) pers, strlen(pers))) != 0) { ssl_log(SSL_ERROR, "mbedtls_ctr_drbg_seed returned %d\n", ret ); goto exit; } /* Initialise SSL config */ mbedtls_ssl_config_init(&ssl->ssl_config); if ((ret = mbedtls_ssl_config_defaults(&ssl->ssl_config, ssl_ctx->endpoint, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { ssl_log(SSL_ERROR, "mbedtls_ssl_config_defaults returned %d\n", ret ); goto exit; } mbedtls_ssl_conf_rng(&ssl->ssl_config, mbedtls_ctr_drbg_random, &ssl->ctr_drbg); mbedtls_ssl_conf_dbg(&ssl->ssl_config, tp_debug, stdout); mbedtls_ssl_cache_init(&ssl->cache); mbedtls_ssl_conf_session_cache(&ssl->ssl_config, &ssl->cache, mbedtls_ssl_cache_get, mbedtls_ssl_cache_set); mbedtls_ssl_conf_ciphersuites(&ssl->ssl_config, g_ciphersuite); mbedtls_ssl_conf_ca_chain(&ssl->ssl_config, ssl_ctx->ca_chain, NULL); if ((ret = mbedtls_ssl_conf_own_cert(&ssl->ssl_config, ssl_ctx->crt_chain, ssl_ctx->priv_key)) != 0) { ssl_log(SSL_ERROR, "mbedtls_ssl_conf_own_cert returned %d\n", ret ); goto exit; } if (!is_server) { mbedtls_ssl_conf_verify(&ssl->ssl_config, verify_callback, NULL); mbedtls_ssl_conf_authmode(&ssl->ssl_config, MBEDTLS_SSL_VERIFY_OPTIONAL); } #ifdef INCLUDE_IMDA mbedtls_ssl_conf_min_version(&ssl->ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); #else #ifdef INCLUDE_HTTP_SSL_MIN_TLS1_2 if (1 != is_cwmp) { mbedtls_ssl_conf_min_version(&ssl->ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); } else #endif /* INCLUDE_HTTP_SSL_MIN_TLS1_2 */ { mbedtls_ssl_conf_min_version(&ssl->ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_1); } #endif /* INCLUDE_IMDA */ mbedtls_ssl_conf_max_version(&ssl->ssl_config, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3); /* Initialise SSL context */ ssl->ctx = (mbedtls_ssl_context*)malloc(sizeof(mbedtls_ssl_context)); if (NULL == ssl->ctx) goto exit; mbedtls_ssl_init(ssl->ctx); if (mbedtls_ssl_setup(ssl->ctx, &ssl->ssl_config) != 0) { ssl_log(SSL_ERROR, "mbedtls_ssl_setup failed"); return -1; } if (is_server) { ssl->endpoint = MBEDTLS_SSL_IS_SERVER; } else { if (ssl_ctx->verify_cn) { mbedtls_ssl_set_hostname(ssl->ctx, ssl_ctx->verify_cn); ssl->verify_cn = 1; } if (ssl_ctx->verify_time) { ssl->verify_time = 1; } ssl->endpoint = MBEDTLS_SSL_IS_CLIENT; } ssl_init_stream(&ssl->stream); ret = 0; exit: ssl_log(SSL_DEBUG, "leave %s ret=%d", __FUNCTION__, ret); return ret; }这里有加载私钥的过程吗
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值