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. leetcode-每个节点的右向指针(填充同一层的兄弟节点)

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  2. H2O Driverless AI

    H2O Driverless AI(H2O无驱动人工智能平台)是一个自动化的机器学习平台,它给你一个有着丰富经验的“数据科学家之盒”来完成你的算法. 使AI技术得到大规模应用 各地的企业都意识到人工智 ...

  3. Live Archive 训练题 2019/3/9

    7454 Parentheses A bracket is a punctuation mark, which is used in matched pairs, usually used withi ...

  4. Bracket Sequences Concatenation Problem括号序列拼接问题(栈+map+思维)

    A bracket(括号) sequence is a string containing only characters "(" and ")".A regu ...

  5. sprint1_11.15燃尽图(第二天)

    找相关的图片资料用于做点餐系统的界面 燃尽图:

  6. “Hello World!”团队第五周第三次会议

    今天是我们团队“Hello World!”团队第五周召开的第三次会议. 双十一大家过的怎么样?由于组内其他成员被“剁手”,今日会议记录由我来写. 博客内容: 一.会议时间 二.会议地点 三.会议成员 ...

  7. Alpha发布用户使用报告【欢迎来怼】

    目录 用户统计表 部分用户评论截图 用户统计图 总结 一.用户统计表 目前,博客园安卓版的用户已达到11位.为了采集到更加客观公正的用户评价,并没有将团队内部人员的评价统计进来.同时,为了更好地保护用 ...

  8. 软件工程团队项目第一个Sprint评论

    (1)跑男:话说我没怎么听懂这个游戏是怎么玩的,可能是由于这是第一组,所以我没有反应过来把,界面设计的还可以,但是像设置,选关,帮助真心没看懂.有一种感觉就是,这个游戏是由一堆的漂亮的图片拼起来的,还 ...

  9. 在pycharm中使用scrapy爬虫

    目标在Win7上建立一个Scrapy爬虫项目,以及对其进行基本操作.运行环境:电脑上已经安装了python(环境变量path已经设置好), 以及scrapy模块,IDE为Pycharm .操作如下: ...

  10. PAT L1-017 到底有多二

    https://pintia.cn/problem-sets/994805046380707840/problems/994805121500692480 一个整数“犯二的程度”定义为该数字中包含2的 ...