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. android之Java+html+javascript混合开发

    android开发,除了使用原生态的开发方式之外,还可以使用java+html+javascript混合开发的方式来开发,这样可以节省大量的开发时间,同时还可以使不同设备的用户获得相同的用户体验.好了 ...

  2. Java基础知识强化之IO流笔记31:转换流出现的原因和格式

    1. 由于字节流操作中文不是特别方便,所以Java就提供了转换流.  字符流 = 字节流 + 编码表 2. 编码表 由字符及其对应数值组成的一张表 常见的编码表: • ASCII/Unicode字符集 ...

  3. Java基础知识强化之IO流笔记30:字节流4种方式复制mp4并测试效率

    1. 需求:把e:\\哥有老婆.mp4 复制到当前项目目录下的copy.mp4中 字节流四种方式复制文件: • 基本字节流一次读写一个字节 • 基本字节流一次读写一个字节数组 • 高效字节流一次读写一 ...

  4. ios中创建可以拖动的view原理和实现详解

    有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)to ...

  5. Linux下rsync增加SSH端口号的用法

    rsync默认使用SSH的22号端口,为了安全起见,很多机器更改了SSH默认的端口号,对应rsync命令的用法为: rsync -e 'ssh -p 1234' username@hostname:S ...

  6. Sliverlight linq中的数组筛选数据库中的数据

    首先 什么是linq呢 ? LINQ即Language Integrated Query(语言集成查询),LINQ是集成到C#和Visual Basic.NET这些语言中用于提供查询数据能力的一个新特 ...

  7. javascript 之原型理解

    最近一直在了解javascript原型的问题,也算是理解了一点,希望把我所理解的,用简单的例子和说明,让更多人清除的去理解javascript原型 1,原型 prototype 是一个什么东西 我们创 ...

  8. jquery mobile入门资料

    由于项目中用到了,就去看了一下视频,然后进一步的找找资源,最后自己再总结一遍!(就是动手操作一遍,不论你感觉多简单,只有动手之后,你才有可能有收获) 当然如果你喜欢看文档可以到官网仔细研究,不过喜欢快 ...

  9. linux常用命令(自我积累)

    创建目录:mkdir + 目录名 使文件可执行:chmod +x filename 执行文件:./filename 来执行您的脚本 {程序必须以下面的行开始(必须方在文件的第一行): #!/bin/s ...

  10. linux rman shell

    # make direcory for backset file and scripts file,in my case /backup/db_bak cd   /backup/db_bak mkdi ...