poj 3613Cow Relays

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.

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
Source

USACO 2007 November Gold

题面

用一个矩阵a(i, j)来表示i到j经过若干条边的最短路,
初始化a为i到j边的长度,没有则是正无穷。
比如a矩阵表示经过n条边,b矩阵表示经过m条边,
那么a * b得到的矩阵表示经过m + n条边,
采用Floyd的思想进行更新。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #include<string>
 8 #include<map>
 9 #define ll long long
10 using namespace std;
11 ll n,m,S,T,l;
12 map<ll,ll>id;
13 struct node{
14     ll a[202][202];
15     friend node operator *(node x,node y)
16     {
17          node z;
18          memset(z.a,0x3f,sizeof(z.a));
19          for(ll k=1;k<=l;k++)
20           for(ll i=1;i<=l;++i)
21            for(ll j=1;j<=l;++j)
22             z.a[i][j]=min(z.a[i][j],x.a[i][k]+y.a[k][j]);
23          return z;
24     }
25 }s,ans;
26 void ksm()
27 {
28     ans=s;
29     n--;
30     while(n)
31     {
32         if(n&1) ans=ans*s;
33         s=s*s;
34         n>>=1;
35     }
36 }
37 int main()
38 {
39     freopen("run.in","r",stdin);
40     freopen("run.out","w",stdout);
41     memset(s.a,0x3f,sizeof(s.a));
42     scanf("%lld%lld%lld%lld",&n,&m,&S,&T);
43     for(ll i=1,x,y,z;i<=m;++i)
44     {
45        scanf("%lld%lld%lld",&z,&x,&y);
46        if(id[x]) x=id[x];
47        else l++,id[x]=l,x=l;
48        if(id[y]) y=id[y];
49        else l++,id[y]=l,y=l;
50        s.a[x][y]=s.a[y][x]=z;
51     }
52     S=id[S];T=id[T];
53     ksm();
54     printf("%lld",ans.a[S][T]);
55     return 0;
56 }
57 /*
58 2 6 6 4
59 11 4 6
60 4 4 8
61 8 4 9
62 6 6 8
63 2 6 9
64 3 8 9
65 10
66 */

原文地址:https://www.cnblogs.com/adelalove/p/9835410.html

时间: 2024-08-30 16:35:52

poj 3613Cow Relays的相关文章

POJ 3613Cow Relays( floyd 倍增法)

题意:从s点出发到达e点且n条边的最短路是多少(可以走重复的路径) 图中点<=200,n<=1000,000 思路:folyd可以实现向路径中添边,但是这题与普通的求最短路问题不一样,比如从S到E经过X条边后就已经达到了最短路,这个时候仍然要强制用folyd再添边,尽管添边后就不是最短路了,但是要注意到添加的这边要使最短路损失最小,抓住这点用folyd可以实现强制添边的操作,所以可以从n=1的状态向n的状态转移 所以可以对原来的map进行n-1次folyd,但是n的范围太大,最坏的情况要进行1

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步最短路

POJ 3613 Cow Relays (Floyd + 矩阵快速幂 + 离散化 神题!)

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

POJ 3613 Cow Relays (floyd + 矩阵快速幂)

题目大意: 求刚好经过K条路的最短路 我们知道如果一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把i-j 的路径长度存到A 中. 在A*A的过程中,不断取小的,那么最后得到的也就是i - j 走过两条路的最短路了. 当然也是利用到了floyd的思想. 然后要求出K次的最短路,那么就是矩阵快速幂的工作了. 注意要离散化.用map #include <cstdio> #include <io

【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到E,恰好经过N条边的最短路径(姑且称为路径吧,虽然好像已经不是了……) 总共只有大约200个点(很多点根本没走到,离散化一下即可)所以可以考虑Floyd算最短路. 引用下题解: 题目求i,j之间边数恰为N的最短路径(边可以重复走),我们知道线性代数中有:01邻接矩阵A的K次方C=A^K,C[i][j

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

BZOJ 1706 usaco 2007 Nov relays 奶牛接力跑/POJ 3613 Cow Relays 倍增Floyd

题目大意:求恰好走k步从S到T的最短路. 思路:设f[p][i][j]为从i到j恰好走2^p步的最短路,DP方程十分简单:f[p][i][j] = min(f[p][i][j],f[p - 1][i][k] + f[p - 1][k][j]); 对总步数T进行二进制拆分,在T有1的位置上,假如这个位置为p,那么就用f[p][][]来更新答案g[][],最后得到的g[][]就是答案矩阵. 注意要离散化一下.. CODE: #include <cstdio> #include <cstrin

poj 3613 Cow Relays(矩阵的图论意义)

题解 用一个矩阵来表示一个图的边的存在性,即矩阵C[i,j]=1表示有一条从i到j的有向边C[i,j]=0表示没有从i到j的边.这个矩阵的k次方后C[i,j]就表示有多少条从i到j恰好经过k条边的路径. 在此题中我们赋予边权值并把矩阵乘法中的+改为min这样这个矩阵的k次方后C[i,j]就表示从i到j恰好经过k条边的最短路径. 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include&l