poj3613 Cow Relays【好题】【最短路】【快速幂】
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:9207 | Accepted: 3604 |
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
题意:
在一个图上求从$S$到$E$的,刚好经过$n$条边的最短路径长。
思路:
没想到最短路的题目还可以用快速幂。也没想到快速幂还可以这么写。
这道题边最多是100条,所以可以先把点离散化。离散化后点的编号最大是$node_cnt$
最初的矩阵$G[i,j]$中存储的其实是从$i$经过一条边到达$j$的最短路
那么$G^{(2)}[i, j] = \min_{1\leq k\leq node_cnt}{G[i, k] + G[k, j]}$就可以表示从$i$经过两条边到达$j$的最短路
如果矩阵$G^{(m)}$表示任意两点之间恰好经过$m$条边的最短路,那么
$G^{(r+m)}[i, j] = \min_{1\leq k\leq node_cnt}{G^{(r)}[i, k] + G^{(m)}[k, j]}$
这就可以使用快速幂进行递推了。只需要把$matrix$的乘法操作中,$+=$变成$\min$, $*$变成$+$
注意矩阵要初始化为$+\infty$
#include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
#include<map>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f int n, t, S, E;
const int maxn = ;
int node_cnt;
struct matrix{
int m[maxn][maxn];
//int m_size;
matrix operator *(const matrix &b)const{
matrix ret;
memset(ret.m, 0x3f, sizeof(ret.m));
for(int i = ; i <= node_cnt; i++){
for(int j = ; j <= node_cnt; j++){
//ret.m[i][j] = inf;
for(int k = ; k <= node_cnt; k++){
ret.m[i][j] = min(m[i][k] + b.m[k][j], ret.m[i][j]);
}
}
}
return ret;
}
}g;
//int g[maxn][maxn];
struct edge{
int u, v, length;
}e[];
set<int>nodes;
set<int>::iterator set_it;
map<int, int>node_mp; matrix ksm(matrix a, int x)
{
matrix ret, k;
k = a;
ret = a;
x--;
while(x){
if(x & ){
ret = ret * k;
}
x >>= ;
k = k * k;
}
return ret;
} int main()
{
while(scanf("%d%d%d%d", &n, &t, &S, &E) != EOF){
for(int i = ; i < t; i++){
scanf("%d%d%d", &e[i].length, &e[i].u, &e[i].v);
nodes.insert(e[i].u);
nodes.insert(e[i].v);
} node_cnt = ;
for(set_it = nodes.begin(); set_it != nodes.end(); set_it++){
node_mp[*set_it] = ++node_cnt;
}
//g.m_size = node_cnt;
memset(g.m, 0x3f, sizeof(g.m));
for(int i = ; i < t; i++){
int u = e[i].u, v = e[i].v;
g.m[node_mp[u]][node_mp[v]] = e[i].length;
g.m[node_mp[v]][node_mp[u]] = e[i].length;
}
/*for(int i = 1; i <= node_cnt; i++){
for(int j = 1; j <= node_cnt; j++){
cout<<g.m[i][j]<<" ";
}
cout<<endl;
}*/ matrix ans = ksm(g, n);
//cout<<node_mp[S]<<" "<<node_mp[E]<<endl;
printf("%d\n", ans.m[node_mp[S]][node_mp[E]]);
}
return ;
}
poj3613 Cow Relays【好题】【最短路】【快速幂】的更多相关文章
- Cow Relays POJ - 3613 (floyd+快速幂)
For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race usin ...
- POJ3613 Cow Relays(矩阵快速幂)
题目大概要求从起点到终点恰好经过k条边的最短路. 离散数学告诉我们邻接矩阵的k次幂就能得出恰好经过k条路的信息,比如POJ2778. 这题也一样,矩阵的幂运算定义成min,而min满足结合律,所以可以 ...
- [POJ3613] Cow Relays(Floyd+矩阵快速幂)
解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- 疯子的算法总结(九) 图论中的矩阵应用 Part 1+POJ3613 Cow Relays
图的存储有邻接矩阵,那么他就具备一些矩阵的性质,设有一个图的demo[100][100];那么demo[M][N]就是M—>N的距离,若经过一次松弛操作demo[M][N]=demo[M][K] ...
- 【POJ3613 Cow Relays】(广义矩阵乘法)
题目链接 先离散化,假设有\(P\)个点 定义矩阵\(A_{ij}\)表示\(i\)到\(j\)只经过一条边的最短路,\[{(A^{a+b})_{ij}=\min_{1\le k\le p} \{ ( ...
- [POJ3613] Cow Relays
link 题目大意 给你一个含有边权的无向图,问从$S$到$T$经过$N$条边的最小花费. 试题分析 我们可以很容易推导$dp$方程,$dp(k,i,j)$表示经过$k$条边从$i$到$j$的最小花费 ...
- (中等) CF 576D Flights for Regular Customers (#319 Div1 D题),矩阵快速幂。
In the country there are exactly n cities numbered with positive integers from 1 to n. In each city ...
- E题:Water Problem(快速幂模板)
题目大意:原题链接 题解链接 解题思路:令x=x-1代入原等式得到新的等式,两式相加,将sin()部分抵消掉,得到只含有f(x)的状态转移方程f(x+1)=f(x)+f(x-2)+f(x-3),然后 ...
随机推荐
- Android数据库大批量数据插入优化
对比在android中批量插入数据的3中方式对比(各插入1W条数据所花费的时间): 1. 一个一个插入 public static boolean insert(SQLiteOpenHelper op ...
- [转载]从100PV到1亿级PV网站架构演变
原文地址:http://www.uml.org.cn/zjjs/201307172.asp 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不同的方法,不同的方法下有 ...
- 团队工作效率分析工具gitstats
如果你是团队领导,关心团队的开发效率和工作激情:如果你是开源软件开发者,维护者某个repo:又或者,你关心某个开源软件的开发进度,那么你可以试一试gitstats. gitstats 是一个git仓库 ...
- IT? 挨踢
中国的IT,是最憋屈的IT. 他们掌握着正常人看不懂的英文+字母+标点符号组成的各类代码语言 他们像作者一样从无到有,从空白的白纸上敲出上千上万条华丽的计算机语言 但是他们承受着正常人的鄙视: 我的需 ...
- CentOS 7 安装SVN并整合HTTP访问
#!/bin/bash## -------------------------------------------------## 安装svn并整合http访问## ----------------- ...
- IOS开发系列之阿堂教程:玩转IPhone客户端和Web服务端交互(客户端)实践
说到ios的应用开发,我们不能不提到web server服务端,如果没有服务端的支持,ios应用开发就没有多大意义了,因为从事过手机开发的朋友都知道(Android也一样),大量复杂业务的处理和数据库 ...
- Java 汇编代码
https://shipilev.net/blog/2015/black-magic-method-dispatch/ https://github.com/shipilev/article-meth ...
- 针对个别utf8编码站点在iis7上浏览器编码不能自动识别的解决方法
个别utf8编码站点在iis7上客户端浏览器编码不能自动识别的编号,输入仍为gbk2312,而不是utf-8,这样导致我们看到的网站为乱码. 要解决这个问题,有两个方法,一为打开网站以后,右键,选择编 ...
- 9最好的JavaScript压缩工具
削减是一个从源代码中删除不必要的字符的技术使它看起来简单而整洁.这种技术也被称为代码压缩和最小化.在这里,我们为你收集了10个最好的JavaScript压缩工具将帮助您删除不必要的空格,换行符,评论, ...
- WARNING: Can not get binary dependencies for file...
环境: window7 64bit python 3.5 pyinstaller 3.2 用pyinstaller 将python文件打包成exe文件的过程中,出现了如下的错误 C:\Users\ca ...