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)

#include<iostream>
#include<algorithm>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int dist[][];
int dp[][];
int cost[];
int n,m,q;
int maxint=;
int s[];
int c[][];
void solve(int v){
for(int i=;i<=n;i++){
if(cost[i]>cost[v])continue;
dist[v][i]=c[v][i];
s[i]=; }
s[v]=;
dist[v][v]=;
for(int i=;i<=n;i++){
int temp=maxint;
int u=v;
for(int j=;j<=n;j++){
if(!s[j]&&cost[j]<=cost[v]&&dist[v][j]<temp)
{
temp=dist[v][j];
u=j; } }
s[u]=;
for(int j=;j<=n;j++){ if(!s[j]&&c[u][j]<maxint&&cost[j]<=cost[v])
{ int min =dist[v][u]+c[u][j];
if(dist[v][j]>min)
{
dist[v][j]=min;
}
} }
}
}
void solve2(){ for(int i=;i<=n;i++)
solve(i); }
int query(int s,int t){
int temp=maxint;
for(int i=;i<=n;i++)
{
temp=min(temp,dist[i][s]+dist[i][t]+cost[i]); }
return temp;
}
int main(){
int ba=;
int t=;
while(scanf("%d%d%d",&n,&m,&q)!=EOF&&n!=){ if(ba)printf("\n");
else
ba=;
for(int i=;i<=n;i++)
scanf("%d",&cost[i]);
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
c[i][j]=maxint;
dist[i][j]=maxint;
}
}
for(int i=;i<=m;i++){
int ss,sf,sg;
scanf("%d%d%d",&ss,&sf,&sg);
if(sg<c[ss][sf]){
c[ss][sf]=sg;
c[sf][ss]=sg;
}
}
solve2(); t++;
printf("Case #%d\n",t);
for(int i=;i<=q;i++){
int rr,rt;
scanf("%d%d",&rr,&rt);
int ah=query(rr,rt);
if(ah==maxint)
printf("-1\n");
else
printf("%d\n",ah); } } }

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. careercup-数组和字符串1.8

    1.8 假定有一个方法isSubstring,可检查一个单词是否为其他字符串的子串.给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成,要求只能调用一次isSubstring.旋转字符串: ...

  2. JAAS LOGIN IN WEBLOGIC SERVER--reference

    The Java Authentication and Authorization Service (JAAS) is a standard extension to the security in ...

  3. hdu2049.java

    hdu 2049 不容易系列之(4)——考新郎 (组合+错排) 国庆期间,省城HZ刚刚举行了一场盛大的集体婚礼,为了使婚礼进行的丰富一些,司仪临时想出了有一个有意思的节目,叫做"考新郎&qu ...

  4. 网站优化的经验和技巧--精简高效的C#

    对大型网站,技术涉及面非常广,对硬件,软件,编程语言,Web Service,防火墙等等有很高要求.    面对大量用户,高并发请求,可以使用高性能服务器,高性能编程语言,高性能数据库,加大带宽等,这 ...

  5. CSS画三角形引发的一些思考

      今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...

  6. ajax 基础教程

    这是一本什么书?这是一本技术类的书籍,主要从历史.XMLHttpRequest对象.怎么样于服务器交互.构建完备的Ajax开发工具箱.使用jsUnit测试javascript 代码,总之就是让我们从这 ...

  7. JavaScript的垃圾回收

    来自MDN,Memory Management 简介 在底层语言中,比如C,有专门的内存管理机制,比如malloc() 和 free().而Javascript是有垃圾回收(garbage colle ...

  8. 7第七章联接和APPLY运算符(转载)

    7第七章联接和APPLY运算符 原文链接 本文由豆约翰博客备份专家远程一键发布

  9. 使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比

    今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...

  10. iOS 跳转到应用所在的App Store市场

    代码入下 #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @end ...