Sightseeing
Time Limit: 5000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 404664-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
解题:最短路
1 #include <cstdio> 2 #include <queue> 3 #include <iostream> 4 #include <cstring> 5 #define pil pair<LL,int> 6 using namespace std; 7 typedef long long LL; 8 const LL INF = 0x3f3f3f3f3f3f3f3f; 9 const int maxn = 2010; 10 int head[maxn],tot,n,m,q,p[maxn],from[maxn*100],to[maxn*100]; 11 bool done[maxn]; 12 LL d[maxn],ans[maxn*100]; 13 struct arc { 14 int to,w,next; 15 arc(int x = 0,int y = 0,int z = -1) { 16 to = x; 17 w = y; 18 next = z; 19 } 20 } e[100010]; 21 void add(int u,int v,int w) { 22 e[tot] = arc(v,w,head[u]); 23 head[u] = tot++; 24 } 25 priority_queue<pil,vector<pil >,greater<pil > >qq; 26 void dijkstra(int s){ 27 while(!qq.empty()) qq.pop(); 28 for(int i = 1; i <= n; ++i){ 29 d[i] = INF; 30 done[i] = false; 31 } 32 d[s] = 0; 33 qq.push(pil(0,s)); 34 while(!qq.empty()){ 35 int u = qq.top().second; 36 qq.pop(); 37 if(done[u]) continue; 38 done[u] = true; 39 for(int i = head[u]; ~i; i = e[i].next){ 40 if(p[e[i].to] <= p[s] && !done[e[i].to] && d[e[i].to] > d[u] + e[i].w){ 41 d[e[i].to] = d[u] + e[i].w; 42 qq.push(pil(d[e[i].to],e[i].to)); 43 } 44 } 45 } 46 for(int i = 0; i < q; ++i) 47 if(d[from[i]] < INF && d[to[i]] < INF) 48 ans[i] = min(ans[i],d[from[i]] + d[to[i]] + p[s]); 49 } 50 int main() { 51 int u,v,w; 52 while(scanf("%d%d",&n,&m),n||m) { 53 for(int i = 1; i <= n; ++i) 54 scanf("%d",p+i); 55 memset(head,-1,sizeof head); 56 tot = 0; 57 while(m--){ 58 scanf("%d%d%d",&u,&v,&w); 59 add(u,v,w); 60 add(v,u,w); 61 } 62 scanf("%d",&q); 63 for(int i = 0; i < q; ++i){ 64 scanf("%d%d",from+i,to+i); 65 ans[i] = INF; 66 } 67 for(int i = 1; i <= n; ++i) dijkstra(i); 68 for(int i = 0; i < q; ++i) 69 printf("%I64d ",ans[i] == INF?-1:ans[i]); 70 puts(""); 71 } 72 return 0; 73 }