Wycieczki Linear Algebra

B. Tours

Title Description

Given a weighted edges m n points directed graph, each edge of the right side only may be one of 1, 2,.
All possible paths according to the path length of the sort, please output length of the k small path, the path is not necessarily a simple note path that can take repeated the same point.

Input Format

The first line contains three integers n, m, k (1 < = n <= 40,1 <= m <= 1000,1 <= k <= 10 ^ 18).
Next m lines of three integers u, v, c (1 < = u, v <= n, u is not equal to v, 1 <= c <= 3), expressed from a single u to v in side to side length c.
There may be multiple edges.

Output Format

Line contains a positive integer, i.e. a short length of the k-th path, and if not, outputs -1.

Sample

Sample input

6 6 11
1 2 1
2 3 2
3 4 2
4 5 1
5 3 1
4 6 3

Sample Output

4

Data range and tips

Path length of 1 are 1> 2,5> 3,4> 5.
The length of the path 2 are 2> 3,3> 4,4> 5-> 3.
The length of the path 3 are 4> 6,1-> 2-> 3,3> 4-> 5,5> 3-> 4.
4 has a length of path 5-> 3-> 4-> 5.

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);
}
hhh

endl;

 

Guess you like

Origin www.cnblogs.com/hzoi-lsc/p/11209856.html
Recommended