zoukankan      html  css  js  c++  java
  • BZOJ2763: [JLOI2011]飞行路线

    BZOJ2763: [JLOI2011]飞行路线

    Description

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

    Input

    数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
    第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
    接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)

    Output

     
    只有一行,包含一个整数,为最少花费。

    Sample Input

    5 6 1
    0 4
    0 1 5
    1 2 5
    2 3 5
    3 4 5
    2 3 3
    0 2 100

    Sample Output

    8

    HINT

    对于30%的数据,2<=n<=50,1<=m<=300,k=0;

    对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;

    对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10.


    题解Here!
    额,裸的分层图最短路。。。
    每层正常建图,相邻两层间建$0$的边。
    然后就可以跑$SPFA$了。
    为了防止$TLE$,我用了$SLF$优化。
    当然堆优化$Dijkstra$也是能跑的。
    附代码:
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<deque>
    #define MAXN 200010
    #define MAX 999999999
    using namespace std;
    deque<int> q;
    int n,m,k,s,t,c=1;
    int head[MAXN],path[MAXN];
    bool vis[MAXN];
    struct Graph{
    	int next,to,w;
    }a[MAXN*20];
    inline int read(){
    	int date=0,w=1;char c=0;
    	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
    	return date*w;
    }
    inline int relax(int u,int v,int w){
    	if(path[v]>path[u]+w){
    		path[v]=path[u]+w;
    		return 1;
    	}
    	return 0;
    }
    inline void add(int u,int v,int w){
    	a[c].to=v;a[c].w=w;a[c].next=head[u];head[u]=c++;
    }
    void spfa(){
    	int u,v;
    	for(int i=1;i<=n*(k+1)+1;i++){path[i]=MAX;vis[i]=false;}
    	path[s]=0;
        vis[s]=true;
        q.push_back(s);
        while(!q.empty()){
            u=q.front();
            q.pop_front();
            vis[u]=false;
            for(int i=head[u];i;i=a[i].next){
                v=a[i].to;
                if(relax(u,v,a[i].w)&&!vis[v]){
                    vis[v]=true;
                    if(!q.empty()){
                        if(path[v]>path[q.front()])q.push_back(v);
                        else q.push_front(v);
                    }
                    else q.push_back(v);
                }
            }
        }
    }
    void work(){
    	int ans=MAX+1;
    	spfa();
    	for(int i=0;i<=k;i++)ans=min(ans,path[t+i*n]);
    	printf("%d
    ",ans);
    }
    void init(){
    	int u,v,w;
    	n=read();m=read();k=read();
    	s=read()+1;t=read()+1;
    	for(int i=1;i<=m;i++){
    		u=read()+1;v=read()+1;w=read();
    		for(int j=0;j<=k;j++){
    			add(u+j*n,v+j*n,w);
    			add(v+j*n,u+j*n,w);
    		}
    		for(int j=0;j<k;j++){
    			add(u+j*n,v+(j+1)*n,0);
    			add(v+j*n,u+(j+1)*n,0);
    		}
    	}
    }
    int main(){
    	init();
    	work();
        return 0;
    }
    
  • 相关阅读:
    c#自动更新+安装程序的制作
    VS2013项目受源代码管理向源代码管理注册此项目时出错
    WinDbg配置和使用基础
    InstallShield Limited Edition for Visual Studio 2013 图文教程(教你如何打包.NET程序)
    PowerDesigner 如何生成数据库更新脚本
    用户故事(User Story)
    Troubleshooting Record and Playback issues in Coded UI Test
    Coded UI
    compare two oracle database schemas
    How to: Use Schema Compare to Compare Different Database Definitions
  • 原文地址:https://www.cnblogs.com/Yangrui-Blog/p/9513629.html
Copyright © 2011-2022 走看看