uva 10537 - The Toll! Revisited(最短路)

737 篇文章 0 订阅

题目链接:uva 10537 - The Toll! Revisited


从终点向起点做最短路,维护到达节点最少需要多少个。


#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 55;
const int inf = 0x3f3f3f3f;

int IDX(char ch) { if (ch >= 'A' && ch <= 'Z') return ch - 'A'; else return 26 + ch - 'a'; }
char RIDX(int x) { if (x < 26) return 'A' + x; else return 'a' + x - 26; }

int N = 52, M, S, E, L, V[maxn];
ll D[maxn];
vector<int> G[maxn], ans;

struct State {
	int u;
	ll d;
	State (int u = 0, ll d = 0): u(u), d(d) {}
	bool operator < (const State& u) const { return d > u.d; }
};

void init () {
	for (int i = 0; i <= N; i++) G[i].clear();

	char s[5], e[5];
	for (int i = 0; i < M; i++) {
		scanf("%s%s", s, e);
		int u = IDX(s[0]), v = IDX(e[0]);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	scanf("%d%s%s", &L, s, e);
	S = IDX(s[0]), E = IDX(e[0]);
}

ll limit (int x, ll w) {
	if (x >= 26) return w+1;
	ll l = 0, r = inf;
	while (l < r) {
		ll mid = (l + r) >> 1;
		ll t = mid + w;
		if (t - (t + 19) / 20 >= w) r = mid;
		else l = mid + 1;
	}
	return w + l;
}

ll consume(int u, ll d) {
	if (u >= 26) return 1;
	else return (d + 19) / 20;
}

void put (int u) {
	ans.push_back(u);
	if (u == E) return;

	int rec = -1;
	for (int i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (D[u] - consume(v, D[u]) == D[v] && (rec == -1 || rec > v))
			rec = v;
	}
	put(rec);
}

void dijkstra () {
	memset(V, 0, sizeof(V));
	memset(D, inf, sizeof(D));

	D[E] = L;

	priority_queue<State> Q;
	Q.push(State(E, D[E]));

	while (!Q.empty()) {
		int u = Q.top().u;
		Q.pop();

		if (V[u]) continue;
		V[u] = 1;
		ll w = limit(u, D[u]);

		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (D[v] > w) {
				D[v] = w;
				Q.push(State(v, w));
			}
		}
	}

	printf("%lld\n", D[S]);
	ans.clear();
	put(S);
	for (int i = 0; i < ans.size(); i++) {
		printf("%c%c", RIDX(ans[i]), i == ans.size()-1 ? '\n' : '-');
	}
}

int main () {
	int cas = 1;
	while (scanf("%d", &M) == 1 && M != -1) {
		init ();
		printf("Case %d:\n", cas++);
		dijkstra();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VMware Tools是一组用于增强虚拟机性能和功能的软件。它提供了与虚拟机主机之间的更好交互和协作。根据提供的引用内容,您遇到的问题可能是拼写错误。正确的名称是"VMware Tools",而不是"VMware Toll"。如果您无法找到VMware Tools的压缩包,请确保您已正确安装Ubuntu,并按照正确的步骤在VMware中安装VMware Tools。请参考中提供的网上教程,按照指示执行安装步骤。如果您仍然遇到问题,可以尝试查找其他可靠的教程或寻求VMware支持社区的帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [“找不到VMware Tools 安装包”的解决办法——安装VMware Tools](https://blog.csdn.net/Donggua_BIT/article/details/100740347)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [vmware虚机安装vmtool](https://blog.csdn.net/weixin_43570089/article/details/90637909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值