[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 ...
随机推荐
- beta冲刺5-咸鱼
昨天的问题: 登陆页面的整合重新制作 各主机版本更迭 我的社团显示功能修改调整 主页的头部替换掉 +修复帖子无法显示内容的问题 +试着将邮箱等判定用正则表达式进行实时判定. 今天的完成: 主要是线下进 ...
- 高级软件工程2017第7次作业--C++团队项目:Beta阶段综合报告
1.Beta阶段敏捷冲刺每日报告 Bate版敏捷冲刺报告--day0 Bate版敏捷冲刺每日报告--day1 Bate敏捷冲刺每日报告--day2 Bate敏捷冲刺每日报告--day3 Bate敏捷冲 ...
- Python 单向循环链表
操作 is_empty() 判断链表是否为空 length() 返回链表的长度 travel() 遍历 add(item) 在头部添加一个节点 append(item) 在尾部添加一个节点 inser ...
- iOS 播放音频的几种方法
Phone OS 主要提供以下了几种播放音频的方法: System Sound Services AVAudioPlayer 类 Audio Queue Services OpenAL 1. Syst ...
- java语法基础(总结)
1,关键字:其实就是某种语言赋予了特殊含义的单词. 保留字:其实就是还没有赋予特殊含义,但是准备日后要使用过的单词. 2,标示符:其实就是在程序中自定义的名词.比如类名,变量名,函数名.包含 0-9. ...
- php的打印sql语句的方法
echo M()->_sql(); 这样就可以调试当前生成的sql语句: //获取指定天的开始时间和结束时间 $datez="2016-05-12"; $t = strtot ...
- OpenShift实战(二):OpenShift节点扩容
1.新增节点信息 增加节点如下,请将xxx改为自己的域名 node6.xxx.net Node 192.168.8.90 8G 20G/60G 4C node7.xxx.net Node 192.16 ...
- C#微信公众号——自定义菜单
自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单.一级菜单最多4个汉字,二级菜单最多7个汉字,多出来的部分将会以“...”代替.自定义菜单的介绍,可以看官方开发文档http://mp. ...
- centOs6.5配置jdk及其注意事项
1.下载jdk1.7 百度云链接: https://pan.baidu.com/s/15vXLO2eV18eVvmt-R5jGnQ 密码: 1gd6 2.解压压缩包 通过终端在/usr/local下新 ...
- NHibernate的基本使用
一.O/R Mapping 概论 工厂模式+反射+每个数据库的DAL层来解决数据访问层的代码 针对数据库表中字段的变化我们是无法预料的,所以每一次用户需求的修改都会直接导致我们程序员来修改—实体类(B ...