P2886 [USACO07NOV]牛继电器Cow Relays

题目描述

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条边的最短路。

输入输出格式

输入格式:

* 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: 复制

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

输出样例#1: 复制

10

程序:
//洛谷2886
//(1)对角线不能设置为0,否则容易自循环。(2)数组要放在主程序外面
//(3)K条边,不一定是最简路
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
map<int,int>f;
const int maxn=210;
int k,t,s,e,n;
int a[maxn][maxn];
struct Matrix{
    int b[maxn][maxn];
};
Matrix A,S;
Matrix operator *(Matrix A,Matrix B){//运算符重载
    Matrix c;
    memset(c.b,127/3,sizeof(c.b) );
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                c.b[i][j]=min(c.b[i][j],A.b[i][k]+B.b[k][j]);
    return c;
}
Matrix power(Matrix A,int k){
    if(k==0) return A;
    Matrix S=A;
    for(int i=1;i<=n;i++){
            for (int j=1;j<=n;j++)
                cout<<A.b[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;
    while(k){
        if(k&1)S=S*A;//奇数执行,偶数不执行
        /*cout<<k<<":"<<endl;
            for(int i=1;i<=n;i++){
            for (int j=1;j<=n;j++)
                cout<<S.b[i][j]<<" ";
            cout<<endl;
        }*/
        cout<<endl;
        A=A*A;
        k=k>>1;

    }
    return S;
}
int main(){
    cin>>k>>t>>s>>e;
    int w,x,y;
    memset(A.b,127/3,sizeof(A.b) );// 赋初值
    n=0;
    for(int i=1;i<=t;i++){//t<100,x<1000  点不是从1开始的,可以用map离散化,给点从一开始编号。n记录共有多少个点。
        cin>>w>>x>>y;
        if (f[x]==0)    f[x]=++n;
        if (f[y]==0)    f[y]=++n;
        A.b[f[x]][f[y]]=A.b[f[y]][f[x]]=min(w,A.b[f[y]][f[x]]);
    }
    S=power(A,k-1);
    s=f[s];
    e=f[e];

    cout<<S.b[s][e];
    return 0;
}


原文地址:https://www.cnblogs.com/ssfzmfy/p/10740803.html

时间: 2024-11-12 13:03:30

P2886 [USACO07NOV]牛继电器Cow Relays的相关文章

[LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离散化大可不必sort+unique,因为我们甚至并不在乎相对大小,只是要一个唯一编号,可以开一个桶记录 之所以进行k-1次快速幂,是因为邻接矩阵本身就走了一步了. #include<iostream> #include<cstring> #include<cstdio> u

Luogu P2886 [USACO07NOV]牛继电器Cow Relays|最短路,倍增

题目链接 题意:给出一张无向连通图,求S到E经过N条边的最短路. 数据范围:边数\(\le 100\),顶点编号\(\le1000\),\(N\le1 \times 10^6\) 题解: 最短路有三种解法,这种数据范围可使用\(floyd\) 可以用\(f[i][j][k]\)表示从\(i\)到\(j\)经过\(k\)条边的最短路,显然TLE 考虑倍增.预处理\(K=2^k\),此时\(f[i][j][k]=min\{f[i][l][k-1] + f[l][j][k-1]\}\) 将\(n\)二

[USACO07NOV]牛继电器Cow Relays

https://www.luogu.org/problem/P2886 题目描述: 给出一张无向连通图,求$S$到$E$经过$k$条边的最短路. 对于一类$S$到$E$走指定数量的边数,求它的最短路或条数,都可以采用矩阵快速幂的方式解决.我们回忆一下那一个慢得惊人的$floyd$算法,将它的$dp$方程式提取出来. $floyd[i][j]=min(floyd[i][k]+floyd[k][j])$ 如果我们把$floyd$看做一个矩阵,那么矩阵乘法的标准式则为: $floyd[i][j]=\s

[luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)

传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring> #include <algorithm> #define N 101 #define min(x, y) ((x) < (y) ? (x) : (y)) int n, t, s, e, cnt; int x[N], y[N], z[N], a[N]; struct Matrix {

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i

矩阵十题【十】 poj 3613 Cow Relays

题目链接:http://poj.org/problem?id=3613 题目大意: 输入N,T,S,E,N表示要走的边数,T表示一共有几条边,S表示开始的点,E表示结束的点 给出一张无向连通图,求S到E经过N条边的最短路. N (2 ≤ N ≤ 1,000,000) T (2 ≤ T ≤ 100) (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000) 1 ≤ lengthi  ≤ 1,000 题目主要的思想就是用矩阵的乘法模拟出Floyd进行运算,是个很好的题目. //k步最短路

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

Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 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

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

poj 3613 Cow Relays

Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 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