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的更多相关文章

  1. Poj 3613 Cow Relays (图论)

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

  2. 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

    倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...

  3. poj 3613 Cow Relays

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

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

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

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

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

  6. POJ 3613 Cow Relays【k边最短路】

    题目链接:http://poj.org/problem?id=3613 题目大意: 给出n头牛,t条有向边,起点以及终点,限制每头牛放在一个点上,(一个点上可以放多头牛),从起点开始进行接力跑到终点, ...

  7. 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 ...

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

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

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

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

随机推荐

  1. Jmeter---后置处理器 BeanShell PostProcessor 获取JDBC结果(多行)并以列表传入另一个请求

    之前用python+locust对脚本生成商品编码, 商品上架,购买商品进行编写脚本和压测: 开始是打算用Jmeter,后来遇到问题在 Jmeter如何读取JDBC多行并组成列表,作为下一个请求 一直 ...

  2. hdu5993/2016icpc青岛L

    zz:https://www.cnblogs.com/ytytzzz/p/9674661.html 题意:给一棵树,每次询问删掉两条边,问剩下的三棵树的最大直径点10W,询问10W,询问相互独立 So ...

  3. 大二 Java上学期总结

    一学期的Java学习结束了,这学期对程序语言的理解更深了,首先感谢李津老师的教导,这学期收获挺多的,不像上学期,这学期没有任何缺课表现,希望之后的语言程序学习会更加努力. 突然感觉Java的学习如此之 ...

  4. 一些重温CSS需要注意的小细节

    <!-- CSS是用于描述页面展示的语言 字体.颜色.大小.间距,将内容分为多列 或者简单的动画及其他的装饰效果 决定了长啥样 html房子的骨架 css负责装修 怎么装修一个房子呢? 首先就是 ...

  5. 比较css中单位px,em和rem的区别

    国内的设计师大都喜欢用px,而国外网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? px特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原 ...

  6. 【监控笔记】【1.4】Pssdiag和Sqldiag管理器

    --没有实操过,有点复杂,先写上以后有用到再深入研究 统计与诊断数据是任何 SQL故障修复工作的关键所在. 如果没有掌握这些数据,就无法确定数据性能问题的根源.数据表的瓶颈可能并不是由索引问题造成的: ...

  7. Java数据结构之栈(Stack)

    1.栈(Stack)的介绍 栈是一个先入后出(FILO:First In Last Out)的有序列表. 栈(Stack)是限制线性表中元素的插入和删除只能在同一端进行的一种特殊线性表. 允许插入和删 ...

  8. 程序员听到bug后的N种反应…

    程序员的世界里, 不止有代码, 还有bug,bug,bug- 当出现bug时, 程序员们的反应是怎样的呢? 作者:苏小喵,来源:小花小画(微信号:hua-little) - END - 推荐阅读: 1 ...

  9. HNUSTOJ 1604:Operations

    1604: Operations 时间限制: 2 Sec  内存限制: 128 MB 提交: 313  解决: 97 [提交][状态][讨论版] 题目描述 You can perform the fo ...

  10. 我的第一个Delphi DLL

    library dd; { 使用字符串参数或嵌套字符串参数需要在uses子句中包括sharemm单元,并将BorlandMM.dll与您的应用程序一起发布. 否则需要对参数值使用PChar或Short ...