程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> zoj 3088 Easter Holidays (spfa )

zoj 3088 Easter Holidays (spfa )

編輯:C++入門知識

zoj 3088 Easter Holidays (spfa )


Easter Holidays

Time Limit: 1 Second Memory Limit: 32768 KB Special Judge

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:

    starts at the bottom of some lift and ends at that same spothas only two phases: in the first phase, he takes one or more lifts up, in the second phase, he will ski all the way down back to where he startedis least scary, that is the ratio of the time spent on the slopes to the time spent on the lifts or waiting for the lifts is the largest possible.

    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;
        queueq;
        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]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=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;
    }
    


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved