Sightseeing

Time Limit: 5000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 4046
64-bit integer IO format: %lld      Java class name: Main

 
CC and MM arrive at a beautiful city for sightseeing. They have found a map of the city on the internet to help them find some places to have meals. They like buffet restaurants (self-service restaurants) and there are n such restaurants and m roads. All restaurants are numbered from 1 to n. Each road connects two different restaurants. They know the price of every restaurant. They go by taxi and they know the taxi fee of each road.
Now they have Q plans. In each plan, they want to start from a given restaurant, pass none or some restaurants and stop at another given restaurant. They will have a meal at one of those restaurants. CC does not want to lose face, so he will definitely choose the most expensive one among the restaurants which they will pass (including the starting one and the stopping one). But CC also wants to save money, so he want you to help him figure out the minimum cost path for each plan.
 

Input

There are multiple test cases in the input. 
For each test case, the first line contains two integers, n, m(1<=n<=1000, 1<=m<=20000),meaning that there are n restaurants and m roads.
The second line contains n integers indicating the price of n restaurant. All integers are smaller than 2×109.
The next m lines, each contains three integers: x, y and  z(1<=x, y <=n, 1<=z<=2×109), meaning that there is a road between x and y, and the taxi fee of this road is z.
Then a single line containing an integer Q follows, meaning that there are Q plans (1<=Q<=20000).
The next Q lines, each contains two integers: s and t (1<=s, t <= n) indicating the starting restaurant and stopping restaurant of each plan.
The input ends with n = 0 and m = 0.
 

Output

For each plan, print the  minimum cost in a line. If there is no path from the starting restaurant to the stopping restaurant, just print -1 instead.
Print a blank line after each test case.
 

Sample Input

6 7
1 2 3 4 5 6
1 2 1
2 3 2
3 4 3
4 5 4
1 5 5
2 5 2
1 4 3
5
1 4
2 3
1 5
3 5
1 6
2 1
10 20
1 2 5
1
1 2
0 0

Sample Output

7
5
8
9
-1 25

Source

 
解题:最短路
 
 #include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>
#define pil pair<LL,int>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = ;
int head[maxn],tot,n,m,q,p[maxn],from[maxn*],to[maxn*];
bool done[maxn];
LL d[maxn],ans[maxn*];
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
priority_queue<pil,vector<pil >,greater<pil > >qq;
void dijkstra(int s){
while(!qq.empty()) qq.pop();
for(int i = ; i <= n; ++i){
d[i] = INF;
done[i] = false;
}
d[s] = ;
qq.push(pil(,s));
while(!qq.empty()){
int u = qq.top().second;
qq.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(p[e[i].to] <= p[s] && !done[e[i].to] && d[e[i].to] > d[u] + e[i].w){
d[e[i].to] = d[u] + e[i].w;
qq.push(pil(d[e[i].to],e[i].to));
}
}
}
for(int i = ; i < q; ++i)
if(d[from[i]] < INF && d[to[i]] < INF)
ans[i] = min(ans[i],d[from[i]] + d[to[i]] + p[s]);
}
int main() {
int u,v,w;
while(scanf("%d%d",&n,&m),n||m) {
for(int i = ; i <= n; ++i)
scanf("%d",p+i);
memset(head,-,sizeof head);
tot = ;
while(m--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
scanf("%d",&q);
for(int i = ; i < q; ++i){
scanf("%d%d",from+i,to+i);
ans[i] = INF;
}
for(int i = ; i <= n; ++i) dijkstra(i);
for(int i = ; i < q; ++i)
printf("%I64d\n",ans[i] == INF?-:ans[i]);
puts("");
}
return ;
}

POJ 4046 Sightseeing的更多相关文章

  1. POJ 4046 Sightseeing 枚举+最短路 好题

    有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...

  2. POJ 1637 Sightseeing tour(最大流)

    POJ 1637 Sightseeing tour 题目链接 题意:给一些有向边一些无向边,问能否把无向边定向之后确定一个欧拉回路 思路:这题的模型很的巧妙,转一个http://blog.csdn.n ...

  3. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  4. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  5. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  6. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

  7. POJ 3621 Sightseeing Cows(最优比例环+SPFA检测)

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10306   Accepted: 3519 ...

  8. POJ - 3463 Sightseeing 最短路计数+次短路计数

    F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...

  9. POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9276   Accepted: 3924 ...

随机推荐

  1. yun install java

    # yum install java-1.7.0-openjdk # yum install java-1.7.0-openjdk-devel 需要执行以上两步 查看版本 # java -versio ...

  2. QFileDialog关于选择文件对话框中的几个信号的说明(currentChanged,directoryEntered,fileSelected,filterSelected)

    QFileDialog关于选择文件对话框中的几个信号 实例: openFile::openFile(QWidget *parent) :QWidget(parent),ui(new Ui::openF ...

  3. B2241 打地鼠 暴力模拟

    大水题!!!30分钟AC(算上思考时间),直接模拟就行,加一个判断约数的剪枝,再多加几个剪枝就可以过(数据巨水) 我也就会做暴力的题了. 题干: Description 打地鼠是这样的一个游戏:地面上 ...

  4. gulp安装成功但是无法使用

    gulp安装正常,但是查看gulp -v和使用gulp的时候报错, 原因:缺少环境变量或环境变量错误. 查找环境变量的方法:在dos下输入npm config get prefix就会显示一个地址,这 ...

  5. bzoj 1503郁闷的出纳员(splay)

    1503: [NOI2004]郁闷的出纳员 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 11759  Solved: 4163[Submit][Stat ...

  6. Python可迭代序列排序总结

    列表排序 示例:lst = [12, 6, 1, 3, 10] 方法一:使用sort def list_sort(lst): lst.sort() # 就地排序,没有返回值 return lst 补充 ...

  7. word2vec改进之Hierarchical Softmax

    首先Hierarchical Softmax是word2vec的一种改进方式,因为传统的word2vec需要巨大的计算量,所以该方法主要有两个改进点: 1. 对于从输入层到隐藏层的映射,没有采取神经网 ...

  8. A - Team

    Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...

  9. .net中的WebForm引人MVC的控制器

    当下.net中比较火的模式MVC模式,说实话对于菜鸟的我还没有遇到一个比较健全的MVC模式的项目也是比较遗憾.偶然间在网上看到WebForm实现MVC中的模式(主要是控制器...)就学习了一波,下面是 ...

  10. Windows phone开发之文件夹与文件操作系列(一)文件夹与文件操作

    Windows phone7中文件的存储模式是独立的,即独立存储空间(IsolatedStorage).对文件夹与文件操作,需要借助IsolatedStorageFile类. IsolatedStor ...