poj 3613Cow Relays
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
Source USACO 2007 November Gold
题面
用一个矩阵a(i, j)来表示i到j经过若干条边的最短路,
初始化a为i到j边的长度,没有则是正无穷。
比如a矩阵表示经过n条边,b矩阵表示经过m条边,
那么a * b得到的矩阵表示经过m + n条边,
采用Floyd的思想进行更新。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<string>
#include<map>
#define ll long long
using namespace std;
ll n,m,S,T,l;
map<ll,ll>id;
struct node{
ll a[][];
friend node operator *(node x,node y)
{
node z;
memset(z.a,0x3f,sizeof(z.a));
for(ll k=;k<=l;k++)
for(ll i=;i<=l;++i)
for(ll j=;j<=l;++j)
z.a[i][j]=min(z.a[i][j],x.a[i][k]+y.a[k][j]);
return z;
}
}s,ans;
void ksm()
{
ans=s;
n--;
while(n)
{
if(n&) ans=ans*s;
s=s*s;
n>>=;
}
}
int main()
{
freopen("run.in","r",stdin);
freopen("run.out","w",stdout);
memset(s.a,0x3f,sizeof(s.a));
scanf("%lld%lld%lld%lld",&n,&m,&S,&T);
for(ll i=,x,y,z;i<=m;++i)
{
scanf("%lld%lld%lld",&z,&x,&y);
if(id[x]) x=id[x];
else l++,id[x]=l,x=l;
if(id[y]) y=id[y];
else l++,id[y]=l,y=l;
s.a[x][y]=s.a[y][x]=z;
}
S=id[S];T=id[T];
ksm();
printf("%lld",ans.a[S][T]);
return ;
}
/*
2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9
10
*/
poj 3613Cow Relays的更多相关文章
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑
倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- POJ 3613 Cow Relays(floyd+快速幂)
http://poj.org/problem?id=3613 题意: 求经过k条路径的最短路径. 思路: 如果看过<矩阵乘法在信息学的应用>这篇论文就会知道 现在我们在邻接矩阵中保存距离, ...
- POJ 3613 Cow Relays 恰好n步的最短路径
http://poj.org/problem?id=3613 题目大意: 有T条路.从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min ...
- POJ 3613 Cow Relays【k边最短路】
题目链接:http://poj.org/problem?id=3613 题目大意: 给出n头牛,t条有向边,起点以及终点,限制每头牛放在一个点上,(一个点上可以放多头牛),从起点开始进行接力跑到终点, ...
- Cow Relays POJ - 3613 (floyd+快速幂)
For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race usin ...
- POJ 3613 Cow Relays (floyd + 矩阵高速幂)
题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B B[i][j] 就表示 i-j 刚好走过两条路的方法数 那么同理 我们把 ...
- poj 3613 Cow Relays【矩阵快速幂+Floyd】
!:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...
随机推荐
- 【Linux开发】Linux V4L2驱动架构解析与开发导引
Linux V4L2驱动架构解析与开发导引 Andrew按:众所周知,linux中可以采用灵活的多层次的驱动架构来对接口进行统一与抽象,最低层次的驱动总是直接面向硬件的,而最高层次的驱动在linux中 ...
- call 和 apply的定义和区别?
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- 根据对象属性查找对象或者数组(根据对象属性查找某数组内符合该条件的对象,数组内对象属性check为true的对象,存放到数组内) 滚动轴样式
1.根据对象属性查找某数组内符合该条件的对象. optionComwords:[ {optionName:"名称1", optionCode: '1'}, {optionNam ...
- linux 正则表达式 使用grep命令
最常应用正则表达式命令是 awk sed grep [root@MongoDB ~]# cat mike.log I am mike! I like linux. I like play footba ...
- [转帖]socat使用笔记
socat使用笔记 https://blog.csdn.net/yangbingzhou/article/details/49783235 进行简单学习 centos 下面安装 yum install ...
- jmeter应用之批量插入数据
上一篇讲到如何在jmeter中配置并连接使用mysql数据库,这一节主要是讲数据库连接的简单应用——批量插入数据 总体步骤如下: 1)新建线程组和添加JDBC Connection Configura ...
- 关于Java多线程的一些面试问题
1.ArrayList和Vecoter区别? Array和ArrayList的异同点一.Array和ArrayList的区别#1. Array类型的变量在声明的同时必须进行实例化(至少得初始化数组的大 ...
- P4290 [HAOI2008]玩具取名
传送门 $dp$ 设 $f[i][j][k]$ 表示初始为 $k$ 时,能否得到 $[i,j]$ 这一段子串 设 $pd[i][j][k]$ 表示长度为二的字符串 $ij$ 能否由 $k$ 得到 然后 ...
- python学习笔记(5)
第七章 模式匹配和正则表达式 1.不用正则表达式来查找文本模式 #对于这样的一个文本查找:3个数字,一个短横线,3个数字,4个端横线,然后再是4个数字,如:415-555-4242def isPh ...
- IBM公司的面试题,看看你能做出多少。
进入IBM差不多是每一个IT人的梦想.IBM公司向来以高素质人才作为企业持续竞争力的保证,所以经常出一些千奇百怪的面试题,来考验一个人的综合能力,以下是5道IBM曾经出过的面试题,看看你能作出几道: ...