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. UTF-8编码下'\u7528\u6237'转换为中文汉字'用户'

    UTF-8编码下'\u7528\u6237'转换为中文'用户' 一.前言 有过多次,在开发项目中遇见设置文件编码格式为UTF-8,但是打开该文件出现类似\u7528这样的数据,看也看不懂,也不是平常见 ...

  2. NO.1:自学tensorflow之路------神经网络背景知识

    引言 从本周,我将开始tensorflow的学习.手头只有一本<tensorflow:实战Google深度学习框架>,这本书对于tensorflow的入门有一定帮助.tensorflow中 ...

  3. 吴恩达 Deep learning 第一周 深度学习概论

    知识点 1. Relu(Rectified Liner Uints 整流线性单元)激活函数:max(0,z) 神经网络中常用ReLU激活函数,与机器学习课程里面提到的sigmoid激活函数相比有以下优 ...

  4. 华为笔试——C++消重输出

    题目:消重输出 题目介绍: 输入一个正整数,给出消除重复数字以后最大的整数,注意需要考虑长整数. 例: 输入:988274320 输出:9874320 题目分析:这个结果的实现需要两个步骤:消重和排序 ...

  5. A Product Recall 产品召回

    Rick: The Board of Directors has come to a decision. Our company will take an image hit, and it's go ...

  6. 如何成为优秀评级卖家(Top-rated seller)?与超级卖家的区别是

    以eBay美国站点为例,要成为优秀评级卖家(Top-rated seller),需满足如下条件: ● 先成为 eBay超级卖家 ● Low DSR (US buyers) <= 0.50% 或 ...

  7. 英文Datasheet没那么难读

    话说学好数理化,走遍天下都不怕.可是在这个所谓的全球化时代,真要走遍天下的话,数理化还真未必比得上一门外语.作为技术人员,可以看到的是目前多数前沿的产品和技术多来自发达的欧美等国家,而英语目前才是真正 ...

  8. TDGA-需求分析

    李青:绝对的技术控,团队中扮演“猪”的角色,勤干肯干,是整个团队的主心骨,课上紧跟老师的步伐,下课谨遵老师的指令,课堂效率高,他的编程格言“没有编不出来的程序,只有解决不了的bug”. 胡金辉:半两油 ...

  9. lintcode-491-回文数

    491-回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未必了. 样例 11, 121, 1 ...

  10. lintcode-397-最长上升连续子序列

    397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...