UVA 10537 - The Toll! Revisited(dijstra扩张)
UVA 10537 - The Toll! Revisited
题意:给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交1,给定起点终点和到目标地点要剩下的货物,问最少要带多少货物上路。并输出路径,假设有多种方案。要求字典序最小
思路:dijstra的逆向运用。d数组含义变成到该结点至少须要这么多货物,然后反向建图,从终点向起点反向做一遍
这题被坑了。。并非输出的城市才存在。比方以下这组例子
0
1 A A
应该输出
1
A
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
using namespace std; const int MAXNODE = 105; typedef long long Type;
const Type INF = (1LL<<61); struct Edge {
int u, v;
Type dist;
Edge() {}
Edge(int u, int v, Type dist) {
this->u = u;
this->v = v;
this->dist = dist;
}
}; struct HeapNode {
Type d;
int u;
HeapNode() {}
HeapNode(Type d, int u) {
this->d = d;
this->u = u;
}
bool operator < (const HeapNode& c) const {
return d > c.d;
}
}; char to[255]; struct Dijkstra {
int n, m;
vector<Edge> edges;
vector<int> g[MAXNODE];
bool done[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE]; void init(int tot) {
n = tot;
for (int i = 0; i < n; i++)
g[i].clear();
edges.clear();
} void add_Edge(int u, int v, Type dist) {
edges.push_back(Edge(u, v, dist));
m = edges.size();
g[u].push_back(m - 1);
} void print(int e) {
if (p[e] == -1) {
printf("%c\n", to[e]);
return;
}
printf("%c-", to[e]);
print(edges[p[e]].u);
} void dijkstra(Type start, int s) {
priority_queue<HeapNode> Q;
for (int i = 0; i < n; i++) d[i] = INF;
d[s] = start;
p[s] = -1;
memset(done, false, sizeof(done));
Q.push(HeapNode(start, s));
while (!Q.empty()) {
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = 0; i < g[u].size(); i++) {
Edge& e = edges[g[u][i]];
Type need;
if (e.dist) need = (Type)ceil(d[u] * 1.0 / 19 * 20);
else need = d[u] + 1;
if (d[e.v] > need || (d[e.v] == need && to[u] < to[edges[p[e.v]].u])) {
d[e.v] = need;
p[e.v] = g[u][i];
Q.push(HeapNode(d[e.v], e.v));
}
}
}
}
} gao; typedef long long ll;
int n, m, vis[255]; int main() {
int cas = 0;
for (int i = 0; i < 26; i++) {
vis['A' + i] = i;
to[i] = 'A' + i;
}
for (int i = 0; i < 26; i++) {
vis['a' + i] = i + 26;
to[i + 26] = 'a' + i;
}
while (~scanf("%d", &m) && m != -1) {
gao.init(52);
char a[2], b[2];
int u, v;
while (m--) {
scanf("%s%s", a, b);
u = vis[a[0]], v = vis[b[0]];
gao.add_Edge(u, v, a[0] < 'a');
gao.add_Edge(v, u, b[0] < 'a');
}
ll need;
scanf("%lld%s%s", &need, a, b);
u = vis[a[0]]; v = vis[b[0]];
gao.dijkstra(need, v);
printf("Case %d:\n", ++cas);
printf("%lld\n", gao.d[u]);
gao.print(u);
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA 10537 - The Toll! Revisited(dijstra扩张)的更多相关文章
- UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)
前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...
- UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)
题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...
- UVa 10537 The Toll! Revisited (最短路)
题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- UVA10537 Toll! Revisited
difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
- 【Toll!Revisited(uva 10537)】
题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra! 在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...
- The Toll! Revisited UVA - 10537(变形。。)
给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...
- UVA 10537 Toll! Revisited (逆推,最短路)
从终点逆推,d[u]表示进入u以后剩下的货物,那么进入u之前的货物数量设为y,d[u] = x,那么y-x=ceil(y/20.0)=(y-1)/20+1=(y+19)/20. (y-x)*20+r= ...
- Uva 10537 过路费
题目链接:http://vjudge.net/contest/143062#problem/C 题意: 给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交 ...
随机推荐
- testlink于smarty配置和使用
于testlink于,采用smarty首先配置. 一般在过程化的编程中.创建一个smarty.inc.php的文件来配置Smarty的信息,其它文件引入就可以,目的是为了不改动smarty.class ...
- 安卓的sqlite增删改
基于安卓的sqlite增删改,笔记学习: 1.使用LinearLayout 布局生成,增删改的页面如图 代码布局如下: <LinearLayout xmlns:android="htt ...
- Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路
D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Thinkphp编辑器扩展类kindeditor用法
一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...
- 我理解设计模式C++实现观察者模式Observer Pattern
概述: 近期中国股市起起伏伏,当然了起伏就用商机,小明发现商机后果断想入市,买入了中国证券,他想在电脑client上,网页上,手机上,iPad上都能够查看到该证券的实时行情,这样的情况下我们应该怎么设 ...
- java 注解 学习
周末闲来无事,想要研究一下注解方面的知识,曾经看过几次,都忘记了,这次学习下,而且写篇文章记录下, 1.元注解 元注解是指注解的注解.包含 @Retention @Target @Document ...
- 【Java基金会】Java整理面试问题和评论(一)
1. ArrayList,Vector, LinkedList 存储性能及特点 ArrayList 和 Vector 都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便添加和插入元素,它们都 ...
- UI 纯代码实现计算器
// MHTAppDelegate.h // TestCa // Copyright (c) 2014年 Summer. All rights reserved. #import <UIK ...
- Velocity脚本新手教程
从网络下的数据汇编 一.Velocity简介 Velocity它是Apache该公司的开源产品,它是一套基于Java语言模板引擎,背景可以非常灵活的数据与模板文件一起反对.他直言不讳地说:,人使用模板 ...
- maven/eclipse搭建ssm(spring+spring mvc+mybatis)
maven/eclipse搭建ssm(spring+spring mvc+mybatis) 前言 本文旨在利用maven搭建ssm环境,而关于maven的具体内容,大家可以去阅读<Maven 实 ...