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[][];
    Martix operator * (Martix &rhs){
    Martix c;
    memset(c.a,0x3f,sizeof(c.a));
    for(int i=;i<=tot;++i)
        for(int j=;j<=tot;++j)
        for(int k=;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=;
     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&) ans=ans*base,--k;
        k>>=;
        base=base*base;
     }
     printf("%d\n",ans.a[mp[st]][mp[ed]]);
}

poj3613 求经过n条边的最短路 ----矩阵玩出新高度 。的更多相关文章

  1. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)

    题意:       给你一个无向图,然后给了一个起点s和终点e,然后问从s到e的最短路是多少,中途有一个限制,那就是必须走k条边,路径可以反复走. 思路:       感觉很赞的一个题目,据说证明是什 ...

  2. 经过N条边的最短路

    http://acm.pku.edu.cn/JudgeOnline/problem?id=3613 求经过N条边的最短路 (2 ≤ N ≤ 1,000,000) 倍增floyd,主体是矩阵乘法.考虑一 ...

  3. hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. c编程:求出4&#215;4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和。

    //求出4×4矩阵中最大和最小元素值及其所在行下标和列下标,求出两条主对角线元素之和 #include <stdio.h> int main() { int sum=0; int max, ...

  5. HDU 4396More lumber is required 过至少K条边的最短路

    /* ** 题目要求过最少k条边的最短路 */ #include <iostream> #include <cstdio> #include <cstring> # ...

  6. Cow Relays,过N条边的最短路

    题目链接 题意: 找从a到b的经过N条边的最短路 分析: 有点板子...方法:矩阵存,然后有个类似快速幂的思想,然后再加上离散化就好了. 没啥写的,只能说说矩阵了,我用的方法是先枚举i,j再枚举k,当 ...

  7. 求去掉一条边使最小割变小 HAOI2017 新型城市化

    先求最小割,然后对残量网络跑Tarjan.对于所有满流的边,若其两端点不在同一个SCC中,则这条边是满足条件的. 证明见 来源:HAOI2017 新型城市化

  8. hive实现根据用户分组,按用户记录求上下两条记录的时间差

    在mysql,数据如下:#查询某一用户该日抽奖时间 select draw_time from user_draw_log where user_id = 1 and draw_date='2016- ...

  9. POJ 2253:Frogger 求每一条路径最大值里面的最小值

    Frogger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31490   Accepted: 10150 Descrip ...

随机推荐

  1. Libra教程之:来了,你最爱的Move语言

    文章目录 Move语言 Move的核心概念 Move交易脚本 Move modules Move resources 写一个Move程序 编写交易脚本 编写自己的Modules Move语言 Move ...

  2. 标准库ConfigParser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 来看一个好多软件的常见文档格式如下: 1 2 3 4 5 6 7 8 9 10 11 12 ...

  3. VS2013 配置全局 VC++目录

    原文链接:https://blog.csdn.net/humanking7/article/details/80391914 也许是我VS2013安装的有问题,每次编译程序都要去 项目属性页-> ...

  4. Bootstrap表格组件 Bootstrap Table

    Bootstrap Table是Bootstrap的一个组件 Bootstrap Table Demo:http://issues.wenzhixin.net.cn/bootstrap-table/i ...

  5. P6474 [NOI Online #2 入门组] 荆轲刺秦王

    P6474 [NOI Online #2 入门组] 荆轲刺秦王 bfs+差分+卡常 本来我其实是场内选手,但是因为记错提交时间,晚了半小时才交,交不上了,就自动降级为了场外选手 题面复杂,不简述了 首 ...

  6. Jenkins 构建 Jmeter 项目之源代码管理(SVN)

    1.查看项目创建中是否又 svn 插件,没有的话下载插件 subversion 2.配置 svn 源代码管理,如下图(testcases 目录下包含 build.xml 和脚本文件) 3.查看 Jen ...

  7. 猫狗大战("简单的二维背包")

    题面:https://www.luogu.com.cn/problem/P1489 看上去是一道简单的二维费用背包,但是要特别小心循环顺序. Ⅰ先循环物品,再循环限制条件. Ⅱ每一个限制条件都必须从后 ...

  8. 环境篇:Superset

    环境篇:Superset Superset 是什么? Apache Superset 是一个开源.现代.轻量的BI分析工具,能够对接多种数据源,拥有丰富的图表展示形式.支持自定义仪表盘,用户界面友好, ...

  9. LFU C# 实现

    周六早上  做了下力扣的LRU 题目  后面接着看了LFU 缓存  难度提高了不少 首先 先说下 这2着 的差别把 LRU :最近 最少使用算法(Least  Recently Used).LRU 是 ...

  10. 【Hadoop离线基础总结】网站流量日志数据分析系统

    目录 点击流数据模型 概述 点击流模型 网站流量分析 网站流量模型分析 网站流量来源 网站流量多维度细分 网站内容及导航分析 网站转化及漏斗分析 流量常见分析角度和指标分类 指标概述 指标分类 分析角 ...