Asterix and Obelix
题意:
地图上有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的更多相关文章
- UVA 10246 Asterix and Obelix
题意:每个城市举办庆祝有一定的花费,A在路径上会选择庆祝花费最大的城市 让你求,A回家所花的路费和庆祝费最少,也就是说并不是最短路径就是结果, 还有可能就是路费比最短路径的多,但是庆祝费就比它的少,总 ...
- KMP CF126B Password
Description Asterix,Obelix和他们的临时伙伴Suffix.Prefix已经最终找到了和谐寺.然而和谐寺大门紧闭,就连Obelix的运气也没好到能打开它. 不久他们发现了一个字符 ...
- UVA 10256 The Great Divide(凸包划分)
The Great Divide Input: standard input Output: standard output Time Limit: 8 seconds Memory Limit: 3 ...
- Codeforces(Round #93) 126 B. Password
B. Password time limit per test 2 seconds memory limit per test 256 megabytes Asterix, Obelix an ...
- codeforces 126B
Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. ...
- Codeforces A. Password(KMP的nxt跳转表)
题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- PHP生成器Generators
下文的第一个逐行读取文件例子用三种方式实现;普通方法,迭代器和生成器,比较了他们的优缺点,很好,可以引用到自己的代码中 ,支持的php版本(PHP 5 >= 5.5.0) 后面的yield讲解, ...
- 论文笔记之:Progressive Neural Network Google DeepMind
Progressive Neural Network Google DeepMind 摘要:学习去解决任务的复杂序列 --- 结合 transfer (迁移),并且避免 catastrophic f ...
- 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 ...
随机推荐
- 3 Ways of JDK Source Code Attachment in Eclipse---reference
You wanna look at a JVM class while you are coding and you cannot. Here is the solution. First of al ...
- linux解压缩命令
1.tar -cvf /data/sc2.tar /data (只打包,不压缩) 把/data下的文件打包成 sc.tar 上面两个都是绝对路径噢 tar -zcvf /data/sc2.tar.g ...
- oracle授权另外一个用户访问自己创建的数据对象
oracle安装好之后,有一个默认的scott用户,该用户有一个默认的emp表,怎样让新创建的用户也能够访问这个表呢? 授权xiaoming这个用户访问emp表,但是xiaoming只有select权 ...
- CGI初识
---恢复内容开始--- 转自http://www.moon-soft.com/program/bbs/readelite887957.htm 用 C/C++ 写 CGI 程序 小传(zhcharle ...
- js--小结①
- SQL SERVER 高级编程 - 自定义函数 拾忆
每个人都很忙,但是花10分钟复习下,总结下基础东西还是很有益处的. 背景: 总结一句,使用简便,还能递归,是的SQL更简洁,相对比一大堆的关联语句,而且关联一大堆还不一定实现特定功能.而且共用部分可以 ...
- 完美解决 未能打开编辑器:Unmatched braces in the pattern.
Eclipse出现这个问题而不能查看源代码 原因就是语言包的问题 出现这个问题了 一定是安装了中文或者多国语言包 下面我就来交大家解决的办法 超简单的 第一步 配置自己Eclipse的启动参数 ecl ...
- Stream To String , String To Stream
public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader stremR ...
- 动效解析工厂:Mask 动画
转载自:http://www.cocoachina.com/ios/20160214/15250.html 前言:很多动效都是多种动画的组合,有时候你可能只是需要其中某个动画,但面对庞杂的代码库或是教 ...
- C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN
C# DataTable的詳細用法 - hcw_peter的专栏 - 博客频道 - CSDN.NET 在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够 ...