zoukankan      html  css  js  c++  java
  • 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]]);
    }

     

  • 相关阅读:
    Windows Phone 四、控件模版
    Windows Phone 三、样式和资源
    Windows Phone 一、XAML基础语法
    第三次月考
    第三次月考
    第七章 LED将为我们闪烁:控制发光二极管
    第六章 第一个linux个程序:统计单词个数
    第五章 搭建S36510开发板的测试环境
    Android深度探索读后感 第四章
    Android深度探索读后感 第三章
  • 原文地址:https://www.cnblogs.com/mfys/p/7240406.html
Copyright © 2011-2022 走看看