zoj 3088 Easter Holidays (spfa)

I - Easter Holidays
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 spot
  • has 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 started
  • is 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
题意:有一滑雪场,前m条路径为从上往下滑雪,后k条路径为电梯,求从某点出发回到该点的路径中t(滑雪)/t(电梯)值最大的,
分析:题目要求任意两点最长路及最短路,而且要求输出路径,数据大小为  (2 <= n <= 1000, 1 <= m <= 1000, 1 <= k <= 1000),floyd及bellman都不能输出路径,
dijkstra的话需要用n次,n^3的时间复杂度绝对超时,spfa时间复杂度O(km) m为边数 k一般为2或3,就是他了,n次spfa求最短(长)路并记录路径。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN=1005;
const int MAXM=2055;
int n,cnt,dis1[MAXN][MAXN],p1[MAXN][MAXN],inq[MAXN],first1[MAXN],dis2[MAXN][MAXN],p2[MAXN][MAXN],first2[MAXN];
int u[MAXM],v[MAXM],w[MAXM],next1[MAXM],next2[MAXM];

void spfa1(int x,int *dis,int *p,int* first,int *next)//最短路
{
    queue<int>q;
    memset(dis,0x3f,MAXN*sizeof(int));
    memset(inq,0,MAXN*sizeof(int));
    q.push(x);
    dis[x]=0;
    inq[x]=1;
    p[x]=-1;
    while(!q.empty())
    {
        int e,x=q.front();q.pop();
        inq[x]=0;
        for(e=first[x];e!=-1;e=next[e])
            if(dis[x]+w[e]<dis[v[e]])
            {
                dis[v[e]]=dis[x]+w[e];
                p[v[e]]=x;
                if(!inq[v[e]])
                {
                    q.push(v[e]);
                    inq[v[e]]=1;
                }
            }
    }
}
void spfa(int x,int dis[],int p[],int* first,int *next)//最长路
{
    queue<int>q;
    memset(dis,0,MAXN*sizeof(int));
    memset(inq,0,MAXN*sizeof(int));
    q.push(x);
    dis[x]=0;
    inq[x]=1;
    p[x]=-1;
    while(!q.empty())
    {
        int e,x=q.front();q.pop();
        inq[x]=0;
        for(e=first[x];e!=-1;e=next[e])
            if(dis[x]+w[e]>dis[v[e]])
            {
                dis[v[e]]=dis[x]+w[e];
                p[v[e]]=x;
                if(!inq[v[e]])
                {
                    q.push(v[e]);
                    inq[v[e]]=1;
                }
            }
    }
}
void add_(int a,int b,int c,int *first,int *next)
{
    u[cnt]=a;
    v[cnt]=b;
    w[cnt]=c;
    next[cnt]=first[a];
    first[a]=cnt++;
}
void showpath(int *p,int x)
{
    if(x==-1)return ;
    showpath(p,p[x]);
    printf("%d ",x);
}
int main()
{
    int N,m,k,i,j;
    cin>>N;
    while(N--)
    {
        scanf("%d%d%d",&n,&m,&k);
        int a,b,c;
        cnt=0;
        memset(first1,-1,sizeof first1);
        memset(next1,-1,sizeof next1);
        memset(first2,-1,sizeof first2);
        memset(next2,-1,sizeof next2);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_(a,b,c,first1,next1);
        }
        for(i=0;i<k;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add_(a,b,c,first2,next2);
        }
        for(i=1;i<=n;i++)
        {
            spfa(i,dis1[i],p1[i],first1,next1);
            spfa1(i,dis2[i],p2[i],first2,next2);
        }
        double ans=0;int ans1,ans2;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(i!=j&&dis2[j][i]!=INF){
                    if(ans<dis1[i][j]*1.0/dis2[j][i])ans=dis1[i][j]*1.0/dis2[j][i],ans1=i,ans2=j;
                }
        showpath(p2[ans2],p2[ans2][ans1]);
        showpath(p1[ans1],p1[ans1][ans2]);
        printf("%d\n%.3lf\n",ans2,(int)(1000*(ans)+0.5)*1.0/1000);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值