[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 ...
随机推荐
- C语言第三次博客作业—循环结构
一.PTA实验作业 题目1 1.实验代码 int N,i; //N为用户数 char sex; //sex表示性别 double High; //Hight表示身高 scanf("%d&qu ...
- 20162327WJH第五周作业
学号 20162327 <程序设计与数据结构>第5周学习总结 教材学习内容总结 1.java是一种面向对象的语言.面向对象是一种编程方法.更是一种思维方式. 2.面向对象编程的终极目标是消 ...
- 关于mule中使用jdbc时报No Suitable Driver found错误的问题
错误大概信息: Exception in thread "main" org.mule.module.launcher.DeploymentStartException: SQLE ...
- OpenGL中怎么把世界坐标系变成屏幕坐标系
对这个3D坐标手动进行OpenGL的四个变换,得到的结果就是屏幕上的像素坐标.前三个变换(Model, View, Projection)都是4x4矩阵,操作对象是四维向量,所以需要把(100, 10 ...
- vue中一个dom元素可以绑定多个事件?
其实这个问题有多个解决方法的 这里提出两点 第一种 第二种 现在dom上绑定一个 然后在你的methods中直接调用 如果要传参数 这时候千万别忘记 原创 如需转载注明出处 谢谢
- String s=new String("abc")产生了几个对象?[权威面试版]
以下总结是我逛论坛 将零零碎碎的知识整理起来,方便自己记忆和阅读,顺便分享出来给大家学习. 若 String s=new String("abc"); 为第一句代码 则会产生两个对 ...
- JAVA_SE基础——21.二维数组的定义
2 二维数组的定义 基本与一维数组类似 //定义一个3行5列的二维数组 //方法1,先new对象,然后再初始化每个元素 int[][] a = new int[3][5]; a[0][0]=1; a[ ...
- JAVA_SE基础——7.常量&变量
上一篇,我讲了标识符&关键字 这篇我来解释下变量&常量~~~ 变量与常量这两个概念相信大家都不会感到陌生,在数学中就已经涉及了变量与常量.理解变量与常量,可以举这样一个例子: 例 ...
- 泛型的 typeof
static void Main(string[] args) { TestTypeOf<string>(); Console.ReadKey(); } static void TestT ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...