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. Beta 第六天

    今天遇到的困难: github服务器响应很慢 推图的API接口相应较慢,超过了初始设定的最大延迟时间,导致了无法正确返回图片 ListView滑动删除Demo出现了某些Bug,这些Bug可能导致了某些 ...

  2. 项目Alpha冲刺Day7

    一.会议照片 二.项目进展 1.今日安排 今天都是课,主要就是用空闲时间熟悉一下框架使用以及继续进行框架搭建. 2.问题困难 前台界面框架vue和element-ui的写法要适应. 3.心得体会 vu ...

  3. Python IDE Spyder的简单介绍

    最近深度学习发展非常迅猛,大有一统江湖的趋势.经过一段时间学习,发现自己对这种神奇的玄学非常感兴趣,希望能够进一步的研究.而这种研究性学科单纯地看论文比较难以明白,所以希望能够跟进大牛们写的代码深入学 ...

  4. XML使用练习

    #!/usr/bin/env python # -*- coding:utf-8 -*- import requests from xml.etree import ElementTree as ET ...

  5. 前端双引号单引号,正则反向引用,js比较jq

    1.js,jq,css,html属性必须双,如果同时出现需要嵌套使用,属性的规范是双但是也可以用单测试有效 单引号现象举例:jq中获取元素标签是单引号:$('input').click:弹出也是单引号 ...

  6. MySQL默认储存引擎修改

    1.输入以下SQL语句查看当前储存引擎支持: SHOW ENGINES; 如图所示本机默认引擎为MyISAM: 2.若要修改引擎执行: ALTER TABLE 表名 ENGINE = 储存引擎名: 3 ...

  7. GZip 压缩及解压缩

    /// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...

  8. Mego开发文档 - 快速概述

    Mego 快速概述 Mego 是一款轻量级,可扩展和跨平台的数据访问技术. Mego 是一个对象关系映射器(O / RM),它使.NET开发人员能够使用.NET对象处理数据库.它消除了开发人员通常需要 ...

  9. Mac 中配置Apache

    使用的mac版本是10.10.1,mac自带的Apache环境 分为两部分: 1.启动Apache 2.设置虚拟主机 启动Apache 打开终端, >>sudo apachectl -v, ...

  10. 28.C++- 单例类模板(详解)

    单例类 描述 指在整个系统生命期中,一个类最多只能有一个实例(instance)存在,使得该实例的唯一性(实例是指一个对象指针)  , 比如:统计在线人数 在单例类里,又分为了懒汉式和饿汉式,它们的区 ...