poj 3613 Cow Relays (floyd快速幂)
来源:互联网 发布:程序员在什么系统算 编辑:程序博客网 时间:2024/06/06 12:57
题意:
给出一个带权图,问S到E恰好经过N个点的最短路。
题解:
floyd版的矩阵快速幂。好强大。
#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<queue>#include<map>#include<set>using namespace std;#define B(x) (1<<(x))typedef long long ll;const int oo=0x3f3f3f3f;const ll OO=1LL<<61;const ll MOD=10000;const int maxn=1005;int n;int ans[maxn][maxn],temp[maxn][maxn];int a[maxn][maxn];int id[maxn],mark[maxn];int cmin(int& a,int b){ if(b<a) a=b;}void floyd(int x[][maxn],int y[][maxn]){ for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cmin(temp[id[i]][id[j]],x[id[i]][id[k]]+y[id[k]][id[j]]);}void Copy(int x[][maxn],int y[][maxn]){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { x[id[i]][id[j]]=y[id[i]][id[j]]; y[id[i]][id[j]]=oo; }}void quick_pow(int x[][maxn],int k){ while(k) { if(k&1) { floyd(ans,x); Copy(ans,temp); } floyd(x,x); Copy(x,temp); k>>=1; }}int main(){ int N,T,S,E; int u,v,w,cnt; while(scanf("%d %d %d %d",&N,&T,&S,&E)!=EOF) { n=0; memset(mark,0,sizeof mark); memset(temp,0x3f,sizeof temp); memset(ans,0x3f,sizeof ans); memset(a,0x3f,sizeof a); for(int i=0;i<maxn;i++) ans[i][i]=0; for(int i=1;i<=T;i++) { scanf("%d %d %d",&w,&u,&v); if(!mark[u]) { id[++n]=u; mark[u]=1; } if(!mark[v]) { id[++n]=v; mark[v]=1; } if(a[u][v]>w) a[u][v]=a[v][u]=w; } quick_pow(a,N); printf("%d\n",ans[S][E]); } return 0;}/*2 6 6 411 4 64 4 88 4 96 6 82 6 93 8 9*/
0 0
- POJ 3613 Cow Relays floyd + 快速幂
- POJ 3613 Cow Relays floyd+快速幂
- poj 3613 Cow Relays (floyd快速幂)
- Poj 3613 Cow Relays【Floyd+快速幂】
- POJ 3613 Cow Relays Floyd+快速幂加速
- POJ 3613 Cow Relays (floyd + 矩阵快速幂)
- POJ题目3613 Cow Relays(Floyd+快速幂)
- POJ 3613 Cow Relays(矩阵模板+快速幂+floyd)
- poj 3613 Cow Relays (K步最短路+Floyd+矩阵快速幂)
- poj 3613 Cow Relays (floyd+矩阵快速幂+坐标离散化)
- POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)
- POJ 3613 Cow Relays k步最短路 (floyd + 矩阵快速幂)
- 【Floyd求最短路+快速幂】PKU-3613-Cow Relays
- POJ--3613[Cow Relays] floyd 倍增法
- POJ--3613[Cow Relays] floyd 倍增法
- POJ 3613 Cow Relays Floyd最短路
- POJ 3613 Cow Relays 倍增Floyd
- POJ 3613 Cow Relays floyd+矩阵
- 黑马程序员_流程控制语句
- scala中的call-by-name和call-by-value
- C语言进阶_笔记 第二章 预处理
- C++ Vector的用法
- 程序设计语言的范式
- poj 3613 Cow Relays (floyd快速幂)
- 不让Vim自动生成~备份文件
- 如何将JSON数据通过Get方法获取数据
- 快速搭建节日促销专题
- 数据库权限和角色模型
- MySQL连接字符串总结
- DirectionalViewPager支持横向和纵向的ViewPager
- 继承
- tar命令详解