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. ionic简单路由及页面传参

    1)页面跳转及传参方法 angular.module('app.routes', [])//routes路由模型 .config(function($stateProvider, $urlRouter ...

  2. 当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决

    在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面 ...

  3. cmp()

    cmp(x, y) 用于比较两个对象的大小,如果 x > y 返回 1 ,如果 x = y 返回 0 ,如果 x < y 返回 -1 In [23]: cmp(5, 2) Out[23]: ...

  4. m2014-architecture-imgserver->Lighttpd +mod_mem_cache的效果简直太好了

    公司的图片服务器一直以来负载都比较高,原因是图片比较分散而且比较小.经常把iowait搞的特别的高.但是只有一台机器也法用squid,经测试squid和apache在同一台机器效果会很糟糕的.因为sq ...

  5. 在ubuntu 10.04 上QGIS的安装步骤

    进入管理员账户后,打开/etc/apt/sources.list. 添 加 deb http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubun ...

  6. android基础---->NDK的使用

    NDK的发布,使“Java+C”的开发方式终于转正,成为官方支持的开发方式.NDK将是Android平台支持C开发的开端,今天我们开始ndk的学习. NDK的简要说明 ndk是什么: The Nati ...

  7. fabric入门

    author: headsen  chen date: 2018-08-12  23:13:16 1,安装 yum -y install epel-releaseyum -y install fabr ...

  8. jQuery --- 收集表单

    第一种:常用获取对应表单的value值进行收集: 第二种:用jQuery的 serializeArray() 方法收集: <form id="change"> < ...

  9. Linux系统下 Rsync 环境安装搭建

    一.Rsync简介 1.认识 Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 ...

  10. javascript飞机大战-----009游戏结束

    /* 游戏引擎 */ var Engine = { //刚开始的游戏状态 gameStatus:false, //所以敌机 enemy:{}, //子弹 bullet:{}, //得分 scroe:0 ...