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]]);
}

最新文章

  1. Spark基本工作流程及YARN cluster模式原理(读书笔记)
  2. zepto的touch模块解决click延迟300ms问题以及点透问题的详解
  3. win8.1下golang+sdl2.0环境搭建
  4. 【C#】线程之Task
  5. Newtonsoft.Json的使用
  6. Allegro16.3约束设置
  7. 实用iPhone Cydia插件
  8. 利用文本编辑器输入课堂上练习的Hello.java,并在JDK环境下编译和运行。
  9. The Movie db (TMDB)的API申请
  10. jq复制
  11. JavaEE 之 文件上传
  12. 原生AJAX请求教程
  13. 1、Python中的正则表达式(0601)
  14. http与https的区别以及https如何保证数据传输安全
  15. [原创]Jmeter工具学习思维导图
  16. 2018.10.13 bzoj4008: [HNOI2015]亚瑟王(概率dp)
  17. MongoDB(五):MongoDB操作文档
  18. EF的使用(DbContext对象的共用问题)
  19. UIView独占响应事件
  20. Luogu P3579 [POI2014]PAN-Solar Panels

热门文章

  1. 原生JS中获取位置的方案总结
  2. 深入认识CSS的块级元素
  3. Eclipse Mac OS 安装中文简体语言包
  4. Express 文档(常见问题)
  5. python selenium(常用关键字)
  6. java的Timer定时器任务
  7. SpringBoot上传文件大小限制
  8. MySQL命令1
  9. 使用Codemirror打造Markdown编辑器
  10. 补一下昨天的博客 J - Sabotage 网络流