uva10246:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1187

题意:

地图上有n个城市和某些城市间直达的道路,每条道路都有过路费,在每个城市举办宴会的花费也是已知的,现在给出A和B的位置,瘦陀陀在城市A,胖陀 陀在另一个未知的城市,两人要到城市X举办宴会,要求举办宴会的城市必须是瘦陀陀回家路线中举办宴会最贵的一个城市。求胖陀佗与瘦陀陀回到B的最小花费。 程序会接受多次询问,每次询问都应该立即给出最小的花费。

首先预处理,对于每个点x,首先删除比其举办宴会花费贵的点,由剩下的点组成一个图,求X到这些点的最短距离,可以直接读出AX和BX的最下花费,对每次询问,需要O(n)

  1. #include<iostream>
  2. #include<algorithm>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. using namespace std;
  8. int dist[][];
  9. int dp[][];
  10. int cost[];
  11. int n,m,q;
  12. int maxint=;
  13. int s[];
  14. int c[][];
  15. void solve(int v){
  16. for(int i=;i<=n;i++){
  17. if(cost[i]>cost[v])continue;
  18. dist[v][i]=c[v][i];
  19. s[i]=;
  20.  
  21. }
  22. s[v]=;
  23. dist[v][v]=;
  24. for(int i=;i<=n;i++){
  25. int temp=maxint;
  26. int u=v;
  27. for(int j=;j<=n;j++){
  28. if(!s[j]&&cost[j]<=cost[v]&&dist[v][j]<temp)
  29. {
  30. temp=dist[v][j];
  31. u=j;
  32.  
  33. }
  34.  
  35. }
  36. s[u]=;
  37. for(int j=;j<=n;j++){
  38.  
  39. if(!s[j]&&c[u][j]<maxint&&cost[j]<=cost[v])
  40. {
  41.  
  42. int min =dist[v][u]+c[u][j];
  43. if(dist[v][j]>min)
  44. {
  45. dist[v][j]=min;
  46. }
  47. }
  48.  
  49. }
  50. }
  51. }
  52. void solve2(){
  53.  
  54. for(int i=;i<=n;i++)
  55. solve(i);
  56.  
  57. }
  58. int query(int s,int t){
  59. int temp=maxint;
  60. for(int i=;i<=n;i++)
  61. {
  62. temp=min(temp,dist[i][s]+dist[i][t]+cost[i]);
  63.  
  64. }
  65. return temp;
  66. }
  67. int main(){
  68. int ba=;
  69. int t=;
  70. while(scanf("%d%d%d",&n,&m,&q)!=EOF&&n!=){
  71.  
  72. if(ba)printf("\n");
  73. else
  74. ba=;
  75. for(int i=;i<=n;i++)
  76. scanf("%d",&cost[i]);
  77. for(int i=;i<=n;i++){
  78. for(int j=;j<=n;j++){
  79. c[i][j]=maxint;
  80. dist[i][j]=maxint;
  81. }
  82. }
  83. for(int i=;i<=m;i++){
  84. int ss,sf,sg;
  85. scanf("%d%d%d",&ss,&sf,&sg);
  86. if(sg<c[ss][sf]){
  87. c[ss][sf]=sg;
  88. c[sf][ss]=sg;
  89. }
  90. }
  91. solve2();
  92.  
  93. t++;
  94. printf("Case #%d\n",t);
  95. for(int i=;i<=q;i++){
  96. int rr,rt;
  97. scanf("%d%d",&rr,&rt);
  98. int ah=query(rr,rt);
  99. if(ah==maxint)
  100. printf("-1\n");
  101. else
  102. printf("%d\n",ah);
  103.  
  104. }
  105.  
  106. }
  107.  
  108. }

Asterix and Obelix的更多相关文章

  1. UVA 10246 Asterix and Obelix

    题意:每个城市举办庆祝有一定的花费,A在路径上会选择庆祝花费最大的城市 让你求,A回家所花的路费和庆祝费最少,也就是说并不是最短路径就是结果, 还有可能就是路费比最短路径的多,但是庆祝费就比它的少,总 ...

  2. KMP CF126B Password

    Description Asterix,Obelix和他们的临时伙伴Suffix.Prefix已经最终找到了和谐寺.然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它. 不久他们发现了一个字符 ...

  3. UVA 10256 The Great Divide(凸包划分)

    The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ...

  4. Codeforces(Round #93) 126 B. Password

    B. Password time limit per test  2 seconds memory limit per test  256 megabytes   Asterix, Obelix an ...

  5. codeforces 126B

    Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. ...

  6. Codeforces A. Password(KMP的nxt跳转表)

    题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  7. PHP生成器Generators

    下文的第一个逐行读取文件例子用三种方式实现;普通方法,迭代器和生成器,比较了他们的优缺点,很好,可以引用到自己的代码中 ,支持的php版本(PHP 5 >= 5.5.0) 后面的yield讲解, ...

  8. 论文笔记之:Progressive Neural Network Google DeepMind

    Progressive Neural Network  Google DeepMind 摘要:学习去解决任务的复杂序列 --- 结合 transfer (迁移),并且避免 catastrophic f ...

  9. uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx

    As you may know from the comic \Asterix and the Chieftain's Shield", Gergovia consists of one s ...

随机推荐

  1. 3 Ways of JDK Source Code Attachment in Eclipse---reference

    You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...

  2. linux解压缩命令

    1.tar -cvf /data/sc2.tar /data (只打包,不压缩) 把/data下的文件打包成 sc.tar  上面两个都是绝对路径噢 tar -zcvf /data/sc2.tar.g ...

  3. oracle授权另外一个用户访问自己创建的数据对象

    oracle安装好之后,有一个默认的scott用户,该用户有一个默认的emp表,怎样让新创建的用户也能够访问这个表呢? 授权xiaoming这个用户访问emp表,但是xiaoming只有select权 ...

  4. CGI初识

    ---恢复内容开始--- 转自http://www.moon-soft.com/program/bbs/readelite887957.htm 用 C/C++ 写 CGI 程序 小传(zhcharle ...

  5. js--小结①

  6. SQL SERVER 高级编程 - 自定义函数 拾忆

    每个人都很忙,但是花10分钟复习下,总结下基础东西还是很有益处的. 背景: 总结一句,使用简便,还能递归,是的SQL更简洁,相对比一大堆的关联语句,而且关联一大堆还不一定实现特定功能.而且共用部分可以 ...

  7. 完美解决 未能打开编辑器:Unmatched braces in the pattern.

    Eclipse出现这个问题而不能查看源代码 原因就是语言包的问题 出现这个问题了 一定是安装了中文或者多国语言包 下面我就来交大家解决的办法 超简单的 第一步 配置自己Eclipse的启动参数 ecl ...

  8. Stream To String , String To Stream

    public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader stremR ...

  9. 动效解析工厂:Mask 动画

    转载自:http://www.cocoachina.com/ios/20160214/15250.html 前言:很多动效都是多种动画的组合,有时候你可能只是需要其中某个动画,但面对庞杂的代码库或是教 ...

  10. C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN

    C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...