Description

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

分析

  • 题意:给定一个T(2 <= T <= 100)条边的无向图,求S到E恰好经过N(2 <= N <= 1000000)条边的最短路。
  • 分析:大致思路就是floyd+矩阵乘法。我们令C[S][E]表示S点到E点正好经过N条边的路径数。
  • 接下来用Floyd每次使用一个中间点k去更新S,E之间的距离,那么更新成功表示S,E之间恰有一个点k时的最短路。我们做n次这样的操作就能够得出结果了。
  • 我们用c[S][E]=max(c[S][E],a[S][k]+a[k][E])来进行路径长度更新。第二次将c[S][E]拷贝回到a[S][E]当中,并将c[S][E]重新置为inf,再做一次,则是在原来的基础上在S,E之间再用一个点k来松弛,这时候S,E之间实际上已经是两个点了,之后重复这么做就好了.

代码

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf = 0x7f7f7f7f;
const int maxn = ;
int K,M,S,T;
int v[maxn],cnt,map[maxn][maxn],used[maxn];
int ans[maxn][maxn],dis[maxn][maxn],tmp[maxn][maxn];
void floyd(int c[][maxn],int a[][maxn],int b[maxn][maxn]){
int i,j,k;
for(k=;k<cnt;k++){
for(i=;i<cnt;i++){
for(j=;j<cnt;j++){
if(c[v[i]][v[j]]>a[v[i]][v[k]]+b[v[k]][v[j]])
c[v[i]][v[j]]=a[v[i]][v[k]]+b[v[k]][v[j]];
}
}
}
}
void copy(int a[][maxn],int b[][maxn]){
int i,j;
for(i=;i<cnt;i++){
for(j=;j<cnt;j++){
a[v[i]][v[j]]=b[v[i]][v[j]];
b[v[i]][v[j]]=inf;
}
}
}
void solve(int k){
while(k){
if(k%){
floyd(dis,ans,map);
copy(ans,dis);
}
floyd(tmp,map,map);
copy(map,tmp);
k=k/;
}
}
int main(){
int i,j;
int x,y,val;
while(scanf("%d%d%d%d",&K,&M,&S,&T)==){
for(i=;i<=;i++){
for(j=;j<=;j++){
map[i][j]=inf;
ans[i][j]=inf;
tmp[i][j]=inf;
dis[i][j]=inf;
}
ans[i][i]=;
}
memset(used,,sizeof(used));
cnt=;
for(i=;i<M;i++){
scanf("%d%d%d",&val,&x,&y);
if(map[x][y]>val){
map[x][y]=val;
map[y][x]=map[x][y];
}
if(!used[x]){
used[x]=;
v[cnt++]=x;
}
if(!used[y]){
used[y]=;
v[cnt++]=y;
}
}
solve(K);
printf("%d\n",ans[S][T]);
}
return ;
}

【floyd+矩阵乘法】POJ 3613 Cow Relays的更多相关文章

  1. Poj 3613 Cow Relays (图论)

    Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...

  2. poj 3613 Cow Relays【矩阵快速幂+Floyd】

    !:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...

  3. POJ 3613 Cow Relays(floyd+快速幂)

    http://poj.org/problem?id=3613 题意: 求经过k条路径的最短路径. 思路: 如果看过<矩阵乘法在信息学的应用>这篇论文就会知道 现在我们在邻接矩阵中保存距离, ...

  4. poj 3613 Cow Relays(矩阵的图论意义)

    题解 用一个矩阵来表示一个图的边的存在性,即矩阵C[i,j]=1表示有一条从i到j的有向边C[i,j]=0表示没有从i到j的边.这个矩阵的k次方后C[i,j]就表示有多少条从i到j恰好经过k条边的路径 ...

  5. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

  6. 【Floyd矩阵乘法】BZOJ1706- [usaco2007 Nov]relays 奶牛接力跑

    [题目大意] 给出一张无向图,求出恰巧经过n条边的最短路. [思路] 首先题目中只有100条边,却给出了10000个点(实际上最多只能有200个),离散化一下. 后面就是Floyd的新姿势,以前看过的 ...

  7. POJ 3613 Cow Relays (floyd + 矩阵高速幂)

    题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把 ...

  8. POJ 3613 [ Cow Relays ] DP,矩阵乘法

    解题思路 首先考虑最暴力的做法.对于每一步,我们都可以枚举每一条边,然后更新每两点之间经过\(k\)条边的最短路径.但是这样复杂度无法接受,我们考虑优化. 由于点数较少(其实最多只有\(200\)个点 ...

  9. POJ 3613 Cow Relays 恰好n步的最短路径

    http://poj.org/problem?id=3613 题目大意: 有T条路.从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min ...

随机推荐

  1. Java实现 LeetCode 743 网络延迟时间(Dijkstra经典例题)

    743. 网络延迟时间 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源节点,v 是目标节点 ...

  2. Java实现 LeetCode 564 寻找最近的回文数(今天要GG在这道题了 头晕+题难(((φ(◎ロ◎;)φ))))

    564. 寻找最近的回文数 给定一个整数 n ,你需要找到与它最近的回文数(不包括自身). "最近的"定义为两个整数差的绝对值最小. 示例 1: 输入: "123&quo ...

  3. Java实现 LeetCode 539 最小时间差(单位转换)

    539. 最小时间差 给定一个 24 小时制(小时:分钟)的时间列表,找出列表中任意两个时间的最小时间差并已分钟数表示. 示例 1: 输入: ["23:59","00:0 ...

  4. Java实现 蓝桥杯VIP 算法提高 解二元一次方程组

    算法提高 解二元一次方程组 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个二元一次方程组,形如: a * x + b * y = c; d * x + e * y = f; x,y代 ...

  5. Java实现第八届蓝桥杯青蛙跳杯子

    青蛙跳杯子 题目描述 X星球的流行宠物是青蛙,一般有两种颜色:白色和黑色. X星球的居民喜欢把它们放在一排茶杯里,这样可以观察它们跳来跳去. 如下图,有一排杯子,左边的一个是空着的,右边的杯子,每个里 ...

  6. java实现第四届蓝桥杯有理数类

    有理数类 题目描述 有理数就是可以表示为两个整数的比值的数字.一般情况下,我们用近似的小数表示.但有些时候,不允许出现误差,必须用两个整数来表示一个有理数. 这时,我们可以建立一个"有理数类 ...

  7. centos6.5 安装 clickhouse

    概述:clickhouse是一个高性能的列式数据库,特点就是快快快,查询性能是mysql的100-1000倍,非常适合存储频繁写入的数据,比如:日志,用户事件记录.单表存储上亿甚至十几亿行数据库查询都 ...

  8. Azure AD(四)知识补充-服务主体

    一,引言 又到了新的一周了,也到了我新的分享的时间了,还记得上一周立得Flag,其中 “保证每周输出一篇文章” ,让我特别“在意”(这里用词不太恰当).主要是我的一个大学舍友,他突然问了我一个关于写博 ...

  9. MySql轻松入门系列——第二站 使用visual studio 对mysql进行源码级调试

    一:背景 1. 讲故事 上一篇说了mysql的架构图,很多同学反馈说不过瘾,毕竟还是听我讲故事,那这篇就来说一说怎么利用visual studio 对 mysql进行源码级调试,毕竟源码面前,不谈隐私 ...

  10. [C#.NET 拾遗补漏]05:操作符的几个骚操作

    阅读本文大概需要 1.5 分钟. 大家好,这是极客精神[C#.NET 拾遗补漏]专辑的第 5 篇文章,今天要讲的内容是操作符. 操作符的英文是 Operator,在数值计算中习惯性的被叫作运算符,所以 ...