Application of Part summary matrix algorithm madman (nine) in graph theory 1 + POJ3613 Cow Relays

FIG adjacency matrix is stored, then he would have some of the properties of the matrix, with a demo of FIG. [100] [100]; then demo [M] [N] is the distance M-> N, if after a relaxation operation demo [M] [N] = demo [M] [K] + demo [K] [N], is the demo [M] [N] through a minimum distance of two edges, floyd is demo [M] [ N] = Min (demo [M ] [K] + demo [K] [N], demo [M] [N]), the shortest distance directly, without passing through the third side between two points is possible, then we do not consider without passing through between the two points, the demo [M] [N] is equal to demo [M] [K] + demo [K] [N] K is the minimum value of the enumeration, so there is a class of problems called two after the shortest distance between the edges of N, then the analogy matrix multiplication, matrix multiplication is the sum, we are here for the minimum, you can transform matrix multiplication results, not Floyd, K on the outside and there is no difference, put out like Floyd, which is the standard release of matrix multiplication, because this only once, and all are equivalent to the enumeration of state.

 1     for(int k=1; k<=cnt; k++)
 2     {
 3         for(int i=1; i<=cnt; i++)
 4         {
 5             for(int j=1; j<=cnt; j++)
 6             {
 7                 c[i][j]=Min(a[i][k]+b[k][j],c[i][j]);
 8             }
 9         }
10     }

 

Each class do a matrix multiplication, on behalf of the M, N rear edge through a plurality relaxation, the relaxation times T after get N, M T edges through the shortest distance, since it is based matrix multiplication is not bound to follow law do? Answer yes. For matrix demo^T*demo^W, through the front sides of the minimum values T, is through the rear edges of a minimum value W, want to take a minimum value of T represents the passage of edges + W, because each insertion point is performed once, even if the point is repeated then he will have a ring appeared, but still after T + W edges, so that we can use to solve their minimum power matrix quickly through N edges after, then we will find that figure is inextricably linked with the matrix, certainly there will be other features waiting to find that it can be used to solve problems spanning tree diagram, the next update.

This idea can be solved POJ3613, as if the question is now gone, to a website https://www.acwing.com/problem/content/347/ , the code is attached below.

 

#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#include<cstring>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define cins(s) scanf("%s",s)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0)
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
#define mem(n,x) memset(n,x,sizeof(n))
#define INF  0x3f3f3f3f
#define maxn  100010
#define esp  1e-9
#define mp(a,b) make_pair(a,b)
using namespace std;
const int N=300;
#define clr(a) memset(a,0,sizeof(a))
int a[N][N],temp[N][N],ans[N][N];
int used[10*N];
int p[10*N];
void floyed(int a[][N],int b[][N],int c[][N],int cnt)
{
    for(int k=1; k<=cnt; k++)
    {
        for(int i=1; i<=cnt; i++)
        {
            for(int j=1; j<=cnt; j++)
            {
                c[i][j]=Min(a[i][k]+b[k][j],c[i][j]);
            }
        }
    }
}
void copy(int n,int a[][N],int b[][N])
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            a[i][j]=b[i][j],b[i][j]=INF;
}
int solve(int s,int t,int n,int cnt)
{
 
    while(n)
    {
        if(n&1)
        {
            floyed(ans,a,temp,cnt);
            copy(cnt,ans,temp);
        }
        floyed(a,a,temp,cnt);
        copy(cnt,a,temp);
        n>>=1;
    }
    return ans[s][t];
}
int main()
{
    int n,t,S,E;
    scanf("%d%d%d%d",&n,&t,&S,&E);
    int u,v,w;
    int cnt=0;
    mem(ans,0x3f);
    mem(temp,0x3f);
    mem(a,0x3f);
    for(int i=0; i<t; i++)
    {
        scanf("%d%d%d",&w,&u,&v);
        if(!used[u])
        {
            used[u]=1;
            p[u]=++cnt;
            a[cnt][cnt]=temp[cnt][cnt]=ans[cnt][cnt]=0;
        }
        if(!used[v])
        {
            used[v]=1;
            p[v]=++cnt;
            a[cnt][cnt]=temp[cnt][cnt]=ans[cnt][cnt]=0;
        }
        a[p[u]][p[v]]=a[p[v]][p[u]]=w;
    }
    printf("%d\n",solve(p[S],p[E],n,cnt));
    return 0;
}

 

This side of the question is not continuous, discrete first.

 

Guess you like

Origin www.cnblogs.com/lunatic-talent/p/11366418.html