poj3613Cow 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: 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
Source
#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的更多相关文章
- poj3613Cow Relays——k边最短路(矩阵快速幂)
题目:http://poj.org/problem?id=3613 题意就是求从起点到终点的一条恰好经过k条边的最短路: floyd+矩阵快速幂,矩阵中的第i行第j列表示从i到j的最短路,矩阵本身代表 ...
- 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑
倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德
BZOJ_[usaco2007 Nov]relays 奶牛接力跑_离散化+倍增弗洛伊德 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们 ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- 【BZOJ1706】[usaco2007 Nov]relays 奶牛接力跑 矩阵乘法
[BZOJ1706][usaco2007 Nov]relays 奶牛接力跑 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项 ...
- Cow Relays 【优先队列优化的BFS】USACO 2001 Open
Cow Relays Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
随机推荐
- UTF-8编码下'\u7528\u6237'转换为中文汉字'用户'
UTF-8编码下'\u7528\u6237'转换为中文'用户' 一.前言 有过多次,在开发项目中遇见设置文件编码格式为UTF-8,但是打开该文件出现类似\u7528这样的数据,看也看不懂,也不是平常见 ...
- NO.1:自学tensorflow之路------神经网络背景知识
引言 从本周,我将开始tensorflow的学习.手头只有一本<tensorflow:实战Google深度学习框架>,这本书对于tensorflow的入门有一定帮助.tensorflow中 ...
- 吴恩达 Deep learning 第一周 深度学习概论
知识点 1. Relu(Rectified Liner Uints 整流线性单元)激活函数:max(0,z) 神经网络中常用ReLU激活函数,与机器学习课程里面提到的sigmoid激活函数相比有以下优 ...
- 华为笔试——C++消重输出
题目:消重输出 题目介绍: 输入一个正整数,给出消除重复数字以后最大的整数,注意需要考虑长整数. 例: 输入:988274320 输出:9874320 题目分析:这个结果的实现需要两个步骤:消重和排序 ...
- A Product Recall 产品召回
Rick: The Board of Directors has come to a decision. Our company will take an image hit, and it's go ...
- 如何成为优秀评级卖家(Top-rated seller)?与超级卖家的区别是
以eBay美国站点为例,要成为优秀评级卖家(Top-rated seller),需满足如下条件: ● 先成为 eBay超级卖家 ● Low DSR (US buyers) <= 0.50% 或 ...
- 英文Datasheet没那么难读
话说学好数理化,走遍天下都不怕.可是在这个所谓的全球化时代,真要走遍天下的话,数理化还真未必比得上一门外语.作为技术人员,可以看到的是目前多数前沿的产品和技术多来自发达的欧美等国家,而英语目前才是真正 ...
- TDGA-需求分析
李青:绝对的技术控,团队中扮演“猪”的角色,勤干肯干,是整个团队的主心骨,课上紧跟老师的步伐,下课谨遵老师的指令,课堂效率高,他的编程格言“没有编不出来的程序,只有解决不了的bug”. 胡金辉:半两油 ...
- lintcode-491-回文数
491-回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未必了. 样例 11, 121, 1 ...
- lintcode-397-最长上升连续子序列
397-最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 注意事 ...