• BZOJ 2763: [JLOI2011]飞行路线 最短路


    2763: [JLOI2011]飞行路线

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=2763

    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.

    题意

    题解:

    最短路,在转移dis的时候多开一维k就好了

    dis[i][j]->dis[t][j]+e[i][t]

    dis[i][j]->dis[t][j+1] if j<k

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 100010
    #define eps 1e-9
    int Num;
    //const int inf=0x7fffffff;   //§&szlig;§é§à§é¨f§3
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    struct node
    {
        int x,y;
    };
    int dis[maxn][12];
    vector<node> E[maxn];
    int inq[maxn][12];
    int main()
    {
        int n=read(),m=read(),k=read(),s=read(),t=read();
    
        for(int i=0;i<m;i++)
        {
            int x=read(),y=read(),z=read();
            E[x].push_back((node){y,z});
            E[y].push_back((node){x,z});
        }
    
        for(int i=0;i<=n;i++)
            for(int j=0;j<=k;j++)
                dis[i][j]=inf;
    
        queue<node> Q;
        Q.push((node){s,0});
        dis[s][0]=0;
        int ans = inf;
        while(!Q.empty())
        {
            node now = Q.front();
            Q.pop();
            inq[now.x][now.y]=0;
            for(int i=0;i<E[now.x].size();i++)
            {
                if(dis[now.x][now.y]+E[now.x][i].y<dis[E[now.x][i].x][now.y])
                {
                    dis[E[now.x][i].x][now.y]=dis[now.x][now.y]+E[now.x][i].y;
                    if(!inq[E[now.x][i].x][now.y])
                    {
                        inq[E[now.x][i].x][now.y]=1;
                        Q.push((node){E[now.x][i].x,now.y});
                    }
                }
                if(dis[now.x][now.y]<dis[E[now.x][i].x][now.y+1]&&now.y<k)
                {
                    dis[E[now.x][i].x][now.y+1]=dis[now.x][now.y];
                    if(!inq[E[now.x][i].x][now.y+1])
                    {
                        inq[E[now.x][i].x][now.y+1] = 1;
                        Q.push((node){E[now.x][i].x,now.y+1});
                    }
                }
            }
        }
        for(int i=0;i<=k;i++)
            ans = min(ans,dis[t][i]);
        cout<<ans<<endl;
    }
  • 相关阅读:
    VS2017gets的使用
    UITableViewCell自定义高度
    登录注册页面的
    监听键盘- KeyBoard
    UITableViewDelete 删除
    UITabView 添加
    navigationbar背景图 设置左右按钮
    Navigation1 PUSH & POP
    母传键老师课堂笔记 -----ViewController的生命周期
    可折叠tableView
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4789383.html
Copyright © 2020-2023  润新知