CF576D Flights for Regular Customers
题目链接
首先很容易想到一个dp
好暴力的cf题
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
using namespace std;
int n,m;
struct M{
bitset<160>a[160];
M(){
for(int i=1;i<=n;i++)
a[i].reset();
}
};
M operator *(const M& a,const M& b)
{
M ans;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a.a[i][j])ans.a[i]|=b.a[j];
return ans;
}
M I()
{
M ans;
for(int i=1;i<=n;i++)
ans.a[i][i]=1;
return ans;
}
M fpow(M t,int b)
{
M ans=I();
while(b)
{
if(b&1)ans=ans*t;
t=t*t;
b>>=1;
}
return ans;
}
int u[160],v[160],d[160];
int p[160];
bool cmp(int x,int y)
{
return d[x]<d[y];
}
M pw[10];
void work(M now,M cur,int pos)
{
pw[0]=cur;
for(int i=1;i<=8;i++)
pw[i]=pw[i-1]*pw[i-1];
if(!((now*pw[8]).a[1][n]))printf("Impossible\n");
else{
int res=d[p[pos]];
for(int i=8;i>=0;i--)
if(!((now*pw[i]).a[1][n]))res+=(1<<i),now=now*pw[i];
printf("%d\n",res+1);
}
exit(0);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u[i],&v[i],&d[i]);
p[i]=i;
}
sort(p+1,p+m+1,cmp);
M now=I();
M A;A.a[n][n]=1;
// cout<<now.a[1]<<"\n";
for(int i=1;i<=m;i++)
{
M tmp=fpow(A,d[p[i]]-d[p[i-1]]);
if((now*tmp).a[1][n])work(now,A,i-1);
now=now*tmp;
A.a[u[p[i]]][v[p[i]]]=1;
// cout<<now.a[1]<<" "<<d[p[i]]<<"\n";
}
work(now,A,m);
return 0;
}