标签:des style blog color os io ar strong for
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are provides fantastic ski conditions, many ski lifts and slopes of various difficulty profiles. However, some lifts go faster than others, and some are so popular that a queue forms at the bottom.
Per is a beginner skier and he is afraid of lifts, even though he wants to ski as much as possible. Now he sees that he can take several different lifts and then many different slopes
or some other lifts, and this freedom of choice is starting to be too puzzling...
He would like to make a ski journey that:
Can you help Per find the least scary ski journey? A ski resort contains n places, m slopes, and k lifts (2 <= n <= 1000, 1 <= m <= 1000, 1 <= k <= 1000). The slopes and lifts always lead from some place to another place: the slopes lead from places with higher altitude to places with lower altitude and lifts vice versa (lifts cannot be taken downwards).
Input
The first line of the input contains the number of cases - the number of ski resorts to process. Each ski resort is described as follows: the first line contains three integers n, m, and k. The following m lines describe the slopes: each line contains three integers - top and bottom place of the slope (the places are numbered 1 to n), and the time it takes to go down the slope (max. 10000). The final k lines describe the lifts by three integers - the bottom and top place of the lift, and the time it takes to wait for the lift in the queue and be brought to its top station (max. 10000). You can assume that no two places are connected by more than one lift or by more than one slope.
Output
For each input case, the program should print two lines. The first line should contain a space-separated list of places in the order they will be visited - the first place should be the same as the last place. The second line should contain the ratio of the time spent in the slopes to the time spent on the lifts or wating for the lifts. The ratio should be rounded to the closest 1/1000th. If there are two possibilities, then the rounding is away from zero (e.g., 1.9812 and 1.9806 become 1.981, 3.1335 becomes 3.134, and 3.1345 becomes 3.135). If there are multiple journeys that prior to rounding are equally scary, print an arbitrary one.
Sample Input
1 5 4 3 1 3 12 2 3 6 3 4 9 5 4 9 4 5 12 5 1 12 4 2 18
Sample Output
4 5 1 3 4 0.875
题意:求坐电梯时间和滑雪时间的比值的最大值。用spfa求出每个点的时间,主要是输出路径感觉比较棘手。
#include"stdio.h" #include"string.h" #include"vector" #include"queue" #include"iostream" #include"algorithm" using namespace std; #define N 1005 const int inf=(int)1e10; struct node { int v,w,next; }g1[N],g2[N]; int t1,t2,dis1[N][N],dis2[N][N]; int head1[N],head2[N],pre1[N][N],pre2[N][N]; void add1(int u,int v,int w) //pre数组记录路径 { g1[t1].v=v; g1[t1].w=w; g1[t1].next=head1[u]; head1[u]=t1++; } void add2(int u,int v,int w) { g2[t2].v=v; g2[t2].w=w; g2[t2].next=head2[u]; head2[u]=t2++; } void spfa1(int u) { int i,s,v,mark[N]; memset(mark,0,sizeof(mark)); mark[u]=1; s=u; queue<int>q; q.push(s); dis1[u][u]=0; while(!q.empty()) { u=q.front(); q.pop(); mark[u]=0; for(i=head1[u];i!=-1;i=g1[i].next) { v=g1[i].v; if(dis1[s][u]!=-1&&dis1[s][v]<dis1[s][u]+g1[i].w) { dis1[s][v]=dis1[s][u]+g1[i].w; pre1[s][v]=u; if(!mark[v]) { q.push(v); mark[v]=1; } } } } } void spfa2(int u) { int i,s,v,mark[N]; memset(mark,0,sizeof(mark)); mark[u]=1; s=u; queue<int>q; q.push(s); dis2[u][u]=0; while(!q.empty()) { u=q.front(); q.pop(); mark[u]=0; for(i=head2[u];i!=-1;i=g2[i].next) { v=g2[i].v; if(dis2[s][u]!=inf&&dis2[s][v]>dis2[s][u]+g2[i].w) { dis2[s][v]=dis2[s][u]+g2[i].w; pre2[s][v]=u; if(!mark[v]) { q.push(v); mark[v]=1; } } } } } /*void path(int s,int t,int pre[N][N]) { if(s==t) return ; path(pre[t][s],t,pre); printf(" %d",s); }*/ int main() { int T,n,m,k,u,v,w,i,j; scanf("%d",&T); while(T--) { scanf("%d%d%d",&n,&m,&k); memset(head1,-1,sizeof(head1)); memset(head2,-1,sizeof(head2)); t1=t2=0; for(i=0;i<m;i++) { scanf("%d%d%d",&u,&v,&w); add1(u,v,w); } for(i=0;i<k;i++) { scanf("%d%d%d",&u,&v,&w); add2(u,v,w); } memset(pre1,-1,sizeof(pre1)); memset(pre2,-1,sizeof(pre2)); memset(dis1,-1,sizeof(dis1)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) dis2[i][j]=inf; } for(i=1;i<=n;i++) { spfa1(i); //上去求最大值 spfa2(i); //下来求最小值 } double ans=0; int s,t; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j||dis2[i][j]>=inf||dis1[j][i]==-1) continue; double tmp=1.0*dis1[j][i]/dis2[i][j]; if(tmp>ans) { ans=tmp; s=i;t=j; } } } printf("%d ",s); i=s;j=t;k=0; int a[N]; //记录答案路径 while(pre2[i][j]!=s) //从终点向起始点找路径 { //到起始点结束 a[k++]=pre2[i][j]; j=pre2[i][j]; } for(i=k-1;i>=0;i--) printf("%d ",a[i]); printf("%d ",t); i=t;j=s;k=0; while(pre1[i][j]!=t) { a[k++]=pre1[i][j]; j=pre1[i][j]; } for(i=k-1;i>=0;i--) printf("%d ",a[i]); printf("%d\n",s); // path(t,s,pre2); // path(s,t,pre1); printf("%.3f\n",ans); } return 0; }
zoj 3088 Easter Holidays (spfa )
标签:des style blog color os io ar strong for
原文地址:http://blog.csdn.net/u011721440/article/details/39059633