题意:给一个无向图,要从起点s运送一批货物到达终点e,每个点代表城镇/乡村,经过城镇需要留下(num+19)/20的货物,而经过乡村只需要1货物即可。现在如果要让p货物到达e,那么从起点出发最少要准备多少货物?输出答案和路径(多条路径则必须输出字典序最小的)。注:终点需要花费,而起点不需要。

思路:这最短路变形的不错。要逆推过来求最短路径,那么就从e出发到s的距离!只是p比较大,而且城镇还得推出前一站到底需要多少货物,既然直接计算那么麻烦,也可以一直p++直到能留下p为止就推出来了;而乡村就容易推了,只是+1。但是还有个字典序要解决,则在碰到dist[u]==dist[v]+距离的时候就比较一下字典序谁小就记谁。

  有一点别忘了,这是在逆推!!所以你要用出发点去更新终点时,要以出发点的dist来比较字典序。无向边是要建两条有向的,防止重边。

 #include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
int n, m, l, edge_cnt;
LL p;
vector<int> vect[N];
struct node
{
int from, to;
node(){};
node(int from,int to):from(from),to(to){};
}edge[]; void add_node(int from,int to)
{
edge[edge_cnt]=node(from, to);
vect[from].push_back(edge_cnt++);
} LL dist[N];
int path[N], vis[N];
LL dijkstra(int s,int e)
{
memset(path, , sizeof(path));
memset(vis, , sizeof(vis));
memset(dist, 0x7f, sizeof(dist)); priority_queue<pii,vector<pii>,greater<pii> > que;
que.push(make_pair(p,s));
dist[s]=p; while(!que.empty())
{
int x=que.top().second;que.pop();
if(vis[x]) continue;
vis[x]=; bool flag=isupper(x); //大写,花费多的
LL t=dist[x]+(dist[x]+)/;
while(t-(t+)/<dist[x]) t++; //注意不要超时 for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if( flag )
{
if(dist[e.to]>=t)
{
if(dist[e.to]==t)
{
if( x<edge[path[e.to]].from ) path[e.to]=vect[x][i]; //字典序
}
else path[e.to]=vect[x][i]; dist[e.to]=t;
que.push(make_pair(dist[e.to],e.to));
}
}
else
{
if(dist[e.to]>=dist[x]+)
{
if(dist[e.to]==dist[x]+)
{
if( x<edge[path[e.to]].from ) path[e.to]=vect[x][i]; //字典序
}
else path[e.to]=vect[x][i]; dist[e.to]=dist[x]+;
que.push(make_pair(dist[e.to],e.to));
}
}
}
}
return dist[e];
} void cal(int s,int e)
{
cout<<dijkstra(s,e)<<endl;
vector<char> ans;
int ed=e;
while(ed!=s)
{
ans.push_back( ed );
int t=path[ed];
ed=edge[t].from;
}
ans.push_back(s);
printf("%c",ans[]);
for(int i=; i<ans.size(); i++) printf("-%c",ans[i]);
cout<<endl;
} int main()
{
freopen("input.txt", "r", stdin);
char a, b;
int j=;
while(scanf("%d", &n), n>=)
{
edge_cnt=;
memset(edge,,sizeof(edge));
for(int i=; i<N; i++) vect[i].clear();
int up=;
for(int i=; i<n; i++)
{
getchar();
scanf("%c %c",&a,&b); //%c会接收到换行!!
add_node(a, b);
add_node(b, a);
}
scanf("%lld %c %c",&p, &a, &b);
printf("Case %d:\n",++j);
cal(b, a);
}
return ;
}

AC代码

UVA 10537 The Toll! Revisited 过路费(最短路,经典变形)的更多相关文章

  1. UVA 10537 - The Toll! Revisited(dijstra扩张)

    UVA 10537 - The Toll! Revisited option=com_onlinejudge&Itemid=8&page=show_problem&catego ...

  2. UVA 10537 The Toll! Revisited uva1027 Toll(最短路+数学坑)

    前者之所以叫加强版,就是把uva1027改编了,附加上打印路径罢了. 03年的final题哦!!虽然是水题,但不是我这个只会做图论题的跛子能轻易尝试的——因为有个数学坑. 题意:运送x个货物从a-&g ...

  3. UVa 10537 The Toll! Revisited (最短路)

    题意:给定一个图,你要从 s 到达 t,当经过大写字母时,要交 ceil(x /20)的税,如果经过小写字母,那么交 1的税,问你到达 t 后还剩下 c 的,那么最少要带多少,并输出一个解,如果多个解 ...

  4. uva 10537 Toll! Revisited(优先队列优化dijstra及变形)

    Toll! Revisited 大致题意:有两种节点,一种是大写字母,一种是小写字母. 首先输入m条边.当经过小写字母时须要付一单位的过路费.当经过大写字母时,要付当前財务的1/20做过路费. 问在起 ...

  5. UVA10537 Toll! Revisited

    difkstra + 路径输出 The Toll! Revisited Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  6. 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= ...

  7. 【Toll!Revisited(uva 10537)】

    题目来源:蓝皮书P331 ·这道题使得我们更加深刻的去理解Dijkstra!       在做惯了if(dis[u]+w<dis[v])的普通最短路后,这道选择路径方案不是简单的比大小的题横在了 ...

  8. The Toll! Revisited UVA - 10537(变形。。)

    给定图G=(V,E)G=(V,E),VV中有两类点,一类点(AA类)在进入时要缴纳1的费用,另一类点(BB类)在进入时要缴纳当前携带金额的1/20(不足20的部分按20算) 已知起点为SS,终点为TT ...

  9. Uva 10537 过路费

    题目链接:http://vjudge.net/contest/143062#problem/C 题意: 给定一个无向图,大写字母是城市,小写字母是村庄,经过城市交过路费为当前货物的%5,路过村庄固定交 ...

随机推荐

  1. oracle字符集问题总结

    在进行web开发和oracle安装的过程中经常有人对字符集搞不清楚,因此对此做一下总结. 1.第一个问题:字符集之间的区别是什么呢?   常见的字符集有:UTF-8和GBK   (1)GBK字符集 G ...

  2. eclipse svn 修改了类名之后提交

    win下面的文件名不区分大小写,所以不能只是把小写类名改成大写. 正确的做法有如下两种:1,先删除类a,提交,此操作会删除服务器上的文件.再添加类A,提交.2,重命名a为aa,提交,此操作会删除服务器 ...

  3. C# \uxxx Unicode编码解码

    /// <summary> /// Unicode编码 /// </summary> /// <param name="str"></pa ...

  4. Indent Guides VS 插件 对齐线

  5. DiskGenius的 “终止位置参数溢出”错误解决方法。

    (转帖)同事电脑系统启动突然明显变慢,重装系统后问题仍未解决(windowsxp sp3).帮忙分析感觉是磁盘分区表出现了错误,用通用PE工具箱进入PE系统,DiskGenius工具检查:“终止位置参 ...

  6. ubuntu安装hadoop2.6

    一:单机版 1.sudo gedit ~/.bashrc 加入JDK路径 #HADOOP VARIABLES START export JAVA_HOME=/usr/lib/jvm/java-1.7. ...

  7. 一些linux的问题

    本文罗列的是我在学习linux与shell编程时所遇到的一些问题.我相信既然存在问题那么就会有需求,记录于此,希望可以快速帮助到大家. 1.在vim中用“/word"查找后,vim会以棕色背 ...

  8. 看来要学 Asp.Net 了

    C#大部分招聘都要这个:对个人用而言,太庞大了,所以对其的感观一直不咋,也就没想学了.

  9. 1009 FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  10. Project Euler 76:Counting summations

    题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + ...