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的更多相关文章

  1. Cow Relays 【优先队列优化的BFS】USACO 2001 Open

    Cow Relays Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Tota ...

  2. POJ3613 Cow Relays [矩阵乘法 floyd类似]

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Descri ...

  3. poj3613 Cow Relays【好题】【最短路】【快速幂】

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Descrip ...

  4. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

  5. 3298: [USACO 2011Open]cow checkers

    3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 65  Solved: 26[Su ...

  6. BZOJ3298: [USACO 2011Open]cow checkers(佐威夫博弈)

    3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 195  Solved: 96[S ...

  7. bzoj 3298: [USACO 2011Open]cow checkers -- 数学

    3298: [USACO 2011Open]cow checkers Time Limit: 10 Sec  Memory Limit: 128 MB Description 一天,Besssie准备 ...

  8. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  9. 「POJ3613」Cow Relays

    「POJ3613」Cow Relays 传送门 就一个思想:\(N\) 遍 \(\text{Floyd}\) 求出经过 \(N\) 个点的最短路 看一眼数据范围,想到离散化+矩阵快速幂 代码: #in ...

随机推荐

  1. JavaScript(第二十一天)【DOM元素尺寸和位置】

    学习要点: 1.获取元素CSS大小 2.获取元素实际大小 3.获取元素周边大小 本章,我们主要讨论一下页面中的某一个元素它的各种大小和各种位置的计算方式,以便更好的理解.   一.获取元素CSS大小 ...

  2. Git使用方法2.0

    ## Git来源: 最早开始是由Ruby程序员们发起的.Ruby是日本的家伙搞出来的,日本有个代码托管网站叫heroku,当时用这个的人比较多,现在这个网站还能打开,网址是www.heroku.com ...

  3. Scrum 冲刺 总结

    Scrum 冲刺 总结 冲刺阶段链接 Scrum冲刺第一天 Scrum冲刺第二天 Scrum冲刺第三天 Scrum冲刺第四天 Scrum冲刺第五天 Scrum冲刺第六天 Scrum冲刺第七天 冲刺阶段 ...

  4. pandas 数据分析使用

    https://github.com/Erick-LONG/data_analysis/blob/master/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90%20%E9%8 ...

  5. 关于webService发布的wsdl中的import问题解决

    大家都知道jdk1.6及以后都支持了对webService的原生态的支持:它在发布时会生成一个wsdl和一个xsd(一个类只生成一个xsd)所以就保留了引用关系,如下: <?xml versio ...

  6. 前端面试题:JS中的let和var的区别

    最近很多前端的朋友去面试被问到let和var的区别,其实阮一峰老师的ES6中已经很详细介绍了let的用法和var的区别.我简单总结一下,以便各位以后面试中使用. ES6 新增了let命令,用来声明局部 ...

  7. mysql5.5中datetime默认值不能为NOW或者CURRENT_TIMESTAMP,用触发器解决

    mysql5.6及以上的版本datatime默认值可以为CURRENT_TIMESTAMP或者NOW 那我们要用的是mysql5.5及以下版本呢? 请看代码 delimiter // DROP TRI ...

  8. monog和github学习

    1.导出服务器数据库到本地以json的格式储存:mongoexport -h ip -d dbname -c user -o D:\mondb\user.json2.导入本地Json到本地项目中:D: ...

  9. typescript简介

    微软作为编译器狂魔一直有一个心病,就是改良JavaScript这种语法超级烂又很多人用的编程语言,于是TypeScript诞生了 先做个对比吧:   TS JS 语法严谨性 严谨 宽松 静态性 静态 ...

  10. jhipster生成项目无法使用restful请求,报access_denied 403错误

    写在前边: 我们的微服务是注册中心.uaa.gateway为基础,添加微服务应用,昨天下午在测试jhipster的增删改查,因为jhipster生成的代码都是restful的,好不容易找到网关配置的映 ...