Cow 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: NTS, 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

题意:在无向图中有n条边,现在给出你一个起点S和一个终点E,让你求从S到E经过且仅K条边的最短路径。注意此题中K远大于n,如果K小于n的话直接一边广搜就过了,第一次没注意到这个条件敲了一个BFS,结果WA了。

思路:此题正解应该是矩阵乘法,但是重定义了,区别于线性代数里面的乘法(其实可以看出无论哪种定义,只要能推出矩阵在该定义下满足交换律即可,因为可以用快速幂来加速)。
设原图G对应的邻接矩阵为M,则M的k次幂中M[i][j]就表示从i点到j点经过k条边路径的个数!那么只需要重新定义一下矩阵乘法:M[i][j]表示从i点到j点的的最短路径长度,即M[i][j] = min(M[i][j],M[i][k]+M[k][j])(这个就是floyd算法的核心,DP思想),可以证明该定义满足交换律,因此可以用快速幂,考虑M^2,它表示从i到j经过2条边的最短路径,同理推出M^n表示从i到j经过n条边的最短路径,因此本题得解。
关于矩阵乘法的应用是参考2008年国家集训队论文《矩阵乘法在信息学中的应用》(俞华程)中看到的,网上此题解法大都参考该论文,在网上看了别人解释的没怎么看懂,直接看论文去了,发现论文里面讲的很明白也很透彻,但是经过别人转述意思可能就不一样了,其实我也说的不怎么清楚,所以建议直接去看论文。
网盘下载地址:http://yunpan.cn/QNeFIw2wIef4B (访问密码:7b0c)
 #include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 111
using namespace std;
class Matrix{
public:
int m[MAXN][MAXN];
Matrix(){
memset(m, -, sizeof(m));
}
};
int N = ;
Matrix mtMul(Matrix A, Matrix B){
Matrix tmp;
for(int i = ;i < N;i ++)
for(int j = ;j < N;j ++)
for(int k = ;k < N;k ++){
if(A.m[i][k] == - || B.m[k][j] == -) continue;
int temp = A.m[i][k] + B.m[k][j];
if(tmp.m[i][j] == - || tmp.m[i][j] > temp) tmp.m[i][j] = temp;
}
return tmp;
}
Matrix mtPow(Matrix A, int k){
if(k == ) return A;
Matrix tmp = mtPow(A, k >> );
Matrix res = mtMul(tmp, tmp);
if(k & ) res = mtMul(res, A);
return res;
}
int main(){
int cnt[];
int n, t, s, e;
int u, v, w;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d%d%d", &n, &t, &s, &e)){
N = ;
Matrix G;
memset(cnt, -, sizeof(cnt));
for(int i = ;i < t;i ++){
scanf("%d%d%d", &w, &u, &v);
if(cnt[u] == -) cnt[u] = N++;
if(cnt[v] == -) cnt[v] = N++;
G.m[cnt[u]][cnt[v]] = w;
G.m[cnt[v]][cnt[u]] = w;
}
Matrix tmp = mtPow(G, n);
printf("%d\n",tmp.m[cnt[s]][cnt[e]]);
}
return ;
}
另外附上BFS的错误代码:
 #include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define MAXN 1111
using namespace std;
class Status{
public:
int pre, w, cnt;
bool operator < (const Status &a) const{
return w < a.w;
}
};
typedef struct{
int to, next, w;
}Edge;
Edge edge[];
priority_queue<Status>q;
int head[MAXN], N, T, S, E;
void addedge(int u, int v, int w, int k){
edge[k].to = v;
edge[k].next = head[u];
edge[k].w = w;
head[u] = k++;
edge[k].to = u;
edge[k].next = head[v];
edge[k].w = w;
head[v] = k;
}
void bfs(int s){
while(!q.empty()) q.pop();
Status tmp;
tmp.pre = s;
tmp.w = tmp.cnt = ;
q.push(tmp);
while(!q.empty()){
Status p = q.top();
int v = p.pre;
q.pop();
for(int i = head[v]; ~i; i = edge[i].next){
int u = edge[i].to;
if(u == E && p.cnt+ == N){
printf("%d\n", p.w+edge[i].w);
return;
}else if(u != E){
Status t;
t.pre = u;
t.w = p.w+edge[i].w;
t.cnt = p.cnt+;
q.push(t);
}
}
}
}
int main(){
int length, u, v, k;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d%d%d", &N, &T, &S, &E)){
memset(head, -, sizeof(head));
k = ;
for(int i = ; i < T; i ++){
scanf("%d%d%d", &length, &u, &v);
addedge(u, v, length, k);
k += ;
}
bfs(S);
}
return ;
}

 

POJ --- 3613 (K步最短路+矩阵快速幂+floyd)的更多相关文章

  1. poj3613Cow Relays——k边最短路(矩阵快速幂)

    题目:http://poj.org/problem?id=3613 题意就是求从起点到终点的一条恰好经过k条边的最短路: floyd+矩阵快速幂,矩阵中的第i行第j列表示从i到j的最短路,矩阵本身代表 ...

  2. poj 3613 Cow Relays【矩阵快速幂+Floyd】

    !:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...

  3. POJ - 2778 ~ HDU - 2243 AC自动机+矩阵快速幂

    这两题属于AC自动机的第二种套路通过矩阵快速幂求方案数. 题意:给m个病毒字符串,问长度为n的DNA片段有多少种没有包含病毒串的. 根据AC自动机的tire图,我们可以获得一个可达矩阵. 关于这题的t ...

  4. POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)

    题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...

  5. poj 3735 Training little cats(矩阵快速幂,模版更权威,这题数据很坑)

    题目 矩阵快速幂,这里的模版就是计算A^n的,A为矩阵. 之前的矩阵快速幂貌似还是个更通用一些. 下面的题目解释来自 我只想做一个努力的人 @@@请注意 ,单位矩阵最初构造 行和列都要是(猫咪数+1) ...

  6. POJ 3233 Matrix Power Series (矩阵快速幂)

    题目链接 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A^2 + A^3 + - ...

  7. POJ 3233 Matrix Power Series(矩阵快速幂)

    Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 19338 Accepted: 8161 ...

  8. 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)

    题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...

  9. 图论专题训练1-D(K步最短路,矩阵连乘)

    题目链接 /* *题目大意: *求出从i到j,刚好经过k条边的最短路; * *矩阵乘法的应用之一(国家队论文): *矩阵乘法不满足交换律,矩阵乘法满足结合律; *给定一个有向图,问从A点恰好走k步(允 ...

随机推荐

  1. Linux内核Radix Tree(三):API介绍

    1.     单值查找radix_tree_lookup 函数radix_tree_lookup执行查找操作,查找方法是:从叶子到树顶,通过数组索引键值值查看数组元素的方法,一层层地查找slot.其列 ...

  2. JSON对象的stringify()和parse()方法

    1.stringify() ---- JavaScript对象序列化为JSON字符串 eg1. var book = {title: 'JS', authors: ['Van'], edition:3 ...

  3. JSP Ajax

    html代码: <!DOCTYPE html> <html> <script> function display() { var div=document.getE ...

  4. django 的请求处理部分----WSGIHandler 源码分析 django1.5.5

    从这里拉开django框架的帷幕..撕开,哈哈 主要涉及的类:django.core.handlers.base.BaseHandler,django.core.handlers.wsgi.WSGIH ...

  5. js函数文件排序化

    因为本人的某些小强迫症,写了一个格式化并根据js函数名排序的c++程序,此作mark #include <stdio.h> #include <map> #include &l ...

  6. HIVE中内连接和左半连接不一致问题

    一.理论 HIVE中都是按等值连接来统计的,理论上两种写法统计结果应该是一致的: 二.实际情况 但实际使用中发现两种写法会返回的结果,总会有一些差距虽然差别不大,但让人很是困惑. 三.原因 当使用jo ...

  7. IQKeyboredManager使用

    这个库是一个单例,它一旦生效,全项目任何界面都有效.让它生效的代码可以写在任意位置,我写在AppDelegate里. 1 2 3 4 5 6 7 8 9 10 - (BOOL)application: ...

  8. Elasticsearch强大的聚合功能Facet

    在常规数据库中,我们都知道有一个sql就是group,分组.如果主表只有对应的一个列记录的分组的ID,那么还好统计,比如说每本书book表,有一个分类catId,记录是属于哪一类的书,那么直接按照ca ...

  9. BZOJ 3996 [TJOI 2015] 线性代数 解题报告

    首先,我们可以得到: $$D = \sum_{i=1}^{n}\sum_{j=1}^{n}a_i\times a_j\times b_{i,j} - \sum_{i=1}^{n}a_i\times c ...

  10. 黑客是怎样绕过WAF之三重防护绕过讲解

    什么是WAF Web Application Firewall 通过执行一系列针对HTTP/HTTPS的安全策略来防御对Web应用的攻击. 目前主要有单设备WAF与云WAF WAF的现状 1.太多数W ...