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. MySQL源码:索引相关的数据结构

    http://www.orczhou.com/index.php/2012/11/mysql-source-code-data-structure-about-index/ 本文将尝试介绍MySQL索 ...

  2. Linux学习笔记总结--云服务器挂载磁盘

    1.通过 "fdisk -l" 命令查看 若执行fdisk -l命令,发现没有 /dev/xvdb 表明云服务无数据盘 2. 对数据盘进行分区 执行"fdisk  /de ...

  3. Android 上使用 iconfont 的一种便捷方案

    最近在学习 AIOSO(Alibaba Internal Open Source Organization,即阿里巴巴内部开源组织) 的一个子项目MMCherryUI,这是一个流式布局,可以在运行时做 ...

  4. float与double的范围和精度(摘录)

    什么是浮点数在计算机系统的发展过程中,曾经提出过多种方法表达实数.典型的比如相对于浮点数的定点数(Fixed Point Number).在这种表达方式中,小数点固定的位于实数所有数字中间的某个位置. ...

  5. 高效 jquery 的奥秘

    当你准备使用 jQuery,我强烈建议你遵循下面这些指南: 1. 缓存变量 DOM 遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $( ...

  6. linux lsof nmap netstat

    lsof -i :22    # 显示22端口当前运行的程序 lsof -c ssh  # 显示ssh进程打开的文件 lsof -p 2120  #显示进程id2120打开的文件   nmap -sP ...

  7. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  8. UESTCOJ-BiliBili, ACFun… And More!(水题)

    BiliBili, ACFun… And More! Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Ja ...

  9. JAVA SE 框架之俄罗斯方块的效果

    Mygame package com.sun.c; import java.awt.event.KeyListener; import com.sun.v.MyJpanel; import com.s ...

  10. PHP 根据值查找键名

    array_search (PHP 4 >= 4.0.5, PHP 5) mixed array_search ( mixed $needle , array $haystack [, bool ...