POJ 4046 Sightseeing
2015-09-06 13:46
183 查看
Sightseeing
Time Limit: 5000msMemory 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
2012 Jinhua ACM-ICPC Provincial Contest解题:最短路
#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 = 2010; int head[maxn],tot,n,m,q,p[maxn],from[maxn*100],to[maxn*100]; bool done[maxn]; LL d[maxn],ans[maxn*100]; struct arc { int to,w,next; arc(int x = 0,int y = 0,int z = -1) { to = x; w = y; next = z; } } e[100010]; 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 = 1; i <= n; ++i){ d[i] = INF; done[i] = false; } d[s] = 0; qq.push(pil(0,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 = 0; 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 = 1; i <= n; ++i) scanf("%d",p+i); memset(head,-1,sizeof head); tot = 0; while(m--){ scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } scanf("%d",&q); for(int i = 0; i < q; ++i){ scanf("%d%d",from+i,to+i); ans[i] = INF; } for(int i = 1; i <= n; ++i) dijkstra(i); for(int i = 0; i < q; ++i) printf("%I64d\n",ans[i] == INF?-1:ans[i]); puts(""); } return 0; }
View Code
相关文章推荐
- 8-30 Bash环境配置和linux上的特殊权限说明
- portlet 与 servlet 区别,问题:传值,方法与返回值命名问题
- ABAP普通屏幕的输入框设置必填项
- hdu5429(BestCoder Round #54 (div.2) 1003题)
- phpMailer在thinkPHP框架中邮件发送
- Stack - Min Stack
- Unity 接入 ShareSDK 的微信分享
- 8-30 文件查找命令find使用说明和练习
- Effective Java 英文 第二版 读书笔记 Item 5:Avoid creating unnecessary objects.
- Linux添加虚拟网卡的多种方法
- MongoDB学习三
- Java主线程等待所有子线程执行完毕再执行解决办法集
- 模拟+贪心 URAL 1804 The Machinegunners in a Playoff
- iBatisnet系列(二) 配置运行环境和日志处理
- CentOS 6.1 x64部署MFS
- Git&GitHub
- Git&GitHub
- Git&GitHub
- Git&GitHub
- Git&GitHub