Cow Relays
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7683   Accepted: 3017

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

Source

题意:求从s到e恰好经过n个点的最短路。
分析:给我的第一印象就是暴力,想dp但没啥思路,没想到还用更厉害的做法。
预备知识:《矩阵乘法在信息学中的应用》,主要是看邻接矩阵那一块,可以发现,我们可以把图用邻接矩阵的方式存起来,那么a[i][j]就是i到j的路径长度,这是读入后存起来的矩阵,其中每两个点之间的路径不经过其它的点,如果我们将它和自己相乘,就会得到一个新矩阵,其实a[i][j]就是i到j经过1个点的最短路径,当然,直接乘是不能得到最短路径的,我们需要floyd算法。
     可是这个N这么大,意味着我们要做n-1次floyd,这个复杂度妥妥的TLE啊,但是因为这个矩阵每次都和自己相乘,所以可以想到利用快速幂,这里套用快速幂的算法即可。
     还有一个问题:点数有点大,数组可能会开不下,那么怎么做呢?很简单,离散化即可.
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm> #define inf 0x7ffffff using namespace std; int n, t, s, e,ans[][],a[][],d[][],cnt,lisan[],temp[][]; void floyd1()
{
for (int k = ; k <= cnt; k++)
for (int i = ; i <= cnt; i++)
for (int j = ; j <= cnt; j++)
d[i][j] = min(ans[i][k] + a[k][j], d[i][j]);
memcpy(ans, d, sizeof(ans));
memset(d, 0x3f, sizeof(d));
} void floyd2()
{
for (int k = ; k <= cnt; k++)
for (int i = ; i <= cnt; i++)
for (int j = ; j <= cnt; j++)
temp[i][j] = min(temp[i][j], a[i][k] + a[k][j]);
memcpy(a, temp, sizeof(a));
memset(temp, 0x3f, sizeof(temp));
} int main()
{
scanf("%d%d%d%d", &n, &t, &s, &e);
memset(ans, 0x3f, sizeof(ans));
memset(a, 0x3f, sizeof(a));
memset(d, 0x3f, sizeof(d));
memset(temp, 0x3f, sizeof(temp));
for (int i = ; i <= ; i++)
ans[i][i] = ;
for (int i = ; i <= t; i++)
{
int w, x, y;
scanf("%d%d%d", &w, &x, &y);
if (!lisan[x])
lisan[x] = ++cnt;
if (!lisan[y])
lisan[y] = ++cnt;
a[lisan[x]][lisan[y]] = a[lisan[y]][lisan[x]] = min(a[lisan[x]][lisan[y]], w);
}
while (n)
{
if (n & )
floyd1();
floyd2();
n >>= ;
}
printf("%d\n", ans[lisan[s]][lisan[e]]); //while (1);
return ;
}

floyd算法初始化弄错了,WA了几次,智障地发现每个点和自己的路径长度竟然初始化成了inf,TAT.

 

poj3613Cow Relays的更多相关文章

  1. poj3613Cow Relays——k边最短路(矩阵快速幂)

    题目:http://poj.org/problem?id=3613 题意就是求从起点到终点的一条恰好经过k条边的最短路: floyd+矩阵快速幂,矩阵中的第i行第j列表示从i到j的最短路,矩阵本身代表 ...

  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. POJ3613 Cow Relays [矩阵乘法 floyd类似]

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Descri ...

  5. BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德

    BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们 ...

  6. poj3613 Cow Relays【好题】【最短路】【快速幂】

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Descrip ...

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

    [BZOJ1706][usaco2007 Nov]relays 奶牛接力跑 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项 ...

  8. Cow Relays 【优先队列优化的BFS】USACO 2001 Open

    Cow Relays Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Tota ...

  9. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

随机推荐

  1. Spring Boot 整合JDBC 实现后端项目开发

    一.前言 二.新建Spring Boot 项目 三.Spring Boot 整合JDBC 与MySQL 交互 3.1 新建数据表skr_user 3.2 Jdbcproject 项目结构如下 3.3 ...

  2. 测试类异常Manual close is not allowed over a Spring managed SqlSession

    在用Spring 和mybatis整合的 写测试类的时候报出解决办法:在全局配置文件   class="org.mybatis.spring.SqlSessionTemplate" ...

  3. Linux内核学习笔记(2)-- 父进程和子进程及它们的访问方法

    Linux系统中,进程之间有一个明显的继承关系,所有进程都是 PID 为1的 init 进程的后代.内核在系统启动的最后阶段启动 init 进程.该进程读取系统的初始化脚本(initscript)并执 ...

  4. 【Python入门学习】闭包&装饰器&开放封闭原则

    1. 介绍闭包 闭包:如果在一个内部函数里,对在外部作用域的变量(不是全局作用域)进行引用,那边内部函数被称为闭包(closure) 例如:如果在一个内部函数里:func2()就是内部函数, 对在外部 ...

  5. 网络安全攻防----html基础

    一.学习html的硬件条件 网页(.Html)是静态的,学习html语言需要选择Chrome浏览器和Sublime3编辑器,选择Chrome浏览器是因为Chrome浏览器兼容性比较好,支持各种各样的插 ...

  6. 亚马逊中国耳机巨头Jabra官方旗舰店上线

    日前,亚马逊中国(Z.cn)宣布,全球无线技术顶级领导品牌 Jabra (捷波朗)官方旗舰店正式上线,产品品类涵盖蓝牙耳机.音乐耳机.无线音箱和车载系列产品.Jabra 旗舰店上线伊始便汇集了 60 ...

  7. mac react-native从零开始android真机测试

    1. 安装android相关jdk,(https://blog.csdn.net/vvv_110/article/details/72897142) 2. 手机和mac使用usb连接, 手机开发者设置 ...

  8. 深入react技术栈解读

    1. react实现virtual DOM ,如果要改变页面的内容,还是需要执行DOM操作,比原生操作DOM多了virtualDOM的操作(计算,对比等), 应该是更耗性能??? 2. react特点 ...

  9. Chrome 鲜为人知的秘籍(内部协议)&&Chrome功能指令大全

    楼主以 Chrome 版本 39.0.2171.95 m 为例,耗费2小时的记录: chrome://accessibility 用于查看浏览器当前访问的标签,打开全局访问模式可以查看:各个标签页面的 ...

  10. EXT4.1表单提交(非AJAX)

    Ext.require([ 'Ext.form.*', 'Ext.data.*' ]); Ext.onReady(function(){ Ext.apply(Ext.form.VTypes, { re ...