[JLOI2011]飞行路线

bzoj2763
↑↑↑
题目
思路:
更改一下d数组的定义,把它变为2维。
d[i][j]表示到达i号点用了j次免费机会的最短路。
至于怎么推下一次状态也很简单,就分2种情况,用免费机会和不要免费机会。
就是这样
↓↓↓

if(d[x][c]+a[k].c<d[y][c])
            {
                d[y][c]=d[x][c]+a[k].c;
                if(v[y][c]==false)
                {
                    v[y][c]=true;
                    list[tail][0]=y;list[tail++][1]=c;
                    if(tail==100001) tail=1;
                }
            }
            if(d[x][c]<d[y][c+1]&&c<kk)
            {
                d[y][c+1]=d[x][c];
                if(!v[y][c+1])
                {
                    v[y][c+1]=true;
                    list[tail][0]=y;list[tail++][1]=c+1;
                    if(tail==100001) tail=1;
                }
            }
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c,next;
}a[210000];int len,last[110000];
void ins(int x,int y,int c)
{
    len++;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
}
int d[110000][11];
int list[110000][11],head,tail;
bool v[110000][11];
int main()
{
    int n,m,kk,st,ed;
    scanf("%d%d%d",&n,&m,&kk);
    scanf("%d%d",&st,&ed);
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        scanf("%d%d%d",&x,&y,&c);
        ins(x,y,c);
        ins(y,x,c);
    }
    memset(d,31,sizeof(d));d[st][0]=0;
    memset(v,false,sizeof(v));
    v[st][0]=true;list[1][0]=st;list[1][1]=0;
    head=1;tail=2;
    while(head!=tail)
    {
        int x=list[head][0];int c=list[head][1];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(d[x][c]+a[k].c<d[y][c])
            {
                d[y][c]=d[x][c]+a[k].c;
                if(v[y][c]==false)
                {
                    v[y][c]=true;
                    list[tail][0]=y;list[tail++][1]=c;
                    if(tail==100001) tail=1;
                }
            }
            if(d[x][c]<d[y][c+1]&&c<kk)
            {
                d[y][c+1]=d[x][c];
                if(!v[y][c+1])
                {
                    v[y][c+1]=true;
                    list[tail][0]=y;list[tail++][1]=c+1;
                    if(tail==100001) tail=1;
                }
            }
        }
        head++; 
        if(head==100001) head=1;
        v[x][c]=false;
    }
    int ans=999999999;
    for(int i=0;i<=kk;i++){ans=min(ans,d[ed][i]);}
    printf("%d\n",ans);
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值