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 ;
}

最新文章

  1. python collections模块
  2. iOS----集成ijkplayer视频直播
  3. weiphp---------图灵机器人存在的bug。
  4. (转)用JQuery实现Fix表头表格
  5. 英特尔实感SDK 代码示例
  6. logstash 各种时间转换
  7. jQuery和DOM对象
  8. Android客户端连接服务器端,向服务器端发送请求HttpURLConnection
  9. 花了一年时间开发的TTFEditor 字体编辑器
  10. linux 下 Fatal error: Class ‘mysqli’ not found in
  11. day4 liaoxuefeng---模块
  12. 【网摘】C#.NET 在 MVC 中动态绑定下拉菜单的方法
  13. Java实现遍历N级树形目录结构
  14. sshpass安装使用
  15. ftp定时任务-日志备份
  16. hdu6395 (矩阵快速幂+分块)
  17. 03MYSQL数据库
  18. ssh原理图解
  19. TemplateBuilder Android Studio
  20. linux kernel notifier chain(事件通知链)

热门文章

  1. python2.0 s12 day8 _ python线程&amp;python进程
  2. UITableView取消选中颜色、常用操作
  3. 《C++ Primer Plus》第13章 类继承 笔记
  4. VMware创建虚拟机教程详解及问题解决
  5. 使用vim-pathogen 进行插件管理
  6. Linux学习(四)档案与目录管理
  7. MySQL 5.6 my.cnf 参数说明(转)
  8. onethink封装arclist调用文章列表!
  9. java8新增的日期时间包
  10. C#操作word之插入图片