• P4568 [JLOI2011]飞行路线


    追债之旅

    分层图最短路

    • 每次的决策:用/不用免费票
    const int N=10010;
    vector<PII> g[N];
    struct Node
    {
        int dis,u,cnt;
        bool operator>(const Node &W) const
        {
            return dis>W.dis;
        }
    };
    int dist[N][15];
    bool vis[N][15];
    int n,m,k;
    int st,ed;
    
    void dijkstra()
    {
        memset(dist,0x3f,sizeof dist);
        priority_queue<Node,vector<Node>,greater<Node> > heap;
        dist[st][0]=0;
        heap.push({0,st,0});
    
        while(heap.size())
        {
            int t=heap.top().u,c=heap.top().cnt;
            heap.pop();
    
            if(vis[t][c]) continue;
            vis[t][c]=true;
    
            for(int i=0;i<g[t].size();i++)
            {
                int j=g[t][i].fi,w=g[t][i].se;
                if(c+1<=k && dist[j][c+1] > dist[t][c])//用免费票
                {
                    dist[j][c+1]=dist[t][c];
                    heap.push({dist[j][c+1],j,c+1});
                }
                if(dist[j][c] > dist[t][c]+w)//不用免费票
                {
                    dist[j][c]=dist[t][c]+w;
                    heap.push({dist[j][c],j,c});
                }
            }
        }
    }
    
    int main()
    {
        cin>>n>>m>>k;
    
        cin>>st>>ed;
    
        while(m--)
        {
            int a,b,c;
            cin>>a>>b>>c;
            g[a].pb({b,c});
            g[b].pb({a,c});
        }
    
        dijkstra();
    
        int res=INF;
        for(int i=0;i<=k;i++)
            res=min(res,dist[ed][i]);
        cout<<res<<endl;
    
        //system("pause");
    }
    
  • 相关阅读:
    第一个 Python 程序
    Qt之字体文件(TTF)
    Memcached
    Qt之QtSoap(访问WebService)
    Crypto++编译使用
    Memcached
    Windows下编译OpenSSL
    基于Core Text实现的TXT电子书阅读器
    java代码获取jdbc链接properties
    ext树表+ZeroClipboard复制链接功能
  • 原文地址:https://www.cnblogs.com/fxh0707/p/13791702.html
Copyright © 2020-2023  润新知