poj 3613 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: 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
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的更多相关文章
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- 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条有向边,起点以及终点,限制每头牛放在一个点上,(一个点上可以放多头牛),从起点开始进行接力跑到终点, ...
- 【floyd+矩阵乘法】POJ 3613 Cow Relays
Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a rel ...
- 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级别的,所 ...
- poj 3613 Cow Relays(矩阵的图论意义)
题解 用一个矩阵来表示一个图的边的存在性,即矩阵C[i,j]=1表示有一条从i到j的有向边C[i,j]=0表示没有从i到j的边.这个矩阵的k次方后C[i,j]就表示有多少条从i到j恰好经过k条边的路径 ...
- POJ 3613 [ Cow Relays ] DP,矩阵乘法
解题思路 首先考虑最暴力的做法.对于每一步,我们都可以枚举每一条边,然后更新每两点之间经过\(k\)条边的最短路径.但是这样复杂度无法接受,我们考虑优化. 由于点数较少(其实最多只有\(200\)个点 ...
随机推荐
- 为SQL表添加全文索引范例
--范例: --为HR_Job中的JobTitle,JobDes创建全文索引 execute sp_fulltext_catalog 'boli188', 'create' --创建全文目录,boli ...
- Web自动化框架搭建之二基于数据驱动应用简单实例~~
整体框架,先划分成细小功能模块~~,从最简单的开始,介绍 实现循环百度搜索实例: #coding=utf-8 '''Created on 2014��6��9�� @author: 小鱼'''impo ...
- Myeclipse8.5 svn插件安装两种方式
第一种方式:(亲测成功)第一步:准备插件包:site-1.6.18.zip解压该包里面有features和plugins文件夹,删除该包里面的xml结尾的文件. 第二:我的Myeclipse8.5安装 ...
- 使用python的logging模块
一.从一个使用场景开始 开发一个日志系统, 既要把日志输出到控制台, 还要写入日志文件 import logging # 创建一个logger logger = logging.getLogger(' ...
- JSON2 源代码
/* json2.js 2014-02-04 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. See ht ...
- (转载)OC学习篇之---类的三大特性:封装,继承,多态
之前的一片文章介绍了OC中类的初始化方法和点语法的使用,今天来继续学习OC中的类的三大特性,我们在学习Java的时候都知道,类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今 ...
- [HIve - LanguageManual] Sort/Distribute/Cluster/Order By
Syntax of Order By Syntax of Sort By Difference between Sort By and Order By Setting Types for Sort ...
- java 图像分析与处理库
OpenCv4Android: http://opencv.org/platforms/android.html opencv官方中文文档: http://www.opencv.org.cn/open ...
- nova --debug image-list
nova --debug image-list DEBUG (session:) REQ: curl -g -i -X GET http://liberty-aio:35357/v3 -H " ...
- homework-06
围棋问题 关于代码的阅读,写注释,我的代码阅读量和阅读能力都有限,而且是关于没有基础的围棋问题,JAVA和C#混合的程序.不免参考了其他同学的思路,忘老师见谅. 1.playPrev(GoMove) ...