zoukankan      html  css  js  c++  java
  • 飞行路线 题解

    Alice 和 Bob 现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n
    个城市设有业务,设这些城市分别标记为 0 到 n−1,一共有 m 种航线,每种航线连接两个城市,并且航线有一定的价格。
    Alice 和 Bob 现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空
    公司对他们这次旅行也推出优惠,他们可以免费在最多 k 种航线上搭
    乘飞机。那么 Alice 和 Bob 这次出行最少花费多少?

    思路:对于每次免费机会,可以看做是直接进入下一次,所以可以分成k层,跑分层最短路

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int N=2e6+7;
    struct edge{
    	int v,w,nxt;
    }e[N<<1];
    int n,m,k,s,t,cnt;
    int head[N],dis[N],vis[N]; 
    void add_edge(int u,int v,int w){
        e[++cnt]=(edge){v,w,head[u]};
        head[u]=cnt;
    }
    void dij(int x){
    	memset(dis,0x3f,sizeof(dis));
        priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
        dis[x]=0;
        q.push(make_pair(dis[x],x));
        while(q.size()){
            int u=q.top().second;
            q.pop();
            if(vis[u]) continue;
            vis[u]=1;
            for(int i=head[u];i;i=e[i].nxt){
                int to=e[i].v;
                if(dis[u]+e[i].w<dis[to]){
                	dis[to]=dis[u]+e[i].w;
                    q.push(make_pair(dis[to],to));
                }
            }
        }
    }
    int main(){
    	scanf("%d%d%d",&n,&m,&k);
        scanf("%d%d",&s,&t);
    	for(int i=1;i<=m;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,z);
            add_edge(y,x,z);
            for(int j=1;j<=k;j++){
                add_edge(x+n*(j-1),y+n*j,0);
                add_edge(y+n*(j-1),x+n*j,0);
                add_edge(x+n*j,y+n*j,z);
                add_edge(y+n*j,x+n*j,z);
            }
        }
        for(int i=1;i<=k;i++){
        	add_edge(t+(i-1)*n,t+i*n,0);
        }
        dij(s);
        cout<<dis[n*k+t];
    }
    
  • 相关阅读:
    apache commons-io相关介绍-IOUtils类
    apache commons-io相关介绍-DirectoryWalker类
    apache commons-io相关介绍-FileUtils类
    apache commons-io相关介绍-monitor包
    android笔记--AsyncTask例子
    Java Swing中的SwingWorker
    Predicting Boston Housing Prices
    matplotlib.pyplot 中很好看的一种style
    机器学习算法比较
    udacity 机器学习课程 project2
  • 原文地址:https://www.cnblogs.com/Aswert/p/13787636.html
Copyright © 2011-2022 走看看