Ambulance Dispatch
Given the map of a city, with all the ambulance dispatch centers (救护车派遣中心) and all the pick-up spots marked. You are supposed to write a program to process the emergency calls. It is assumed that the callers are waiting at some pick-up spot. You must inform the nearest (that is, to take the minimum time to reach the spot) dispatch center if that center has at least one ambulance available. Note: a center without any ambulance must not be considered.
In case your options are not unique, inform the one with the largest number of ambulances available. If there is still a tie, choose the one that can pass the least number of streets to reach the spot, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers Ns (≤) and Na (≤), which are the total number of pick-up spots and the number of ambulance dispatch centers, respectively. Hence the pick-up spots are numbered from 1 to Ns, and the ambulance dispatch centers are numbered from A−1 to A−Na.
The next line gives Na non-negative integers, where the i-th integer is the number of available ambulances at the i-th center. All the integers are no larger than 100.
In the next line a positive number M (≤) is given as the number of streets connecting the spots and the centers. Then M lines follow, each describes a street by giving the indices of the spots or centers at the two ends, followed by the time taken to pass this street, which is a positive integer no larger than 100.
Finally the number of emergency calls, K, is given as a positive integer no larger than 1, followed by K indices of pick-up spots.
All the inputs in a line are separated by a space.
Output Specification:
For each of the K calls, first print in a line the path from the center that must send an ambulance to the calling spot. All the nodes must be separated by exactly one space and there must be no extra space at the beginning or the end of the line. Then print the minimum time taken to reach the spot in the next line. It is assumed that the center will send an ambulance after each call. If no ambulance is available, just print All Busy
in a line. It is guaranteed that all the spots are connected to all the centers.
题解:
#include<bits/stdc++.h> using namespace std; const int maxn=1015; const int inf=1e9; int g[maxn][maxn]; int visit[maxn][maxn]; int d[maxn][maxn]; int weight[maxn]; int numNode[maxn][maxn]; int N,M,Ns,Na,st; int pre[maxn][maxn]; void dijkstra (int s) { //for (int i=1;i<=N;i++) d[i]=inf,visit[i]=0,numNode[i]=0,pre[i]=i; d[s][s]=0; pre[s][s]=s; for (int i=1;i<=N;i++) { int u=-1,Min=inf; for (int j=1;j<=N;j++) if (!visit[s][j]&&d[s][j]<Min) { u=j; Min=d[s][j]; } if (u==-1) return; visit[s][u]=1; for (int v=1;v<=N;v++) { if (!visit[s][v]&&g[u][v]!=inf) { if (d[s][u]+g[u][v]<d[s][v]) { d[s][v]=d[s][u]+g[u][v]; numNode[s][v]=numNode[s][u]+1; pre[s][v]=u; } else if (d[s][u]+g[u][v]==d[s][v]) { if (numNode[s][u]+1<numNode[s][v]) { numNode[s][v]=numNode[s][u]+1; pre[s][v]=u; } } } } } } int zh (char * s) { if (s[0]=='A') { int ans=0; for (int i=2;i<strlen(s);i++) ans=ans*10+s[i]-'0'; return ans+Ns; } int ans=0; for (int i=0;i<strlen(s);i++) ans=ans*10+s[i]-'0'; return ans; } void dfs (int v,int st) { if (v==st) { printf("%d",v); return; } if (v>=1&&v<=Ns) printf("%d ",v); else printf("A-%d ",v-Ns); dfs(pre[st][v],st); } int main () { scanf("%d%d",&Ns,&Na); N=Ns+Na; fill(g[0],g[0]+1015*1015,inf); fill(d[0],d[0]+1015*1015,inf); for (int i=1;i<=Na;i++) scanf("%d",&weight[Ns+i]); scanf("%d",&M); for (int i=1;i<=M;i++) { char s1[20],s2[20]; scanf("%s %s",s1,s2); int x; scanf("%d",&x); int u=zh(s1); int v=zh(s2); g[u][v]=g[v][u]=x; } int K; scanf("%d",&K); for (int i=0;i<K;i++) { scanf("%d",&st); dijkstra(st); int MinDistance=inf,u=-1; for (int j=Ns+1;j<=Ns+Na;j++) { if (weight[j]==0) continue; if (d[st][j]>MinDistance) continue; if (d[st][j]<MinDistance) { MinDistance=d[st][j]; u=j; continue; } if (d[st][j]==MinDistance) { if (weight[j]>weight[u]) { u=j; } else if (weight[j]==weight[u]&&numNode[st][j]<numNode[st][u]) { u=j; } } } if (u==-1) { printf("All Busy\n"); continue; } dfs(u,st); weight[u]--; printf("\n%d\n",d[st][u]); } }