• poj3613 求经过n条边的最短路 ----矩阵玩出新高度 。


    For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

    Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.

    To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

    Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

    Input

    * Line 1: Four space-separated integers: N, T, S, and E
    * Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

    Output

    * Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

    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

    Source

     

    http://blog.csdn.net/monster__yi/article/details/51069236   感谢题解

    矩阵mx[i][j]表示已经有一条i->j的边,然后在和基础矩阵进行运算,那么mx[j][k],就代表再走一条边从i到j,满足了每次只走一条边的条件

    #include<cstdio>
    #include<cstring>
    #include<map>
    using namespace std;
    map<int,int>mp;
    int tot,k;
    struct Martix{
        int a[205][205];
        Martix operator * (Martix &rhs){
        Martix c;
        memset(c.a,0x3f,sizeof(c.a));
        for(int i=1;i<=tot;++i)
            for(int j=1;j<=tot;++j)
            for(int k=1;k<=tot;++k)
            c.a[i][j]=min(c.a[i][j],a[i][k]+rhs.a[k][j]);
        return c;
        }
    }base,ans;
    int main(){
         int u,v,x,m,st,ed;
         tot=0;
         memset(base.a,0x3f,sizeof(base.a));
         scanf("%d%d%d%d",&k,&m,&st,&ed);
         while(m--){
            scanf("%d%d%d",&x,&u,&v);
            if(!mp[u]) mp[u]=++tot;
            if(!mp[v]) mp[v]=++tot;
            base.a[mp[u]][mp[v]]=base.a[mp[v]][mp[u]]=x;
         }
         ans=base;
         --k;
         while(k){
            if(k&1) ans=ans*base,--k;
            k>>=1;
            base=base*base;
         }
         printf("%d
    ",ans.a[mp[st]][mp[ed]]);
    }

     

  • 相关阅读:
    binary tree解题模板
    done infosys 八股文springboot员工管理系统
    好题 telus国际_从镜子里看到另一台显示器的面试
    done 对于spring boot oa的面试思考
    done 有一点题 infosys 秀出你的ID spring经历问得很细
    Eliassen Group vendor 检测眼球视线的oa 又细又多做不完
    done tek 把你从牛角尖钻出来的node list算法题
    contagious.ly 自豪的纽约客,政府vendor
    Apple screening carlos白人 头晕脑胀的三道简单算法题
    done apple_Infosys_Jugal 要求完整写出Java8 stream
  • 原文地址:https://www.cnblogs.com/mfys/p/7240406.html
Copyright © 2020-2023  润新知