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\n",ans);
}

int main()
{
    init();
    dijkstra();
    return 0;
}
View Code

 

posted @ 2017-09-29 16:02  啊宸  阅读(159)  评论(0编辑  收藏  举报