[USACO 07NOV]Cow Relays
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.
给出一张无向连通图,求S到E经过k条边的最短路。
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$条边的从$S$到$T$的最短路。
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
#define RE register
#define IL inline
using namespace std;
const int INF=1e9; IL int Min(int a,int b){return a<b ? a:b;} int num[],pos;
int f[][];
int k,m,s,e,u,v,l;
struct mat
{
int a[][];
mat() {for (int i=;i<=pos;i++)for (int j=;j<=pos;j++) a[i][j]=INF;}
mat operator * (const mat &b)
{
mat ans;
for (RE int i=;i<=pos;i++)
for (RE int j=;j<=pos;j++)
for (RE int k=;k<=pos;k++)
ans.a[i][j]=Min(ans.a[i][j],a[i][k]+b.a[k][j]);
return ans;
}
}; int main()
{
scanf("%d%d%d%d",&k,&m,&s,&e);
for (RE int i=;i<=m;i++)
{
scanf("%d%d%d",&l,&u,&v);
if (!num[u]) num[u]=++pos;
if (!num[v]) num[v]=++pos;
f[num[u]][num[v]]=f[num[v]][num[u]]=l;
}
mat S,T;
for (RE int i=;i<=pos;i++) for (RE int j=;j<=pos;j++) if (f[i][j]) S.a[i][j]=T.a[i][j]=f[i][j];
k--;
while (k)
{
if (k&) S=S*T;
k>>=;
T=T*T;
}
printf("%d\n",S.a[num[s]][num[e]]);
return ;
}
矩乘
解法二:
利用倍增的思想。令$f[i][j][t]$表示从$i$到$j$经过$2^t$条边的最优值,做一遍$floyd$再统计答案即可。
#include<cmath>
#include<queue>
#include<ctime>
#include<stack>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=1e9; int num[],pos;
int f[][][];
int towards[][];
bool t;
int k,m,s,e,u,v,l; int main()
{
memset(f,/,sizeof(f));
memset(towards,/,sizeof(towards));
scanf("%d%d%d%d",&k,&m,&s,&e);
for (int i=;i<=m;i++)
{
scanf("%d%d%d",&l,&u,&v);
if (!num[u]) num[u]=++pos;
if (!num[v]) num[v]=++pos;
f[num[u]][num[v]][]=f[num[v]][num[u]][]=l;
}
int lim=log2(k);
for (int p=;p<=lim;p++)
for (int q=;q<=pos;q++)
for (int i=;i<=pos;i++)
for (int j=;j<=pos;j++)// if (i!=j&&q!=i)
if (f[i][j][p]>f[i][q][p-]+f[q][j][p-])
f[i][j][p]=f[i][q][p-]+f[q][j][p-];
int p=;
towards[num[s]][t]=;
while (k!=)
{
if (k&)
{
t=!t;
for (int i=;i<=pos;i++)
{
towards[i][t]=INF;
for (int j=;j<=pos;j++)
if (towards[i][t]>towards[j][!t]+f[i][j][p])
towards[i][t]=towards[j][!t]+f[i][j][p];
}
}
p++;
k=k>>;
}
printf("%d\n",towards[num[e]][t]);
return ;
}
倍增
[USACO 07NOV]Cow Relays的更多相关文章
- 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: 7335 Accepted: 2878 Descri ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- 3298: [USACO 2011Open]cow checkers
3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 65 Solved: 26[Su ...
- BZOJ3298: [USACO 2011Open]cow checkers(佐威夫博弈)
3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 195 Solved: 96[S ...
- bzoj 3298: [USACO 2011Open]cow checkers -- 数学
3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec Memory Limit: 128 MB Description 一天,Besssie准备 ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- 「POJ3613」Cow Relays
「POJ3613」Cow Relays 传送门 就一个思想:\(N\) 遍 \(\text{Floyd}\) 求出经过 \(N\) 个点的最短路 看一眼数据范围,想到离散化+矩阵快速幂 代码: #in ...
随机推荐
- alpha-咸鱼冲刺day2-紫仪
总汇链接 一,合照 emmmmm.自然是没有的. 二,项目燃尽图 三,项目进展 今天并没有什么进展,弄了好久好像也只研究出怎么把JS的功能块插入进去.html的信息提交这些还不知道要怎么弄. 四,问题 ...
- MySQL 服务安装及命令使用
MySQL 服务安装及命令使用 课程来源说明 本节实验后续至第17节实验为本课程的进阶篇,都基于 MySQL 官方参考手册制作,并根据实验楼环境进行测试调整改编.在此感谢 MySQL 的开发者,官方文 ...
- 一个毕生难忘的BUG
记得以前接手过一个Java项目,服务器程序,直接让Jar在linux上跑的那种, 这个项目由两个web服务组成,也就是两条Java进程,主进程 xxx.jar,辅助进程 xxx_helper.jar. ...
- .net 小程序获取用户UnionID
第一次写博客,写的不好多多海涵! 1.小程序获取UnionID的流程用code去换取session_key,然后去解密小程序获取到的那串字符! 话不多说,原理大家都懂!!!!!! 直接上代码 publ ...
- python性能分析--cProfile
Python标准库中提供了三种用来分析程序性能的模块,分别是cProfile, profile和hotshot,另外还有一个辅助模块stats.这些模块提供了对Python程序的确定性分析功能,同时也 ...
- JAVA_SE基础——71.Random类制作随机验证码
public class Demo5 { public static void main(String[] args) { char[] arr={'s','b','g','h','a','c'}; ...
- Connect Appium Server Fail.A new session could not be created
1.由于安卓测试机性能低下,并不能支持测试工作,想安装一个模拟器帮助测试,然后发现群里有朋友发了一个夜神模拟器..下载..安装..美滋滋的准备运行脚本.What..居然报错了..orz..然后百度查找 ...
- hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(4)SPARK 安装
hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(4)SPARK 安装 一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh ...
- 用Jmeter实现SQLServer数据库的增删查改
1.添加线程组 Jmeter性能测试,最重要的就是线程组了,线程组相当于用户活动 2.添加JDBC Connection Configuration Database URL:jdbc:sqlserv ...
- python多进程之间的通信:消息队列Queue
python中进程的通信:消息队列. 我们知道进程是互相独立的,各自运行在自己独立的内存空间. 所以进程之间不共享任何变量. 我们要想进程之间互相通信,传送一些东西怎么办? 需要用到消息队列!! 进程 ...