唔不知道怎么说……大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神

首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf

然后注意重载乘号的部分,注意到这样乘一次就相当于把本来存了经过k条路的最短路的邻接矩阵变成存了经过k+1条路的最短路的邻接矩阵

这样看来乘n次就行了,这里用矩阵快速幂

妙啊

#include<iostream>
#include<cstdio>
using namespace std;
const int N=1005;
const long long inf=1e15;
int n,m,s,t,id[N],tot;
struct qwe
{
long long a[205][205];
qwe operator * (const qwe &b) const
{
qwe c;
for(int i=1;i<=tot;i++)
for(int j=1;j<=tot;j++)
c.a[i][j]=inf;
for(int k=1;k<=tot;k++)
for(int i=1;i<=tot;i++)
for(int j=1;j<=tot;j++)
c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[k][j]);
return c;
}
}a,r;
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
int main()
{
n=read(),m=read(),s=read(),t=read();
for(int i=1;i<=m;i++)
{
int z=read(),x=read(),y=read();
x=(id[x]!=0)?id[x]:(id[x]=++tot);
y=(id[y]!=0)?id[y]:(id[y]=++tot);//cerr<<x<<" "<<y<<endl;
a.a[x][y]=a.a[y][x]=z;
}
for(int i=1;i<=tot;i++)
for(int j=1;j<=tot;j++)
if(!a.a[i][j])
a.a[i][j]=inf;
int x=n-1;
r=a;
while(x)
{
if(x&1)
r=r*a;
a=a*a;
x>>=1;
}
printf("%lld\n",r.a[id[s]][id[t]]);
return 0;
}

最新文章

  1. iTool拷贝app到电脑上
  2. linux初始化配置---主机名、关闭防火墙、关闭selinux
  3. 使用SharedPreferences进行简单的储存
  4. ARPA
  5. busybox filesystem udhcpc 原理
  6. html 表格中文字的背景色
  7. 在Ubuntu中使用JAVA与tomcat搭建web服务器
  8. 写一个Vue loading 插件
  9. 201521123044 《Java程序设计》第7周学习总结
  10. 下篇: php 微商城 基于Thinkphp3.2框架开发
  11. 使用 win10 的正确姿势 (第二版)
  12. ●BZOJ 2820 YY的GCD
  13. CentOS7安装codeblocks(转载)
  14. .NET Garbage Collection配置在.net core的写法
  15. Java全栈程序员之06:IDEA中MAVEN项目依赖及运行
  16. linux的基本操作(shell 脚本的基础知识)
  17. jenkins 结合 jmeter 的报告篇
  18. wampserver安装
  19. Hadoop初期学习和集群搭建
  20. mysql——leetcode问题记录

热门文章

  1. HDU 2475 Box
  2. HDU1241&amp;POJ2386 dfs简单题
  3. HDU 2280 状压DP
  4. [BZOJ1029] [JSOI2007]建筑抢修(贪心 + 优先队列)
  5. Ftp启动与关闭
  6. codevs3164 质因数分解
  7. key通用操作【九】
  8. Less Time, More profit 最大权闭合子图(最大流最小割)
  9. 8VC Venture Cup 2016 - Final Round (Div2) E
  10. linux下常用快捷方式