bzoj2763: [JLOI2011]飞行路线 最短路

题意:求最多可以有k条路免费的最短路
题解:用dis[x][k]表示从s开始用了k次免费机会到x的最短路,然后dij跑的时候优先队列里多维护一个k就好了

/**************************************************************
    Problem: 2763
    User: walfy
    Language: C++
    Result: Accepted
    Time:272 ms
    Memory:4528 kb
****************************************************************/
 
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 20101009
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
 
using namespace std;
 
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=10000+10,maxn=200000+10,inf=0x3f3f3f3f;
 
struct edge{
    int to,Next,c;
}e[maxn];
int cnt,head[N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof head);
}
void add(int u,int v,int c)
{
    e[cnt].to=v;
    e[cnt].c=c;
    e[cnt].Next=head[u];
    head[u]=cnt++;
}
struct node{
    int d,id,num;
    bool operator <(const node&rhs)const
    {
        return d>rhs.d||d==rhs.d&&id<rhs.id;
    }
};
int dis[N][12];
priority_queue<node>q;
int n,m,k,s,t;
void dij()
{
    memset(dis,inf,sizeof dis);
    dis[s][0]=0;
    q.push({dis[s][0],s,0});
    while(!q.empty())
    {
        node u=q.top();q.pop();
        if(u.d>dis[u.id][u.num])continue;
        for(int i=head[u.id];~i;i=e[i].Next)
        {
            int x=e[i].to;
            if(dis[x][u.num]>dis[u.id][u.num]+e[i].c)
            {
                dis[x][u.num]=dis[u.id][u.num]+e[i].c;
                q.push({dis[x][u.num],x,u.num});
            }
            if(u.num<k&&dis[x][u.num+1]>dis[u.id][u.num])
            {
                dis[x][u.num+1]=dis[u.id][u.num];
                q.push({dis[x][u.num+1],x,u.num+1});
            }
        }
    }
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    scanf("%d%d",&s,&t);s++,t++;
    init();
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);a++,b++;
        add(a,b,c);add(b,a,c);
    }
    dij();
    int ans=inf;
    for(int i=0;i<=k;i++)ans=min(ans,dis[t][i]);
    printf("%d\n",ans);
    return 0;
}
/********************
 
********************/
posted @ 2018-08-11 20:07  walfy  阅读(175)  评论(0编辑  收藏  举报