• BZOJ 2763 [JLOI2011]飞行路线


    题面

    堆优的dijkstra裸题。

    努力把自己的代码写得更好看Ing

    //Twenty
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<ctime>
    const int maxn=10000+299;
    const int maxm=50000+299;
    typedef long long LL;
    using namespace std;
    
    struct node {
        int id,ct,k;
        node(){}
        node(int id,int ct,int k):id(id),ct(ct),k(k){}
        friend bool operator <(const node &A,const node &B) {
            return A.ct>B.ct;
        }
    }now,tp;
    
    inline int read() {
        char ch=getchar(); int ret=0;
        while(ch<'0'||ch>'9') ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) ret=ret*10+ch-'0';
        return ret;
    }
    
    int fir[maxn],nxt[maxm*2],to[maxm*2],val[maxm*2],ecnt;
    void add(int u,int v,int w) {
        nxt[++ecnt]=fir[u]; fir[u]=ecnt; to[ecnt]=v; val[ecnt]=w;
        nxt[++ecnt]=fir[v]; fir[v]=ecnt; to[ecnt]=u; val[ecnt]=w;
    }
    
    int n,m,kk,s,t;
    void init() {
        n=read(); m=read(); kk=read();
        s=read(); t=read();
        for(int i=1;i<=m;i++) {
            int x,y,z;
            x=read(); y=read(); z=read();
            add(x,y,z);
        }
    }
    
    int ans=1e9,vis[maxn][11],dis[maxn][11];
    priority_queue<node>que;
    void dijkstra() {
        memset(dis,127,sizeof(dis));
        que.push(node(s,0,0));
        while(!que.empty()) {
            now=que.top();
            que.pop();
            int x=now.id,d=now.ct,k=now.k;
            vis[x][k]=1;
            for(int i=fir[x];i;i=nxt[i]) {
                int v=to[i];
                if(k<kk&&!vis[v][k+1]&&dis[v][k+1]>d) {
                    dis[v][k+1]=d;
                    que.push(node(v,d,k+1)); 
                }
                if(!vis[v][k]&&dis[v][k]>d+val[i]) {
                    dis[v][k]=d+val[i];
                    que.push(node(v,d+val[i],k)); 
                }
            }
        }
        for(int i=0;i<=kk;i++) ans=min(ans,dis[t][i]);
        printf("%d
    ",ans);
    }
    
    int main()
    {
        init();
        dijkstra();
        return 0;
    }
    View Code
  • 相关阅读:
    leetcode 279. Perfect Squares
    leetcode 546. Remove Boxes
    leetcode 312. Burst Balloons
    leetcode 160. Intersection of Two Linked Lists
    leetcode 55. Jump Game
    剑指offer 滑动窗口的最大值
    剑指offer 剪绳子
    剑指offer 字符流中第一个不重复的字符
    leetcode 673. Number of Longest Increasing Subsequence
    leetcode 75. Sort Colors (荷兰三色旗问题)
  • 原文地址:https://www.cnblogs.com/Achenchen/p/7611460.html
Copyright © 2020-2023  润新知