(中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。

本文介绍了一种求解特殊图上最短路径的问题,通过使用邻接矩阵和位运算加速算法来寻找从起点到终点所需的最少飞行次数。适用于图中边有权重限制的情况。

  In the country there are exactly n cities numbered with positive integers from 1 to n. In each city there is an airport is located.

Also, there is the only one airline, which makes m flights. Unfortunately, to use them, you need to be a regular customer of this company, namely, you have the opportunity to enjoy flight i from city ai to city bi only if you have already made at least di flights before that.

Please note that flight i flies exactly from city ai to city bi. It can not be used to fly from city bi to city ai. An interesting fact is that there may possibly be recreational flights with a beautiful view of the sky, which begin and end in the same city.

You need to get from city 1 to city n. Unfortunately, you've never traveled by plane before. What minimum number of flights you have to perform in order to get to city n?

Note that the same flight can be used multiple times.

 

  题意大致就是给一个图,然后每条边有一个限制条件d表示至少已经走过d长度才能打开这条路,然后问最少需要多少步才能从1到n。

  本来看到n的数据范围只有150时就应该想到矩阵的,然而忘记了。。。

  邻接矩阵有一个性质就是他的k次幂的第 i 行 j 列就表示从 i 正好走 k 步到 j 的方案数。然后对于这题,对每条路排序,然后一次次对矩阵进行乘法,当次数到限制条件的时候就增加新的可以走的边,然后继续乘。。。

  但是这样的复杂度就有点。。。了,矩阵快速幂的复杂度是n^3 log k的,然后是m次,所以复杂度(n^3mlogk)的,比较大。。。但是对于CF那样速度巨快的机子,写的好的话也是可以过的。。。

  不过这里可以用位运算加速,因为主要考虑的是矩阵的01,而不是方案数,所以用bitset可以加速很多。。。

 

代码如下:

// ━━━━━━神兽出没━━━━━━
//      ┏┓       ┏┓
//     ┏┛┻━━━━━━━┛┻┓
//     ┃           ┃
//     ┃     ━     ┃
//     ████━████   ┃
//     ┃           ┃
//     ┃    ┻      ┃
//     ┃           ┃
//     ┗━┓       ┏━┛
//       ┃       ┃
//       ┃       ┃
//       ┃       ┗━━━┓
//       ┃           ┣┓
//       ┃           ┏┛
//       ┗┓┓┏━━━━━┳┓┏┛
//        ┃┫┫     ┃┫┫
//        ┗┻┛     ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━

// Author        : WhyWhy
// Created Time  : 2015年09月30日 星期三 22时03分41秒
// File Name     : 1_D.cpp

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#include <bitset>

using namespace std;

const int MaxN=160;

int N;

struct Mat
{
    bitset <MaxN> num[MaxN];

    Mat operator * (const Mat & b) const
    {
        Mat ret;
        for(int i=1;i<=N;++i)
            for(int j=1;j<=N;++j)
                if(num[i][j])
                    ret.num[i]|=b.num[j];
        return ret;
    }
};

int M;

struct Edge
{
    int u,v,d;

    bool operator < (const Edge & b) const
    {
        return d<b.d;
    }
};

Edge E[MaxN];
Mat ans,map1;

Mat _pow(Mat base,int n)
{
    Mat ret;
    for(int i=1;i<=N;++i) ret.num[i][i]=1;

    while(n)
    {
        if(n&1) ret=ret*base;
        base=base*base;
        n>>=1;
    }
    return ret;
}

int getans(int R)
{
    int L=1,M;
    Mat temp=ans*_pow(map1,R);

    if(temp.num[1][N]==0)
    {
        ans=temp;
        return 0;
    }

    while(R>L)
    {
        M=(L+R)>>1;
        temp=ans*_pow(map1,M);
        if(temp.num[1][N]) R=M;
        else L=M+1;
    }
    return L;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    scanf("%d %d",&N,&M);
    for(int i=1;i<=M;++i)
        scanf("%d %d %d",&E[i].u,&E[i].v,&E[i].d);
    sort(E+1,E+M+1);
    ++M;
    E[M].d=1000000000+1000;
    E[M].u=E[M].v=0;

    if(E[1].d)
    {
        puts("Impossible");
        return 0;
    }
    
    int t;

    map1.num[N][N]=1;
    for(int i=1;i<=N;++i) ans.num[i][i]=1;
    
    map1.num[E[1].u][E[1].v]=1;
    for(int i=2;i<=M;++i)
        if(E[i].d!=E[i-1].d)
        {
            if(t=getans(E[i].d-E[i-1].d))
            {
                printf("%d\n",E[i-1].d+t);
                return 0;
            }
            map1.num[E[i].u][E[i].v]=1;
        }
        else
            map1.num[E[i].u][E[i].v]=1;

    puts("Impossible");
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whywhy/p/4855221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值