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. iOS UIKit:Navigation Controllers

    navigation controller是一种层次结构的container view controller,即其通过一个view controllers栈来管理内部的content view con ...

  2. 256MB小内存MySQL配置优化

    [client] port = 3306 socket = /var/run/mysqld/mysqld.sock [mysqld_safe] socket = /var/run/mysqld/mys ...

  3. python np.linspace

    该函数的形式为: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) 作用为:在规定的时间内,返回固定间隔的 ...

  4. jQuery各种选择器总结

    首先介绍几个简单的: id选择器 $('#p1').html('<font color='red'>nihao</font>); 类选择器:表示页面上所有应用了a样式的标签 $ ...

  5. “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常。

    今天在项目中用log4net,App.config文件中增加了configSections节点,程序运行报错“System.Transactions.Diagnostics.DiagnosticTra ...

  6. 7z 压缩命令行工具

    命令行压缩解压一 7z 1) 简介7z,全称7-Zip, 是一款开源软件.是目前公认的压缩比例最大的压缩解压软件.主页:http://www.7-zip.org/中文主页:http://7z.spar ...

  7. Android简单例子——AlertDialog

    最近学习了一段时间的Android,在网上找了些小的Demo,自己模拟这做了下,首先谢谢那些提供例子的朋友 今天主要学习的是简单的Dialog的使用(实现退出对话框)和自定义对话框 1.实现退出对话框 ...

  8. HDU_1406 完数

    Problem Description 完数的定义:如果一个大于1的正整数的所有因子之和等于它的本身,则称这个数是完数,比如6,28都是完数:6=1+2+3:28=1+2+4+7+14. 本题的任务是 ...

  9. C语言数据输入与输出

    1 概论 C语言提供了跨平台的数据输入输出函数scanf()和printf()函数,它们可以按照指定的格式来解析常见的数据类型,例如整数,浮点数,字符和字符串等等.数据输入的来源可以是文件,控制台以及 ...

  10. 哈弗曼实现(C++)

    HuffmanCode.h #ifndef HUFFMANCODE_H #define HUFFMANCODE_H enum LRSTATUS { LEFTCHILD, //左子树 RIGHTCHIL ...