题目描述

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. embed简介

    go embed 是 Go 1.16 中引入的特性,它允许将文件嵌入到 Go 代码中,以便在运行时访问这些文件.这对于将静态资源(如 HTML.CSS.JavaScript 文件)直接嵌入到 Go 二 ...

  2. Python字符串操作函数split()和join()

    字符串拆分 在python中有切片(Slice)操作符,可以对字符串进行截取,还提供了split()函数可以将一个 字符串分裂成多个字符串组成的列表.在使用split()函数来拆分字符串之前,我们先来 ...

  3. 细谈商品详情API接口设计

    一.引言 随着互联网技术的发展,商品详情信息的展示和交互变得越来越重要.为了提供更好的用户体验,我们需要设计一套高效.稳定且易于扩展的商品详情API接口.本文将详细探讨商品详情API接口的设计,包括接 ...

  4. 通过提示大语言模型进行个性化推荐LLM-Rec: Personalized Recommendation via Prompting Large Language Models

    论文原文地址:https://arxiv.org/abs/2307.15780 本文提出了一种提示LLM并使用其生成的内容增强推荐系统的输入的方法,提高了个性化推荐的效果. LLM-Rec Promp ...

  5. Github的一个奇技淫巧

    背景 前段时间给 VictoriaLogs 提交了一个 PR: https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4934 本来一切都很顺 ...

  6. Avalonia开发(二)项目结构解析

    一.前言 在Avalonia开发(一)环境搭建 文章中介绍了Avalonia的介绍.开发环境的搭建.项目创建,以及项目FirstAvaloniaApp项目结构的介绍.本篇文章将介绍各平台的项目介绍. ...

  7. STL容器:map

    map 可以当作特殊的数组来使用,在数组开不下,或者数组下标不是整数的时候使用 map 就很方便,比如统计字符串的出现个数,统计 int 范围内的数的出现次数等等. 映射是指两个集合之间的元素的相互对 ...

  8. Python socket实现简单聊天,同步输入和接收消息

    查的资料很多都是必须等待接收数据后才能再次输入.做了修改,使用多线程的形式,实现一边输入,一边接收 服务端代码 import socket import threading import sys im ...

  9. Error in v-on handler: “TypeError: _user.default is not a function“

    碰到这个问题一开始以为是方法名重复了,后来检查了一遍也没发现方法名或者属性名重复然后发现是 这个导入方法时没加{}的问题. , 无语.

  10. Opencv系列之一:简介与基本使用

    1 Opencv简介 Opencv是计算机视觉中经典的专用库,其支持多语言,跨平台,功能强大.Opencv-Python为Opencv提供了Python接口,使得使用者在Python中能够调用C/C+ ...