[Linear Algebra]: relays relay race cows

Question: relays cows Relay Race

Time limit: 1 Sec   Memory Limit: 256 MB

Face questions


Title Description

FJ of N (2 <= N <= 1,000,000) cows selected as the relay to run their daily exercise program. As the relay locations to run the existing natural T (2 <= T <= 100) on the runway in a pasture. On some farms the runway intersection, each runway are connected to two different intersection I1_i and I2_i (1 <= I1_i <= 1,000; 1 <= I2_i <= 1,000). Each intersection is the endpoint of at least two runways. Cows know each runway length length_i (1 <= length_i <= 1,000), and connecting the intersection of each runway number and no two junctions are connected directly by the two different runways. You can think of these constitute a meeting point and a view of the runway. In order to complete a relay race, all N cows before running start all stand on one intersection (probably standing on some meeting point not only a cow). Of course, their stations to ensure that they can in turn pass the baton, and finally holding stick cow to stop at a preset destination. Your task is to write a program that calculates in the case of relay race starting point (S) and end (E) determined cows running the smallest possible total path length. Obviously, this path must go through exactly N runways.

Input Format

Line 1: 4 with a space-separated integers: N, T, S, and E
+ 1 line 2..T: i + 1 of three integers separated by a space: length_i, I1_i, and I2_i He describes the i-th runway.

Output Format

Line 1: Output a positive integer indicating the starting point is S, the end of E, N and right across the path of the minimum length of the runway

Sample input

2 6 6 4

11 4 6

4 4 8

8 4 9

6 6 8

2 6 9

3 8 9

 

Sample Output

10

answer


This question is to expand the matrix multiplication introductory title, it can be said that doubling Floyed . (Also my first line of linear algebra raise questions)

This paper mainly from the perspective of matrix multiplication. (Said to be because doubled Floyed a bit far-fetched, mainly reflected in the rapid doubling the power of the matrix)

We all know that the use of fast power matrix aims to reduce the number of operations in order to reduce the effect of time complexity.

Matrix multiplication code is as follows:

Mal Mal_MULTI(Mal a,Mal b)
{
    Mal ans;
    for(int i=1;i<=cnt;++i)
        for(int j=1;j<=cnt;++j)
            for(int k=1;k<=cnt;++k)
                ans.data[i][j]=a.data[i][k]*b.data[k][j]
    return ans;
}

Very basic matrix multiplication.

However, this question and it is different. This problem is seeking the path length requirements. So obviously not the multiplying, but it should be added.

But the sum is clearly not meet the requirements of the meaning of the title: the smallest path.

So we need to take min.

So function matrix multiplication becomes like this:

Mal Mal_MULTI(Mal a,Mal b)
{
    Mal ans;
    memset(ans.data,0x3f,sizeof(ans.data));
    for(rint i=1;i<=cnt;++i)
        for(rint j=1;j<=cnt;++j)
            for(rint k=1;k<=cnt;++k)
                ans.data[i][j]=min(ans.data[i][j],a.data[i][k]+b.data[k][j]);
    return ans;
}

I think we all saw it, which is obviously floyed very similar. So we called him Floyed algorithm doubled optimization.

However, such a request is not a thing ah QAQ, N large scary, 1e6, so we quickly moved out of the matrix power.

Do not forget to open discrete oh ~

code show as below:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
#define rint register int
using namespace std;
int in_n,in_t,in_s,in_e,ls_dy[1000006],in_x,in_y;
int cnt,in_l;
struct Mal{int data[103][103],line;}map,anss;
Mal Mal_MULTI(Mal a,Mal b)
{
    Mal ans;
    memset(ans.data,0x3f,sizeof(ans.data));
    for(rint i=1;i<=cnt;++i)
        for(rint j=1;j<=cnt;++j)
            for(rint k=1;k<=cnt;++k)
                ans.data[i][j]=min(ans.data[i][j],a.data[i][k]+b.data[k][j]);
    return ans;
}
Mal Mal_QPOW(Mal a,int x)
{
    Mal ans;
    memset(ans.data,0x3f,sizeof(ans.data));
    for(rint i=1;i<=cnt;++i)
        ans.data[i][i]=0;
    while(x)
    {
        if(x&1)ans=Mal_MULTI(ans,a);
        a=Mal_MULTI(a,a);x>>=1;
    }
    return ans;
}
int main()
{
    memset(anss.data,0x3f,sizeof(anss.data));
    memset(map.data,0x3f,sizeof(map.data));
//    cout<<map.data[1][1]<<endl;
    scanf("%d %d %d %d",&in_n,&in_t,&in_s,&in_e);
    for(rint i=1;i<=in_t;++i)
    {
        scanf("%d %d %d",&in_l,&in_x,&in_y);
        if(!ls_dy[in_x])ls_dy[in_x]=++cnt;
        if(!ls_dy[in_y])ls_dy[in_y]=++cnt;
        map.data[ls_dy[in_x]][ls_dy[in_y]]=in_l;
        map.data[ls_dy[in_y]][ls_dy[in_x]]=in_l;
    }
    for(rint i=1;i<=cnt;++i) anss.data[i][i]=0;
    for(rint i=1;i<=cnt;++i,cout<<endl)
        for(rint j=1;j<=cnt;++j)
            cout<<anss.data[i][j]<<' ';
    cout<<endl<<endl;
    for(rint i=1;i<=cnt;++i,cout<<endl)
        for(rint j=1;j<=cnt;++j)
            cout<<map.data[i][j]<<' ';
    cout<<endl<<endl;
    anss=Mal_MULTI(anss,map);
    for(rint i=1;i<=cnt;++i,cout<<endl)
        for(rint j=1;j<=cnt;++j)
            cout<<anss.data[i][j]<<' ';
    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/xingmi-weiyouni/p/11200890.html