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. nohub和重定向文件

    1.如果使用远程连接的Linux的方式并想后台运行执行如下命令: 格式:nohup <程序名> & 比如:nohup /usr/local/collection/bin/start ...

  2. VS2015编译JPEG9b源码

    输入:nmake -f makefile.vc libjpeg.lib 出现错误:找不到win32.mak文件 按照网上说的,这个win32.mak可以在C:\Program Files (x86)\ ...

  3. php 连接mongdb的类

    <?php/** * php 连接mongdb的类的封装 * @author 李秀然 */ class mongdb{ private $host;//"mongodb://admin ...

  4. JAVA中替换字符的方法replace和replaceAll 区别

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSe ...

  5. 《转》python学习(9)字典

    转自 http://www.cnblogs.com/BeginMan/p/3156960.html 一.映射类型 我理解中的映射类型是:键值对的关系,键(key)映射值(value),且它们是一对多的 ...

  6. poj_2195 最小费用最大流

    题目大意 一个nxm的地图,地图上的横纵交错成nxm个交叉点,其中有k个交叉点为房间,k个交叉点为k个小人的初始位置.小人可以在地图上沿着水平或垂直方向行走,每走一步的代价为1.求这k个小人分别到达k ...

  7. const T* 和 T* const

    使用c++的时候,经常会在 const int *p 和 int * const p这个地方迷惑.这里记录一下: const int *p  = int const *p //这里const后面的为* ...

  8. 【linux】Crontab 定时任务 使用实例

    1 使用putty 登录linux 服务器 2 输入以下命令.查看已有的定时任务 crontab -l 3 输入  以下命令,进入定时任务文件 crontab -e 4  键盘 选择 i  键 进行输 ...

  9. Java中DESKeySpec类

    此类位于 javax.crypto.spec 包下.声明如下: public class DESKeySpec extends Object implements KeySpec 此类指定一个 DES ...

  10. LeetCode——Implement Queue using Stacks

    Description: Implement the following operations of a queue using stacks. push(x) -- Push element x t ...