Medium CF 576D Flights for Regular Customers (#319 Div1 D), Matrix fast power.

Source: Internet
Author: User
Tags bitset

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

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

Please note this flight I flies exactly from city ai to city b I.IT can is used to fly from city bi to City aI. An interesting fact was that there may possibly was recreational flights with a beautiful view of the sky, which begin and End in the same city.

You need to get from the City 1 to the city n. Unfortunately, you ' ve never traveled by plane before. What minimum Number of flights are you having to perform in order to get to City n?

Note that the same flight can is used multiple times.

Test instructions is roughly given to a diagram, and then each edge has a constraint d indicating that at least the D length has passed to open the road, and then ask how many steps it takes to get from 1 to N.

We should have thought of the matrix when I saw that the data range of n was only 150, but forgot ...

One of the properties of the adjacency matrix is that the first row J of his K-power is the number of schemes from I just walk K-steps to J. Then for this problem, the order of each road, and then multiply the matrix again and again, when the number of times to limit the time to add new can go to the edge, and then continue to multiply ...

But this kind of complexity is a bit ... , the complexity of the matrix fast Power is n^3 log k, then the M times, so the complexity (N^3MLOGK), the larger ... But for CF as fast as the machine, write good words can also be too ...

However, this can be accelerated with bit operations, because the main consideration is the matrix of 01, not the number of scenarios, so with Bitset can speed up a lot of ...

The code is as follows:

//The ━━━━━━ of gods and Beasts ━━━━━━//┏┓┏┓//┏┛┻━━━━━━━┛┻┓//┃┃//┃━┃//████━████┃//┃┃//┃┻┃//┃┃//┗━┓┏━┛//┃┃//┃┃//┃┗━━━┓//┃┣┓//┃┏┛//┗┓┓┏━━━━━┳┓┏┛//┃┫┫┃┫┫//┗┻┛┗┻┛////━━━━━━ Feel the ━━━━━━ of Meng Meng//author:whywhy//Created time:2015 September 30 Wednesday 22:03 41 seconds//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 namespacestd;Const intmaxn= the;intN;structmat{Bitset<MaxN>NUM[MAXN]; Matoperator* (ConstMat & B)Const{MAT ret;  for(intI=1; i<=n;++i) for(intj=1; j<=n;++j)if(Num[i][j]) ret.num[i]|=B.num[j]; returnret; }};intM;structedge{intu,v,d; BOOL operator< (ConstEdge & B)Const    {        returnd<B.D; }}; Edge E[MAXN]; Mat Ans,map1; Mat _pow (MatBase,intN)    {MAT ret;  for(intI=1; i<=n;++i) ret.num[i][i]=1;  while(n) {if(n&1) ret=ret*Base; Base=Base*Base; N>>=1; }    returnret;}intGetans (intR) {    intL=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; Elsel=m+1; }    returnL;}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);scanf ("%d%d",&n,&M);  for(intI=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+ +; E[M].U=e[m].v=0; if(e[1].d) {puts ("Impossible"); return 0; }        intT; Map1.num[n][n]=1;  for(intI=1; i<=n;++i) ans.num[i][i]=1; map1.num[e[1].u][e[1].v]=1;  for(intI=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; }        ElseMAP1.NUM[E[I].U][E[I].V]=1; Puts ("Impossible"); return 0;}
View Code

Medium CF 576D Flights for Regular Customers (#319 Div1 D), Matrix fast power.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.