21:49:45 2015-03-09

传送 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1478

这题说的是运送货物需要交纳过路费。进入一个村庄需要交纳1个单位的货物,而进入一个城镇是每20个单位的货物中就要上缴1个单位的(70要上交4个)问选择哪条道路使得过路费最少,

这题我们知道了终太 , 那么我们就可以逆推回去, 比如说我们知道了最后一站我们就可以逆推回上一站, 采用dijkstra 计算出每个点到终点的最优的过路费,再来一次贪心取最小的结果。

 #include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
#include <cstdio>
using namespace std;
const int maxn=;
const long long INF = 1LL<<;
typedef long long LL;
int hash_num(char c){
if(c>='A'&&c<='Z') return c-'A';
else return c-'a'+;
}
char hash_char(int c){
if(c>=&&c<) return c+'A';
else return c-+'a';
}
int st,ed;
LL d[maxn];
bool G[maxn][maxn],mark[maxn];
LL projud(LL item, int next){
if(next<) return item-(item+)/;
return item-;
}
LL jud(int u){
if(u >= ) return d[u]+;
LL v = d[u];
LL ans=;
ans=v;
while(true){
LL d=v/;
v=v%;
ans+=d;
v+=d;
if(d==)break;
}
if(v) ans++;
return ans;
}
LL forward(LL item, int next) {
if(next < ) return item - (item + ) / ;
return item - ;
}
struct Head{
LL d; int u;
bool operator < (const Head &r)const {
return d>r.d;
}
Head (LL dd =, int uu = ){
d=dd; u = uu;
}
};
void print(LL p){
int nod=;
memset(mark,false,sizeof(mark));
for(int i =; i<nod ; i++ )d[i]=INF;
d[ed]=p;
priority_queue<Head> Q;
Q.push(Head(p,ed));
while(!Q.empty()){
Head x= Q.top(); Q.pop();
if(mark[x.u]) continue;
mark[x.u]=true;
for(int i=; i<nod; ++i){
LL pe = jud(x.u);
if(mark[i]==false&&G[i][x.u]&&pe<d[i]){
d[i] = pe;
Q.push(Head(d[i],i));
}
}
} int u=st;
printf("%lld\n",d[st]);
printf("%c",hash_char(u)); LL item = d[st];
while(u != ed ){
int next;
for(next = ; next < nod; next++) if(G[u][next] && forward(item, next) >= d[next]) break;
item = d[next];
printf("-%c", hash_char(next));
u = next;
}
printf("\n");
}
int main()
{
int n,cas=;
char s1[],s2[]; while(scanf("%d",&n)==&&n!=-){
memset(G,false,sizeof(G)); for(int i= ; i<n; ++i){
scanf("%s%s",s1,s2);
int u = hash_num(s1[]);
int v = hash_num(s2[]);
if(u==v)continue;
G[u][v]=G[v][u]=true; }
LL p;
scanf("%lld%s%s",&p,s1,s2);
st = hash_num(s1[]); ed = hash_num(s2[]);
printf("Case %d:\n",++cas);
print(p);
}
return ;
}

uva10537 dijkstra + 逆推的更多相关文章

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

  2. UVA116Unidirectional TSP(DP+逆推)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18206 题意:M*N的数阵,从左边一列到右边一列走过的数的和的最小.并输出路 ...

  3. HDU 5844 LCM Walk(数学逆推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5584 题意: 现在有坐标(x,y),设它们的最小公倍数为k,接下来可以移动到(x+k,y)或者(x,y+k).现 ...

  4. hdu 5063 操作逆推+mul每次要*2%(modo - 1)

    http://acm.hdu.edu.cn/showproblem.php?pid=5063 只有50个询问,50个操作逆推回去即可,注意mul每次要*2%(modo - 1)因为是指数! #incl ...

  5. hdu 3853 LOOPS (概率dp 逆推求期望)

    题目链接 LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Tota ...

  6. HDU 1176-免费馅饼(DP_逆推)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. Codeforces Round #499 (Div. 2) C.FLY 数学推导_逆推

    本题应该是可以使用实数二分的,不过笔者一直未调出来,而且发现了一种更为优美的解法,那就是逆推. 首先,不难猜到在最优解中当飞船回到 111 号节点时油量一定为 000, 这就意味着减少的油量等于减少之 ...

  8. C# Net 计算周(可正推和逆推)

    C# Net 计算周(可正推和逆推) 拷贝代码(方法): /// <summary> /// 计算周 /// </summary> /// <param name=&qu ...

  9. 概率dp——逆推期望+循环迭代zoj3329

    首先要推出dp[i]的期望方程,会发现每一项都和dp[0]相关, 那我们将dp[i]设为和dp[0]有关的式子dp[i]=a[i]*dp[0]+b[i],然后再回代到原来的期望方程里 然后进行整理,可 ...

随机推荐

  1. python2.0 s12 day2

    s12 day2 视频每节的内容 05 python s12 day2 python编码   1.第一句python代码 python 执行代码的过程 文件读到内存 分析内容 编译字节码  转换机器码 ...

  2. 如何才能知道外线是FSK还是DTMF制式?

    在直线上接上来电显示话机,然后用手机或其他电话拨接电话的外线号码. 1.如果是先振铃后来显,就是FSK制式. 2.如果先送来显,再振铃或来显和振铃同时响应,就是DTMF制式 交换机默认配置是FSK制式 ...

  3. Spring装配Bean的过程

    首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...

  4. Python 入门(九)迭代

    什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...

  5. Effective C++ —— 继承与面向对象设计(六)

    条款32 : 确定你的public继承塑模出is-a关系 以C++进行面向对象编程,最重要的一个规则是:public inheritance(公开继承)意味“is-a”(是一种)的关系.请务必牢记.当 ...

  6. luogu P1379 八数码难题(A*算法入门详细讲解)

     代码实现细节 #include<cstdio> #include<cstring> #include<iostream> using namespace std; ...

  7. 【Android】保存Bitmap到SD卡

    1.打开读写SD卡的权限 需要在AndroidManifest.xml加入如下代码: <uses-permission android:name="android.permission ...

  8. SSH电力项目四-显示首页

    1.登录页面: 将上一节中的页面放到/WEB-INF/page/目录下,需要登录后才能访问该页面: 对应页面:/WEB-INF/page/menu/home.jsp <%@ page langu ...

  9. linux的ls命令输出结果的逐条解释

    转自:http://blog.csdn.net/god123209/article/details/7193485 ls 命令的含义是list显示当前目录中的文件名字.注意不加参数它显示除隐藏文件外的 ...

  10. Linux系统 centOS 更换软件安装源

    阿里云Linux安装软件镜像源阿里云是最近新出的一个镜像源.得益与阿里云的高速发展,这么大的需求,肯定会推出自己的镜像源.阿里云Linux安装镜像源地址:http://mirrors.aliyun.c ...