B. Tours
Title Description
Input Format
Output Format
Sample
Data range and tips
This question is relatively long span of time, mainly because of this question thief difficult to tune, the slightest mistake will WA, and solving the problem of test points more than thieves, there will be more than 2 points to the case, so I really said signature, a a cup of tea bag paper, into a piece of code X
In fact, this question seems fairly, but also the amount of thinking, the main point is to take the side of the split matrix, then the construction side, need to pay attention to the point, that is, the entire matrix will be expanded three times; in fact, the whole problem is two points (I hit half , running really slow, so for a way) doubled, and doubled on demand lca fact is the same, is replaced by the matrix, I do not know how other gods Ben is playing, anyway, I was using the structure, but pay attention to details, the entire card for a night, it is because the matrix mass participation did not increase to take address, plus to a, and God knows why Ben would leave a message
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> using namespace std; #define LL long long #define re register #define F(i,a,b) for(LL i=a;i<=b;i++) LL n,m,u,v,d,t; long long s,k; bool flag; inline LL read() { re LL ss=0;char bb=getchar(); while(bb<48||bb>57)bb=getchar(); while(bb>=48&&bb<=57)ss=(ss<<1)+(ss<<3)+(bb^48),bb=getchar(); return ss; } struct Martix { LL x[250][250]; void init() { memset(x,0,sizeof(x)); } }mul[125],tmp; Martix bg,base,now; void made(Martix &a,Martix &b,Martix &c) { flag=1; tmp.init(); F(i,1,3*n+1) F(l,1,3*n+1) { if(!a.x[i][l])continue; //debug(i);debug(l); F(j,1,3*n+1) tmp.x[i][j]=tmp.x[i][j]+a.x[i][l]*b.x[l][j]; if(i==1&&tmp.x[i][3*n+1]>=k)flag=0; } c=tmp; } int main() { //freopen("cnm.txt","r",stdin); n=read(),m=read(),k=read(); F(i,1,n) { bg.x[1][i]=1; base.x[i][i+n]=1; base.x[i+n][i+2*n]=1; } while(m--) { u=read(),v=read(),d=read(); base.x[u+(d-1)*n][v]++; base.x[u+(d-1)*n][3*n+1]++; } base.x[3*n+1][3*n+1]=1; mul[0]=base; for(;t<=63;t++) { if(t) made(mul[t-1],mul[t-1],mul[t]); made(bg,mul[t],now); if(!flag||now.x[1][3*n+1]>=k){break;} } t--; if(t==63&&now.x[1][3*n+1]<k) { puts("-1"); return 0; } for(LL i=t;i>=0;i--) { made(bg,mul[i],now); if(flag&&now.x[1][3*n+1]<k) { s+=(1ll<<i); bg=now; } } printf("%lld\n",s+1); }
endl;