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

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: NTS, 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

n次floyd,原来flody也能够像矩阵一样高速幂。详细的能够看论文《矩阵乘法在信息学的应用》

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int inf=(1<<30)-1;
int n;
int hash[3010];//映射
struct matrix
{
int ma[210][210];
matrix()
{
for(int i=0;i<210;i++)
for(int j=0;j<210;j++)
ma[i][j]=inf;
}
};
matrix floyd(matrix x,matrix y)
{
matrix ans;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
ans.ma[i][j]=min(ans.ma[i][j],x.ma[i][k]+y.ma[k][j]);
}
}
return ans;
}
matrix pow(matrix a,int m)
{
matrix ans;
for(int i=1;i<=n;i++)
ans.ma[i][i]=0;
while(m)
{
if(m&1)
ans=floyd(ans,a);
a=floyd(a,a);
m=m>>1;
}
return ans;
}
int main()
{
int k,t,s,e;
while(~scanf("%d%d%d%d",&k,&t,&s,&e))
{
int x,y,d;
memset(hash,0,sizeof(hash));
n=1;
matrix a;
for(int i=0;i<t;i++)
{
scanf("%d%d%d",&d,&x,&y);
if(!hash[x])
hash[x]=n++;
if(!hash[y])
hash[y]=n++;
a.ma[hash[x]][hash[y]]=a.ma[hash[y]][hash[x]]=d;
}
n=n-1;
matrix ans;
ans=pow(a,k);
printf("%d\n",ans.ma[hash[s]][hash[e]]);
}
return 0;
}

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+快速幂)

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

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

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

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

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

  5. 【floyd+矩阵乘法】POJ 3613 Cow Relays

    Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a rel ...

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

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

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

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

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

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

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

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

随机推荐

  1. cgroup隔离的知识点

    tasks中写入的是线程号 cgroup.procs是进程号 ===================CPU隔离===================== 主机CPU核数: cat /proc/cpui ...

  2. Color Length

    题意: 给出两个字符串,求把两字符串组成一个字符串使的字符串中的相同字母的最远距离的和最小. 分析: 本题关键在于怎么计算距离和的方法上.dp[i][j]表示处理到长度i的a串,长度j的b串还需要的计 ...

  3. HDU 5278 PowMod 数论公式推导

    题意:中文题自己看吧 分析:这题分两步 第一步:利用已知公式求出k: 第二步:求出k然后使用欧拉降幂公式即可,欧拉降幂公式不需要互质(第二步就是BZOJ3884原题了) 求k的话就需要构造了(引入官方 ...

  4. <转>如何测试大型ERP软件?

    大型ERP软件是一个在企业范围内部应用的.高度集成的软件,且操作频繁,数据在各业务系统之间高度共享.那么针对大型ERP软件的特点,我们应该怎么测试呢?要使用怎样的测试方法?需要什么样的测试人员?... ...

  5. QT多线程笔记

    1.QT多线程涉及到主线程和子线程之间交互大量数据的时候,使用QThread并不方便,因为run()函数本身不能接受任何参数,因此只能通过信号和槽的交互来获取数据,如果只是单方面简单交互数据还过得去, ...

  6. 大连网络赛 1006 Football Games

    //大连网络赛 1006 // 吐槽:数据比较水.下面代码可以AC // 但是正解好像是:排序后,前i项的和大于等于i*(i-1) #include <bits/stdc++.h> usi ...

  7. c++ 概念及学习/c++ concept&learning(二)

    上篇内容讲述了整个语言的发展[为什么会产生编程语言],以及学习C++所需要掌握的内容.这节开始认识第一部分最基本的内容:C++的内建类型,也就是基本类型. 在这些知识之前留一个问题:为什么基本所有语言 ...

  8. [算法] 选择排序 Selection sort

    选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然 ...

  9. FMX架构图

  10. 第二百九十、一、二天 how can I 坚持

    上周日,腊八,好冷,去菏泽了.坐火车,没座.好冷,下午菏泽学院,大体看了看,很是怀念,有点遗憾,在网吧玩的时间太长了,留不住. 周一,长太婚礼,竟然没有只是吃了个饭,有点小失望,下午坐车回京,两点坐的 ...