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. cookie记录用户名和密码

    getAttribute和getParameter的区别: request.getAttribute():是request时设置的变量的值,用request.setAttribute("na ...

  2. 版本控制-git的使用

    最近刚到公司实习,知道了版本控制,并略微会用了git的版本控制,下面就简单的记录一下,给健忘的自己日后回顾~ 师傅教我的是命令行的使用,所以暂时只说命令行的方法, 1.首先进入CLone到本地的那个本 ...

  3. CentOS 6.7下iPython提示“WARNING: Readline services not available or not loaded.”的解决办法

    yum install readline-devel 然后,使用pip或者easy_install安装readline即可 pip install readline

  4. winform windowsmediaplayer的属性

    首先将C:\WINDOWS\system32下的wmp.dll应用到项目中: WMPLib.WindowsMediaPlayerClass player = new WMPLib.WindowsMed ...

  5. 自定义组合控件,适配器原理-Day31

    自定义组合控件,适配器原理-Day31 mobile2.1 主页定义 手机上锁功能 1.弹出设置密码框. 手机下载进度 自定定义控件 控件的属性其实就是控件类一个属性设置属性调用类的set方法方法, ...

  6. 第八章 CTE 递归 及 分组汇总 高级部分(多维数据集)

    UNION 等集合操作符: UNION 等以第一个 SELECT  的 列明 作为 整个结果集的列明,整个结果集 唯一认可的 唯一逻辑处理阶段 是 ORDER BY  这个意思是说 只有 ORDER ...

  7. 中文字体在CSS中的表达方式

    在写一个网站的样式表的时候,都会不可避免地用到一些中文字体,比如说微软雅黑.黑体等,除非是做英文站,或者说你乐意整站都用浏览器默认的字体,那我也算服了U.在 CSS 中写入中文字体的方法一般采用 fo ...

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

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

  9. Error Domain=com.google.greenhouse Code=-102

    *** Terminating app due to uncaught exception 'com.google.greenhouse', reason: 'Error Domain=com.goo ...

  10. cmake简易教程

    用cmake替代makefile,构建项目还是蛮简单实用的. 工程目录下src放源代码,build保存所有的编译过程和结果. 首先看看src目录下的源代码结构: 最顶层CMakeLists.txt内容 ...