题意:给一个无向图,要从起点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. hdu 4187 Alphabet Soup

    这题的主要就是找循环节数,这里用找字符串最小覆盖来实现,也就是n-next[n],证明在这http://blog.csdn.net/fjsd155/article/details/6866991 #i ...

  2. mysql 中 isnull 和 ifnull 判断字段是否为null

    对于统计count(type)和avg(type) 都不起作用 SQL中有ISNULL方法,介绍如下: ISNULL使用指定的替换值替换 NULL. 语法ISNULL ( check_expressi ...

  3. C语言的字符测试函数

    C语言的字符测试函数 isalnum, isalpha, isdigit, isxdigit, isblank, isspace, isascii, iscntrl, ispunct, isgraph ...

  4. 猜拳 GuessFist

    import java.util.Scanner;import java.util.Random;/***跟电脑玩石头剪刀布,需要从控制台输入信息,*然后去判断,然后给予反馈信息*/public cl ...

  5. java:装饰者模式,节点流和处理流

    接口: interface Worker { public void dowork(); } 清洁工:被装饰者,节点流 class Clean implements Worker { public v ...

  6. Filter设计实现IP地址限制

    示例:创建一个IP过滤Filter,当一个用户发出访问请求的时候,首先通过过滤器进行判断, 如果用户的IP地址被限制,就禁止访问,只有合法的IP才可以继续访问.IP过滤Filter代码如下: IPFi ...

  7. Hadoop HDFS文件常用操作及注意事项(更新)

    1.Copy a file from the local file system to HDFS The srcFile variable needs to contain the full name ...

  8. JS代码片段:判断一个元素是否进入可视区域

    // Determine if an element is in the visible viewport function isInViewport(element) { var rect = el ...

  9. 写出优秀论文How To Write A Great Essay About Anything

    There is an assumption in the world that an essay is something literary you write for school about a ...

  10. Hibernate HQL查询的参数绑定

    参数绑定: Hibernate中对动态查询参数绑定提供了丰富的支持,那么什么是查询参数动态绑定呢?其实如果我们熟悉传统JDBC编程的话,我们就不难理解查询参数动态绑定,如下代码传统JDBC的参数绑定: ...