题目描述

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. API接口设计规范

    说明:在实际的业务中,难免会跟第三方系统进行数据的交互与传递,那么如何保证数据在传输过程中的安全呢(防窃取)?除了https的协议之外,能不能加上通用的一套算法以及规范来保证传输的安全性呢? 下面我们 ...

  2. 在微服务环境下,远程调用feign和异步线程存在请求数据丢失问题

    一.无异步线程得情况下feign远程调用: 0.登录拦截器: @Component public class LoginUserInterceptor implements HandlerInterc ...

  3. 2017-A

    2017-A 题目描述: 输入一个字符串,要求输出能把所有的小写字符放前面,大写字符放中间,数字放后面,并且中间用空格隔开,如果同种类字符间有不同种类的字符,输出后也要用字符隔开. 例: 输入 12a ...

  4. Z-Blog火车头免登录发布教程+插件3.2+支持最新Z-Blog1.7

    Z-Blog免登录采集评论,之前没有加入评论接口,今天把评论接口写好了,写一下简单的教程,(采集评论规则是一件很麻烦的事)有时候采集文章的时候也采集评论,今天教大家怎样用我的Z-Blog免登录采集插件 ...

  5. 小札 Combinatorics 2

    对于 Newton Expansion,式子本身的证明其实无甚可翻新的花样,但是题还是很有意思的.比如 codeforces - 1332E Height All the Same 这个. 首先给出几 ...

  6. 关于Teamcenter RAC开发如何查看Soa调用情况,已经查看反编译源码

  7. Netty集成HTTP的GET和POST通讯

    核心就是ChannelInitializer的实现使用http 消息解码器 package com.coremain.handler; import io.netty.channel.ChannelI ...

  8. Flask框架——Flask脚本、flask知识点补充

    文章目录 Flask_脚本 1 集成Python shell 1.1 flask-script的用法: 1.1.1 实例:flask-script的简单实现 1.1.1命令添加方式: 第一种(无参命令 ...

  9. 基于Java Swing和BouncyCastle的证书生成工具

    "Almost no one will remember what he had just not interested." - Nobody "几乎没有人会记得他所丝毫 ...

  10. #866 div1A

    A. Constructive Problem 题意:给定一个长度为n的非负数组a,我们可以进行一次操作,操作是将l~r这个区间内的所有数变为k(k >= 0),得到b,能不能使mex(a)+ ...