题目描述

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.

输入格式

* 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

输出格式

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

样例 #1

样例输入 #1

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

样例输出 #1

10

有T条边连起来的图最多T+1个点,可以对所有点进行离散化。点的个数是T级别的。

首先有一个暴力的思路就是循环n次Floyd,然后输出s到e的距离。复杂度\(O(nT^3)\).\(T^3\)很难优化,考虑怎么优化\(n\)

发现Floyd的过程似乎是可以通过倍增来实现的。首先预处理出在\(n=2^i\)时任意两点之间的距离,然后把n拆成二进制数一个一个松弛上去。复杂度\(O(T^3logn)\),可以通过此题。

#include<bits/stdc++.h>
using namespace std;
int n,t,s,e,u[105],v[105],w[105],m,dp[25][205][205],f[2][205][205],c,lsh[205],fa[1005],x,y;
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
int main()
{
memset(dp,0x3f,sizeof(dp));
memset(f,0x3f,sizeof(f));
scanf("%d%d%d%d",&t,&m,&s,&e);
for(int i=1;i<=1000;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",w+i,u+i,v+i);
fa[find(u[i])]=find(v[i]);
}
for(int i=1;i<=1000;i++)
if(find(i)==find(s))
lsh[++n]=i;
for(int i=1;i<=n;i++)
f[1][i][i]=0;
for(int i=1;i<=m;i++)
{
x=lower_bound(lsh+1,lsh+n+1,u[i])-lsh;
y=lower_bound(lsh+1,lsh+n+1,v[i])-lsh;
if(lsh[x]==u[i]&&lsh[y]==v[i])
dp[0][x][y]=dp[0][y][x]=min(dp[0][x][y],w[i]);
}
for(int p=1;(1<<p)<=t;p++)
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[p][i][j]=min(dp[p][i][j],dp[p-1][i][k]+dp[p-1][k][j]);
}
}
}
}
for(int p=0;(1<<p)<=t;p++)
{
if(t&(1<<p))
{
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
f[c][i][j]=min(f[c][i][j],f[!c][i][k]+dp[p][k][j]);
}
}
}
c^=1;
memset(f[c],0x3f,sizeof(f[c]));
}
}
printf("%d",f[!c][lower_bound(lsh+1,lsh+n+1,s)-lsh][lower_bound(lsh+1,lsh+n+1,e)-lsh]);
return 0;
}

[USACO2007NOVG] Cow Relays G的更多相关文章

  1. 2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed)

    2021.11.03 P2886 [USACO07NOV]Cow Relays G(矩阵+floyed) [P2886 USACO07NOV]Cow Relays G - 洛谷 | 计算机科学教育新生 ...

  2. USACO07NOV Cow Relays G 题解

    题目 For their physical fitness program, \(N (2 ≤ N ≤ 1,000,000)\) cows have decided to run a relay ra ...

  3. 【图论】USACO07NOV Cow Relays G

    题目大意 洛谷链接 给定一张\(T\)条边的无向连通图,求从\(S\)到\(E\)经过\(N\)条边的最短路长度. 输入格式 第一行四个正整数\(N,T,S,E\),意义如题面所示. 接下来\(T\) ...

  4. [USACO07NOV]Cow Relays G

    题目大意 给出一张无向连通图(点数小于1000),求S到E经过k条边的最短路. 算法 这是之前国庆模拟赛的题 因为懒 所以就只挑一些题写博客 在考场上写了个dp 然后水到了50分 出考场和神仙们一问才 ...

  5. 洛谷P2886 [USACO07NOV]Cow Relays G (矩阵乘法与路径问题)

    本题就是求两点间只经过n条边的最短路径,定义广义的矩阵乘法,就是把普通的矩阵乘法从求和改成了取最小值,把内部相乘改成了相加. 代码包含三个内容:广义矩阵乘法,矩阵快速幂,离散化: 1 #include ...

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

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

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

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

  8. poj 3613 Cow Relays

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

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

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

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

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

随机推荐

  1. [ABC141E] Who Says a Pun?

    2023-02-17 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 dp,字符串 解题思路 看到求两个完全相同的子串时,我们可以发现其与求最长 ...

  2. AI绘画:StableDiffusion炼丹Lora攻略-实战萌宠图片生成

    写在前面的话 近期在小红书发现了许多极其可爱.美观的萌宠图片,对这些美妙的图像深深着迷 于是想着看看利用AI绘画StableDiffusion以下简称(SD)做出来. 以下是详细实操的全过程,包括所有 ...

  3. 聊聊HuggingFace如何处理大模型下海量数据集

    翻译自: Big data? Datasets to the rescue! 如今,使用大GB的数据集并不罕见,特别是从头开始预训练像BERT或GPT-2这样的Tranformer模型.在这样的情况下 ...

  4. 【Azure Batch】在批处理的Task中如何让它执行多个CMD指令呢

    问题描述 根据Azure Batch的入门文档(使用 Azure 门户创建 Batch 帐户并运行作业 : https://docs.azure.cn/zh-cn/batch/quick-create ...

  5. Tcp/Ip协议 A类B类C类D类 地址

    TCP(传输控制协议):负责和远程主机连接  Ip(网际协议):负责寻址,使报文发送到其该在的地方 Ip地址:是TCP/IP的网络层用以标识网络中主机的逻辑地址,可以唯一标识Interent中的一台主 ...

  6. 「codeforces - 1720」

    壹 最近 cq 情况很急急,昨天出去排核酸整了两个半小时,十分无语.提前放假自然是一大好事,但是一个人在家也蛮无聊.不要再涨体重了为好,这一年间他妈 delta 了 10 kilos,算了下 BMI ...

  7. 【Python进阶-PyQt5】00搭建PyQt5环境

    1.创建独立开发虚拟环境 1.1虚拟环境简介 我们编写的程序,有时用到的Python库是不一样的,比如说开发桌面应用程序我们主要用到PyQt5相关的Python库.开发Web应用程序我们主要用到Dja ...

  8. MySQL 的 InnoDB 存储引擎简介

    MySQL 是世界上最流行的开源关系型数据库管理系统之一,而其中的存储引擎则是其关键组成部分之一.InnoDB 存储引擎在 MySQL 中扮演了重要角色,提供了许多高级功能和性能优化,适用于各种应用程 ...

  9. DBeaver Ultimate 22.1.0 连接数据库(MySQL+Mongo+Clickhouse)

    前言 继续书接上文 Docker Compose V2 安装常用数据库MySQL+Mongo,部署安装好之后我本来是找了一个web端的在线连接数据库的工具,但是使用过程中并不丝滑,最终还是选择了使用 ...

  10. 基于LangChain的LLM应用开发1——介绍

    这是基于LangChain的大语言模型应用开发系列的第一篇. 文章内容会参考deeplearning.ai的短课程(https://learn.deeplearning.ai/langchain/), ...