我是靠谱客的博主 傻傻小霸王,最近开发中收集的这篇文章主要介绍Bus System (dij),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 

Because of the huge population of China, public transportation is very important. Bus is an important transportation method in traditional public transportation system. And it’s still playing an important role even now.
The bus system of City X is quite strange. Unlike other city’s system, the cost of ticket is calculated based on the distance between the two stations. Here is a list which describes the relationship between the distance and the cost.

 



Your neighbor is a person who is a really miser. He asked you to help him to calculate the minimum cost between the two stations he listed. Can you solve this problem for him?
To simplify this problem, you can assume that all the stations are located on a straight line. We use x-coordinates to describe the stations’ positions.

Input

The input consists of several test cases. There is a single number above all, the number of cases. There are no more than 20 cases.
Each case contains eight integers on the first line, which are L1, L2, L3, L4, C1, C2, C3, C4, each number is non-negative and not larger than 1,000,000,000. You can also assume that L1<=L2<=L3<=L4.
Two integers, n and m, are given next, representing the number of the stations and questions. Each of the next n lines contains one integer, representing the x-coordinate of the ith station. Each of the next m lines contains two integers, representing the start point and the destination.
In all of the questions, the start point will be different from the destination.
For each case,2<=N<=100,0<=M<=500, each x-coordinate is between -1,000,000,000 and 1,000,000,000, and no two x-coordinates will have the same value.

Output

For each question, if the two stations are attainable, print the minimum cost between them. Otherwise, print “Station X and station Y are not attainable.” Use the format in the sample.

Sample Input

2
1 2 3 4 1 3 5 7
4 2
1
2
3
4
1 4
4 1
1 2 3 4 1 3 5 7
4 1
1
2
3
10
1 4

Sample Output

Case 1:
The minimum cost between station 1 and station 4 is 3.
The minimum cost between station 4 and station 1 is 3.
Case 2:
Station 1 and station 4 are not attainable.
//#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const ll inf=1e18;
const int N=500+5;

    int n,m,t;
    int i,j,k;
    ll dis[N];
    ll G[N][N];
    bool vis[N];
    vector<int>ans;

void dijkstra(int s)
{
    ms(vis,0);
    for(i=1;i<=n;i++) dis[i]=G[s][i];
    dis[s]=0,vis[s]=1;
    for(i=1;i<=n-1;i++){
        ll minn=inf,pos=-1;
        for(j=1;j<=n;j++){
            if(!vis[j] && dis[j]<minn){
                minn=dis[j];
                pos=j;
            }
        }
        if(minn==inf) break;
        vis[pos]=1;
        for(j=1;j<=n;j++){
            if( !vis[j] && dis[j]>dis[pos]+G[pos][j]){
                dis[j]=dis[pos]+G[pos][j];
            }
        }
    }
    return ;
}

int main()
{
    /*
    公交收费与距离有关,如上图,
    现在给你一些公交车站的位置坐标,
    根据公交车站的位置和题中的收费标准来计算两个车站的最小费用。
    若无法通行,特列输出*/
    cin.tie(0);istream::sync_with_stdio(false);
    int num=0;
    rush()
    {
        ll l[4],c[4],x[N];
        for(i=0;i<4;i++) cin>>l[i];
        for(i=0;i<4;i++) cin>>c[i];
        cin>>n>>m;
        for(i=1;i<=n;i++) cin>>x[i];

        for(i=1;i<=n;i++){
            for(j=i+1;j<=n;j++){
                ll _=abs(x[i]-x[j]);
                if(_>0 && _<=l[0])
                    G[i][j]=G[j][i]=c[0];
                else if(_>l[0] && _<=l[1])
                    G[i][j]=G[j][i]=c[1];
                else if(_>l[1] && _<=l[2])
                    G[i][j]=G[j][i]=c[2];
                else if(_>l[2] && _<=l[3])
                    G[i][j]=G[j][i]=c[3];
                else
                    G[i][j]=G[j][i]=inf;
            }
            G[i][i]=0;
        }

        printf("Case %d:n",++num);
        for(k=1;k<=m;k++){
            ll s,e;
            cin>>s>>e;
            dijkstra(s);
            if(dis[e]==inf ){
                printf("Station %lld and station %lld are not attainable.n",s,e);
            }
            else{
                printf("The minimum cost between station %lld and station %lld is %lld.n",s,e,dis[e]);
            }
        }
    }
    return 0;
}

 

最后

以上就是傻傻小霸王为你收集整理的Bus System (dij)的全部内容,希望文章能够帮你解决Bus System (dij)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部