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=y+19,0≤r≤19,即19*y=20*x+r,根据题意y应该尽量小,x的部分是不能变动的,所以y=x+ceil(x/19.0)。
然后从起点找一条字典序最小的路径即可,因为每个字母都是独一无二的,所以不必bfs,每次记录一个点就够了。
#include<bits/stdc++.h>
using namespace std; const int maxn = , maxm = maxn*maxn, town = , village = ;
bool g[maxn][maxn];
int head[maxn],nxt[maxm],to[maxm],ecnt;
int tp[maxn];
int id[];
char rid[]; void addEdge(int u,int v)
{
to[ecnt] = v;
nxt[ecnt] = head[u];
head[u] = ecnt++;
}
int id_cnt; void init()
{
memset(head,-,sizeof(head));
memset(id,-,sizeof(id));
memset(g,,sizeof(g));
ecnt = ;
id_cnt = ;
} inline int ID(char c) {
if(~id[c]) return id[c];
id[c]= id_cnt;
rid[id_cnt] = c;
tp[id_cnt] = 'a'<=c&&c<='z';//写成isalpha(c)-1;WA了
return id_cnt++;
}
typedef long long ll;
typedef pair<ll,int> Node;
#define fi first
#define se second ll d[maxn];
void dijkstra(int s,ll p)
{
memset(d,0x7f,sizeof(d));
priority_queue<Node,vector<Node>,greater<Node> > q;
q.push(Node(d[s] = p,s));
while(q.size()){
Node x = q.top(); q.pop();
int u = x.se;
if(d[u] != x.fi) continue;
ll t = (tp[u]?(+d[u]):((d[u]+)/+d[u]));
for(int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if(d[v] > t ){
q.push(Node(d[v]= t,v));
}
}
}
} ll cost(int u,int v)
{
return tp[v]?:((d[u]+)/);
} void FindPath(int s,int e)
{
int u = s;
while(u != e){
printf("%c-",rid[u]);
int nex = -;
for(int i = head[u]; ~i; i = nxt[i]){
int v = to[i];
if(d[u]- cost(u,v) >= d[v]){
if(~nex) {
if(rid[v] < rid[nex]) nex = v;
}else nex = v;
}
}
swap(nex,u);
}
printf("%c\n",rid[e]);
} int main()
{
//freopen("in.txt","r",stdin);
int n;
char a[],b[];
int kas = ;
while(scanf("%d",&n),~n){
init();
while(n--){
scanf("%s%s",a,b);
int u = ID(*a), v = ID(*b);
if(!g[u][v]){
g[u][v] = g[v][u] = true;
addEdge(u,v); addEdge(v,u);
}
}
ll p;
scanf("%lld%s%s",&p,a,b);
int s = ID(*a),e = ID(*b);
dijkstra(e,p);
printf("Case %d:\n%lld\n",++kas,d[s]);
FindPath(s,e);
}
return ;
}
UVA 10537 Toll! Revisited (逆推,最短路)的更多相关文章
- uva 10537 Toll! Revisited(优先队列优化dijstra及变形)
Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...
- UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)
题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可.现在如果要让p货物到达e,那么从起点出发最少要准备 ...
- 【UVA10537】The Toll! Revisited (逆推最短路)
题目: Sample Input1a Z19 a Z5A DD XA bb cc X39 A X-1Sample OutputCase 1:20a-ZCase 2:44A-b-c-X 题意: 有两种节 ...
- UVa 10537 The Toll! Revisited (最短路)
题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...
- UVA 10537 - The Toll! Revisited(dijstra扩张)
UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...
- Uva 10537 过路费
题目链接:http://vjudge.net/contest/143062#problem/C 题意: 给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交 ...
- uva10537 dijkstra + 逆推
21:49:45 2015-03-09 传送 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8& ...
- UVA116Unidirectional TSP(DP+逆推)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...
- UVA10537 Toll! Revisited
difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
随机推荐
- react中事件的使用
import React from 'react' class Home extends React.Component{ constructor(props){ super(props) this. ...
- 使用ASP.NET Core实现Docker的HealthCheck指令
写在前面 HealthCheck 不仅是对应用程序内运行情况.数据流通情况进行检查, 还包括应用程序对外部服务或依赖资源的健康检查. 健康检查通常是以暴露应用程序的HTTP端点的形式 实施,可用于配 ...
- [nyoj]会场安排问题-贪心
会场安排问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工 ...
- 【转】oracle的分析函数over
源地址:http://www.cnblogs.com/sumsen/archive/2012/05/30/2525800.html
- js的Element.scrollIntoView的学习
1.Element.scrollIntoView() 该方法让当前元素滚动到浏览器窗口的可是区域内: 2.语法: element.scrollIntoView();//等同于element.sc ...
- jQuery EasyUI/TopJUI创建日期时间输入框
jQuery EasyUI/TopJUI创建日期时间输入框 日期时间输入框组件 HTML 和日期输入框类似,日期时间输入框允许用户选择日期和指定的时间并按照指定的输出格式显示.相比日期输入框,它在下拉 ...
- mysql5.6数据库同步,单向双向同步问题
windows下MySQL5.6实现主从数据库同步数据 mysql5.6数据库同步,单向双向同步问题 一.单向同步 主数据库(mysql5.6)192.168.1.104 从数据库(mysql5. ...
- SQL SERVER CAST 和 CONVERT 函数
遇到CAST 函数转化数字不一致情况, select CAST('0000000011237590798' AS money) / 100 AS Amount--output : 112375907. ...
- Django配置文件解释
"""Django settings for first project. Generated by 'django-admin startproject' using ...
- 长春理工大学第十四届程序设计竞赛(重现赛)B.Bowling Game
链接:https://ac.nowcoder.com/acm/contest/912/B 题意: 链接:https://ac.nowcoder.com/acm/contest/912/B来源:牛客网 ...